Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E1.37-5 JSON Metadata Defines / Schema for Manufacturer PIDs #35

Draft
wants to merge 50 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
2740476
WIP
patrick-dmxc Sep 13, 2024
684480d
Start implementing JSON-Parser
patrick-dmxc Sep 16, 2024
069f5ef
CleanUp
patrick-dmxc Sep 16, 2024
8ef0200
Work on OneOfTypes
patrick-dmxc Sep 16, 2024
d8c767c
Minor
patrick-dmxc Sep 16, 2024
3489e58
Minor
patrick-dmxc Sep 16, 2024
56471b0
Add missing Types
patrick-dmxc Sep 17, 2024
a5ffb11
Minor
patrick-dmxc Sep 17, 2024
22bce8b
Wrote Test for JSON-Deserielizer
patrick-dmxc Sep 17, 2024
0696394
Remove Not working and not defined Parameters
patrick-dmxc Sep 17, 2024
50c8917
Add more Tests and improve code while adding Tests
patrick-dmxc Sep 17, 2024
081c376
More optimizations
patrick-dmxc Sep 17, 2024
ed6d313
More tests and optimizations
patrick-dmxc Sep 18, 2024
3ab8529
MInor
patrick-dmxc Sep 18, 2024
402368f
Implement PDL-Calculation
patrick-dmxc Sep 19, 2024
5a0bbe9
More Tests
patrick-dmxc Sep 19, 2024
c9202aa
Add more Tests, achieve 100% Code Coverage on Metadata for now
patrick-dmxc Sep 20, 2024
ff47503
Start Implement Type Parsing
patrick-dmxc Sep 21, 2024
c13e5b6
Fix Build
patrick-dmxc Sep 24, 2024
d8eb6f1
WIP
patrick-dmxc Sep 24, 2024
c0516ca
WIP
patrick-dmxc Sep 24, 2024
551e5d4
MInor Fix
patrick-dmxc Sep 24, 2024
978f3e7
Minor
patrick-dmxc Sep 24, 2024
769bd80
WIP
patrick-dmxc Sep 24, 2024
f95c1ec
Minor
patrick-dmxc Sep 25, 2024
67240aa
WIP
patrick-dmxc Sep 25, 2024
4e9782d
Fix BitFieldType-Parser and wrote tests for it
patrick-dmxc Sep 25, 2024
f2cee92
Minor
patrick-dmxc Sep 25, 2024
40311a6
MOOOOOOR TESTS
patrick-dmxc Sep 25, 2024
ea070df
Moooore Tests
patrick-dmxc Sep 25, 2024
87c4171
Implement Parser for ByteType and wrote Tests
patrick-dmxc Sep 25, 2024
5094b6a
minor
patrick-dmxc Sep 25, 2024
1aabd78
Wip Parse StringType
patrick-dmxc Oct 2, 2024
5ecf37a
Minor
patrick-dmxc Oct 2, 2024
1a791d4
Implement Array Format for BytesType
patrick-dmxc Oct 3, 2024
0239ca3
Test CompoundType Parser Stuff
patrick-dmxc Oct 3, 2024
ba00e59
Minor
patrick-dmxc Oct 3, 2024
e3522d2
Add Tests for ListType Parser
patrick-dmxc Oct 3, 2024
9d084b4
More Tests
patrick-dmxc Oct 3, 2024
52314a5
More Tests and Coverage
patrick-dmxc Oct 3, 2024
f89fd52
Add Test for debugFailing Linux Tests
patrick-dmxc Oct 3, 2024
514a573
try fix Tests
patrick-dmxc Oct 3, 2024
aaeba4a
try fix tests
patrick-dmxc Oct 3, 2024
87d9186
MInor
patrick-dmxc Oct 3, 2024
7c8c941
Wrote Parse Tests for ReferenceType
patrick-dmxc Oct 3, 2024
7523afe
Add more Tests
patrick-dmxc Oct 15, 2024
d2d3030
Grrr
patrick-dmxc Oct 15, 2024
6520c47
More Tests
patrick-dmxc Oct 22, 2024
35f941f
Work on PeerToPeerProcess and Tests
patrick-dmxc Oct 22, 2024
e636b05
More Tests
patrick-dmxc Oct 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions RDMSharp/Metadata/DataTree.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace RDMSharp.Metadata
{
public readonly struct DataTree : IEquatable<DataTree>
{
public readonly string Name;
public readonly uint Index;
public readonly object? Value;

Check warning on line 11 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 11 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 11 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 11 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 11 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 11 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 11 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 11 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
public readonly string? Unit;

Check warning on line 12 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 12 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 12 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 12 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 12 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 12 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 12 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 12 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
public readonly DataTreeValueLabel[]? Labels;

Check warning on line 13 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 13 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 13 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 13 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 13 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 13 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
public readonly DataTree[]? Children;

Check warning on line 14 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 14 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 14 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 14 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 14 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 14 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
public readonly DataTreeIssue[]? Issues;

Check warning on line 15 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 15 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 15 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 15 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 15 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 15 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

private DataTree(string name, uint index, DataTreeIssue[]? issues = null)

Check warning on line 17 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 17 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 17 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 17 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 17 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 17 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
Name = name;
Index = index;
Issues = issues;
}
public DataTree(DataTree dataTree, uint index) : this(dataTree.Name, index, dataTree.Issues)
{
Value = dataTree.Value;
Unit = dataTree.Unit;
Labels = dataTree.Labels;
Children = dataTree.Children;
}
public DataTree(string name, uint index, object value, DataTreeIssue[]? issues = null, string unit = null, DataTreeValueLabel[] labels = null) : this(name, index, issues)

Check warning on line 30 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 30 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 30 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 30 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 30 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 30 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
if (value is IEnumerable<DataTree> || value is DataTree[] children)
throw new ArgumentException($"Use other Constructor if you use {nameof(Children)}");

Value = value;
Unit = unit;
Labels = labels;
}

public DataTree(string name, uint index, DataTree[] children, DataTreeIssue[]? issues = null) : this(name, index, issues)

Check warning on line 40 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 40 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 40 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 40 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 40 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 40 in RDMSharp/Metadata/DataTree.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
Children = children;
}

public override string ToString()
{
return $"[{Index}] {Name}: {Value}";
}

public override bool Equals(object obj)
{
return obj is DataTree tree && Equals(tree);
}

public bool Equals(DataTree other)
{
return Name == other.Name &&
Index == other.Index &&
EqualityComparer<object>.Default.Equals(Value, other.Value) &&
compairArrays(this, other);

bool compairArrays(DataTree _this, DataTree other)
{
if (_this.Children != null)
{
if (!_this.Children.SequenceEqual(other.Children))
return false;
}
else if (other.Children != null)
return false;
if (_this.Issues != null)
{
if (!_this.Issues.SequenceEqual(other.Issues))
return false;
}
else if (other.Issues != null)
return false;

return true;
}
}

public override int GetHashCode()
{
return HashCode.Combine(Name, Index, Value, Children, Issues);
}

public static bool operator ==(DataTree left, DataTree right)
{
return left.Equals(right);
}

public static bool operator !=(DataTree left, DataTree right)
{
return !(left == right);
}
}
}
20 changes: 20 additions & 0 deletions RDMSharp/Metadata/DataTreeIssue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace RDMSharp.Metadata
{
public readonly struct DataTreeIssue
{
public readonly string Description;

public DataTreeIssue(string description)
{
if (string.IsNullOrWhiteSpace(description))
throw new ArgumentNullException($"{nameof(description)} has to be a vaild String");
Description = description;
}
public override string ToString()
{
return Description;
}
}
}
14 changes: 14 additions & 0 deletions RDMSharp/Metadata/DataTreeValueLabel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace RDMSharp.Metadata
{
public readonly struct DataTreeValueLabel
{
public readonly object Value;
public readonly string Label;

public DataTreeValueLabel(object value, string label)
{
Value = value;
Label = label;
}
}
}
15 changes: 15 additions & 0 deletions RDMSharp/Metadata/DefineNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace RDMSharp.Metadata
{
public class DefineNotFoundException : Exception
{
public DefineNotFoundException()
{
}

public DefineNotFoundException(string message) : base(message)
{
}
}
}
78 changes: 78 additions & 0 deletions RDMSharp/Metadata/JSON/Command.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Linq;
using System.Text.Json.Serialization;
using RDMSharp.Metadata.JSON.Converter;
using RDMSharp.RDM;
using OneOf = RDMSharp.Metadata.JSON.OneOfTypes.OneOfTypes;

namespace RDMSharp.Metadata.JSON
{
[JsonConverter(typeof(CommandConverter))]
public readonly struct Command
{
[JsonConverter(typeof(CustomEnumConverter<ECommandDublicte>))]
public enum ECommandDublicte
{
[JsonPropertyName("get_request")]
GetRequest,
[JsonPropertyName("get_response")]
GetResponse,
[JsonPropertyName("set_request")]
SetRequest,
[JsonPropertyName("set_response")]
SetResponse,
[JsonPropertyName("different_pid")]
DifferentDid
}
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public readonly ECommandDublicte? EnumValue { get; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public readonly OneOf? SingleField { get; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public readonly OneOf[]? ListOfFields { get; }

Check warning on line 31 in RDMSharp/Metadata/JSON/Command.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 31 in RDMSharp/Metadata/JSON/Command.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 31 in RDMSharp/Metadata/JSON/Command.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 31 in RDMSharp/Metadata/JSON/Command.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 31 in RDMSharp/Metadata/JSON/Command.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 31 in RDMSharp/Metadata/JSON/Command.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 31 in RDMSharp/Metadata/JSON/Command.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 31 in RDMSharp/Metadata/JSON/Command.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 31 in RDMSharp/Metadata/JSON/Command.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 31 in RDMSharp/Metadata/JSON/Command.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 31 in RDMSharp/Metadata/JSON/Command.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 31 in RDMSharp/Metadata/JSON/Command.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 31 in RDMSharp/Metadata/JSON/Command.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 31 in RDMSharp/Metadata/JSON/Command.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

public bool GetIsEmpty()
{
if (EnumValue != null)
return false;
if (SingleField != null)
return false;
if (ListOfFields != null)
return ListOfFields.Length == 0;

return true;
}
public Command(ECommandDublicte enumValue)
{
EnumValue = enumValue;
}
public Command(OneOf singleField)
{
SingleField = singleField;
}
public Command(OneOf[] listOfFields)
{
ListOfFields = listOfFields;
}
public PDL GetDataLength()
{
if(GetIsEmpty())
return new PDL();
if (SingleField.HasValue)
return SingleField.Value.GetDataLength();
if (ListOfFields != null)
return new PDL(ListOfFields.Select(f => f.GetDataLength()).ToArray());

throw new System.NotSupportedException();
}
public override string ToString()
{
if (EnumValue.HasValue)
return EnumValue.Value.ToString();
if (SingleField.HasValue)
return SingleField.Value.ToString();
if (ListOfFields != null)
return $"[ {string.Join("; ", ListOfFields.Select(f => f.ToString()))} ]";
return base.ToString();
}
}
}
34 changes: 34 additions & 0 deletions RDMSharp/Metadata/JSON/CommonPropertiesForNamed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using RDMSharp.RDM;
using System.Text.Json.Serialization;

namespace RDMSharp.Metadata.JSON
{
public abstract class CommonPropertiesForNamed
{
[JsonPropertyName("name")]
[JsonIgnore(Condition = JsonIgnoreCondition.Never)]
public abstract string Name { get; }
[JsonPropertyName("displayName")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public abstract string DisplayName { get; }
[JsonPropertyName("notes")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public abstract string? Notes { get; }

Check warning on line 16 in RDMSharp/Metadata/JSON/CommonPropertiesForNamed.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 16 in RDMSharp/Metadata/JSON/CommonPropertiesForNamed.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 16 in RDMSharp/Metadata/JSON/CommonPropertiesForNamed.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 16 in RDMSharp/Metadata/JSON/CommonPropertiesForNamed.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
[JsonPropertyName("resources")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public abstract string[]? Resources { get; }

Check warning on line 19 in RDMSharp/Metadata/JSON/CommonPropertiesForNamed.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 8.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 19 in RDMSharp/Metadata/JSON/CommonPropertiesForNamed.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 19 in RDMSharp/Metadata/JSON/CommonPropertiesForNamed.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 19 in RDMSharp/Metadata/JSON/CommonPropertiesForNamed.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 7.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

public abstract PDL GetDataLength();

public abstract byte[] ParsePayloadToData(DataTree dataTree);
public abstract DataTree ParseDataToPayload(ref byte[] data);

public override string ToString()
{
if(!string.IsNullOrWhiteSpace(DisplayName))
return DisplayName;

return Name;
}
}
}
45 changes: 45 additions & 0 deletions RDMSharp/Metadata/JSON/Converter/CommandConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Text.Json;
using System;
using System.Text.Json.Serialization;
using OneOf = RDMSharp.Metadata.JSON.OneOfTypes.OneOfTypes;

namespace RDMSharp.Metadata.JSON.Converter
{
public class CommandConverter : JsonConverter<Command>
{
public override Command Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
var enumValue = JsonSerializer.Deserialize<Command.ECommandDublicte>(ref reader, options);
return new Command(enumValue);
}
else if (reader.TokenType == JsonTokenType.StartObject)
{
var singleField = JsonSerializer.Deserialize<OneOf>(ref reader, options);
return new Command(singleField);
}
else if (reader.TokenType == JsonTokenType.StartArray)
{
var listOfFields = JsonSerializer.Deserialize<OneOf[]>(ref reader, options);
if (listOfFields.Length == 0)
return new Command();
return new Command(listOfFields);
}

throw new JsonException("Unexpected JSON format for FieldContainer.");
}

public override void Write(Utf8JsonWriter writer, Command value, JsonSerializerOptions options)
{
if (value.GetIsEmpty())
JsonSerializer.Serialize(writer, new object[0], options);
else if (value.EnumValue.HasValue)
JsonSerializer.Serialize(writer, value.EnumValue.Value, options);
else if (value.SingleField != null)
JsonSerializer.Serialize(writer, value.SingleField.Value, options);
else if (value.ListOfFields != null)
JsonSerializer.Serialize(writer, value.ListOfFields, options);
}
}
}
36 changes: 36 additions & 0 deletions RDMSharp/Metadata/JSON/Converter/CustomEnumConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace RDMSharp.Metadata.JSON.Converter
{
public class CustomEnumConverter<T> : JsonConverter<T> where T : struct, Enum
{
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string enumValue = reader.GetString();

foreach (var field in typeof(T).GetFields())
{
if (field.GetCustomAttribute<JsonPropertyNameAttribute>()?.Name == enumValue)
{
return (T)field.GetValue(null);
}
}

throw new JsonException($"Unknown enum value: {enumValue}");
}

public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
var field = typeof(T).GetField(value.ToString());

var attribute = field.GetCustomAttribute<JsonPropertyNameAttribute>();
string enumString = attribute.Name;

writer.WriteStringValue(enumString);
}
}

}
Loading