Skip to content

Commit f8d8ff5

Browse files
committed
Add Serializable Attribute
Add JsonSerializer Bump v0.0.2
1 parent 0ec968d commit f8d8ff5

20 files changed

+290
-50
lines changed

Diff for: WellKnownDataTypes-Tests/Light/ArtNet/Address_Tests.cs

+12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using org.dmxc.wkdt.Light.ArtNet;
2+
using org.dmxc.wkdt.Tests;
23

34
namespace org.dmxc.wkdt.Tests.Light.ArtNet
45
{
@@ -59,5 +60,16 @@ public void TestAddress()
5960

6061
Assert.That(addresses.OrderByDescending(s => s.Universe).ThenBy(s => s).ToArray(), Has.Length.EqualTo(byte.MaxValue));
6162
}
63+
64+
[Test]
65+
public void TestSerializable()
66+
{
67+
Address address = new Address(2, 3);
68+
var data = Tools.Serialize(address);
69+
string json= System.Text.Encoding.Default.GetString(data);
70+
Address result = Tools.Deserialize<Address>(data);
71+
72+
Assert.That(result, Is.EqualTo(address), json);
73+
}
6274
}
6375
}

Diff for: WellKnownDataTypes-Tests/Light/ArtNet/Net_Tests.cs

+11
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,16 @@ public void TestNet()
4848
Assert.That(new Net(1).Equals((Net)2), Is.False);
4949
});
5050
}
51+
52+
[Test]
53+
public void TestSerializable()
54+
{
55+
Net net = new Net(2);
56+
var data = Tools.Serialize(net);
57+
string json = System.Text.Encoding.Default.GetString(data);
58+
Net result = Tools.Deserialize<Net>(data);
59+
60+
Assert.That(result, Is.EqualTo(net), json);
61+
}
5162
}
5263
}

Diff for: WellKnownDataTypes-Tests/Light/ArtNet/PortAddress_Tests.cs

+11
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,16 @@ public void TestPortAddress()
5252
Assert.That(new PortAddress(1).Equals((PortAddress)2), Is.False);
5353
});
5454
}
55+
56+
[Test]
57+
public void TestSerializable()
58+
{
59+
PortAddress portAddress = new PortAddress(2,3,4);
60+
var data = Tools.Serialize(portAddress);
61+
string json = System.Text.Encoding.Default.GetString(data);
62+
PortAddress result = Tools.Deserialize<PortAddress>(data);
63+
64+
Assert.That(result, Is.EqualTo(portAddress), json);
65+
}
5566
}
5667
}

Diff for: WellKnownDataTypes-Tests/Light/ArtNet/Subnet_Tests.cs

+10
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,15 @@ public void TestSubnet()
5050
Assert.That(new Subnet(0).Equals(Subnet.Default), Is.True);
5151
});
5252
}
53+
[Test]
54+
public void TestSerializable()
55+
{
56+
Subnet subnet = new Subnet(13);
57+
var data = Tools.Serialize(subnet);
58+
string json = System.Text.Encoding.Default.GetString(data);
59+
Subnet result = Tools.Deserialize<Subnet>(data);
60+
61+
Assert.That(result, Is.EqualTo(subnet), json);
62+
}
5363
}
5464
}

Diff for: WellKnownDataTypes-Tests/Light/ArtNet/Universe_Tests.cs

+10
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,15 @@ public void TestUniverse()
4949
Assert.That(new Universe(0).Equals(Universe.Default), Is.True);
5050
});
5151
}
52+
[Test]
53+
public void TestSerializable()
54+
{
55+
Universe universe = new Universe(13);
56+
var data = Tools.Serialize(universe);
57+
string json = System.Text.Encoding.Default.GetString(data);
58+
Universe result = Tools.Deserialize<Universe>(data);
59+
60+
Assert.That(result, Is.EqualTo(universe), json);
61+
}
5262
}
5363
}

Diff for: WellKnownDataTypes-Tests/Light/RDM/UID_Tests.cs

+10
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,15 @@ public void TestUID()
110110
Assert.That(new UID(0).Equals(UID.Empty), Is.True);
111111
});
112112
}
113+
[Test]
114+
public void TestSerializable()
115+
{
116+
UID uid = new UID("1234:12345678");
117+
var data = Tools.Serialize(uid);
118+
string json = System.Text.Encoding.Default.GetString(data);
119+
UID result = Tools.Deserialize<UID>(data);
120+
121+
Assert.That(result, Is.EqualTo(uid), json);
122+
}
113123
}
114124
}

Diff for: WellKnownDataTypes-Tests/Network/IPv4Address_Tests.cs

+11
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,16 @@ public void TestIPv4Address()
6161
Assert.Throws(typeof(ArgumentException), () => { var ip = (IPv4Address)System.Net.IPAddress.IPv6Any; });
6262
});
6363
}
64+
65+
[Test]
66+
public void TestSerializable()
67+
{
68+
IPv4Address ipv4Address = new IPv4Address("192.168.178.33");
69+
var data = Tools.Serialize(ipv4Address);
70+
string json = System.Text.Encoding.Default.GetString(data);
71+
IPv4Address result = Tools.Deserialize<IPv4Address>(data);
72+
73+
Assert.That(result, Is.EqualTo(ipv4Address), json);
74+
}
6475
}
6576
}

Diff for: WellKnownDataTypes-Tests/Network/MACAddress_Tests.cs

+11
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,16 @@ public void TestMACAddress()
5353
}
5454
});
5555
}
56+
57+
[Test]
58+
public void TestSerializable()
59+
{
60+
MACAddress macAddress = new MACAddress("aa:bb:cc:dD:Ee:fF");
61+
var data = Tools.Serialize(macAddress);
62+
string json = System.Text.Encoding.Default.GetString(data);
63+
MACAddress result = Tools.Deserialize<MACAddress>(data);
64+
65+
Assert.That(result, Is.EqualTo(macAddress), json);
66+
}
5667
}
5768
}

Diff for: WellKnownDataTypes-Tests/Tools.cs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace org.dmxc.wkdt.Tests
8+
{
9+
public static class Tools
10+
{
11+
public static byte[] Serialize(object value)
12+
{
13+
#if !NET8_0_OR_GREATER
14+
byte[]? data = null;
15+
using (MemoryStream ms = new MemoryStream())
16+
{
17+
//Format the object as Binary
18+
19+
BinaryFormatter formatter = new BinaryFormatter();
20+
//It serialize the employee object
21+
#pragma warning disable SYSLIB0011 // Typ oder Element ist veraltet
22+
formatter.Serialize(ms, value);
23+
#pragma warning restore SYSLIB0011 // Typ oder Element ist veraltet
24+
data = ms.GetBuffer();
25+
26+
ms.Close();
27+
}
28+
return data;
29+
#else
30+
return JsonSerializer.SerializeToUtf8Bytes(value);
31+
#endif
32+
}
33+
public static T? Deserialize<T>(byte[] data)
34+
{
35+
36+
#if !NET8_0_OR_GREATER
37+
using (MemoryStream ms = new MemoryStream(data))
38+
{
39+
BinaryFormatter formatter = new BinaryFormatter();
40+
#pragma warning disable SYSLIB0011 // Typ oder Element ist veraltet
41+
return (T)formatter.Deserialize(ms);
42+
#pragma warning restore SYSLIB0011 // Typ oder Element ist veraltet
43+
}
44+
#else
45+
return JsonSerializer.Deserialize<T>(new ReadOnlySpan<byte>(data));
46+
#endif
47+
}
48+
}
49+
}

Diff for: WellKnownDataTypes-Tests/Usings.cs

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
11
global using NUnit.Framework;
2-
3-
/* Nicht gemergte Änderung aus Projekt "WellKnownDataTypes-Tests (net7.0)"
4-
Vor:
5-
global using org.dmxc.wkdt;
6-
Nach:
7-
global using System;
8-
*/
9-
10-
/* Nicht gemergte Änderung aus Projekt "WellKnownDataTypes-Tests (net8.0)"
11-
Vor:
12-
global using org.dmxc.wkdt;
13-
Nach:
14-
global using System;
15-
*/
2+
global using System.Buffers.Text;
3+
global using System.Runtime.Serialization.Formatters.Binary;
4+
global using System.Text.Json;

Diff for: WellKnownDataTypes/Light/ArtNet/Address.cs

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
using System;
2-
3-
namespace org.dmxc.wkdt.Light.ArtNet
1+
namespace org.dmxc.wkdt.Light.ArtNet
42
{
3+
[Serializable]
54
public readonly struct Address : IEquatable<Address>, IComparable<Address>
65
{
6+
#if NET8_0_OR_GREATER
7+
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
8+
#endif
79
public readonly Subnet Subnet;
10+
#if NET8_0_OR_GREATER
11+
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
12+
#endif
813
public readonly Universe Universe;
14+
15+
#if NET8_0_OR_GREATER
16+
[JsonInclude]
17+
#endif
918
public readonly byte Combined;
1019

1120
public Address(in Subnet subnet, in Universe universe)
@@ -14,7 +23,11 @@ public Address(in Subnet subnet, in Universe universe)
1423
Universe = universe;
1524
Combined = (byte)((Subnet & 0xf) << 4 | (Universe & 0xf));
1625
}
17-
public Address(in byte combined)
26+
27+
#if NET8_0_OR_GREATER
28+
[JsonConstructor]
29+
#endif
30+
public Address(byte combined)
1831
{
1932
Subnet = (Subnet)((combined >> 4) & 0xf);
2033
Universe = (Universe)(combined & 0xf);

Diff for: WellKnownDataTypes/Light/ArtNet/Net.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
using System;
2-
3-
namespace org.dmxc.wkdt.Light.ArtNet
1+
namespace org.dmxc.wkdt.Light.ArtNet
42
{
3+
[Serializable]
54
public readonly struct Net : IEquatable<Net>, IComparable<Net>
65
{
6+
#if NET8_0_OR_GREATER
7+
[JsonInclude]
8+
#endif
79
public readonly byte Value;
810

9-
public Net(in byte value)
11+
12+
#if NET8_0_OR_GREATER
13+
[JsonConstructor]
14+
#endif
15+
public Net(byte value)
1016
{
1117
if ((byte)(value & 0x7f) != value)
1218
throw new ArgumentException($"Value (0x{value:x}) out of range! A valid value is between 0x00 and 0x7f.");

Diff for: WellKnownDataTypes/Light/ArtNet/PortAddress.cs

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1-
using System;
2-
3-
namespace org.dmxc.wkdt.Light.ArtNet
1+
namespace org.dmxc.wkdt.Light.ArtNet
42
{
3+
[Serializable]
54
public readonly struct PortAddress : IEquatable<PortAddress>, IComparable<PortAddress>
65
{
6+
#if NET8_0_OR_GREATER
7+
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
8+
#endif
79
public readonly Net Net;
10+
#if NET8_0_OR_GREATER
11+
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
12+
#endif
813
public readonly Subnet Subnet;
14+
#if NET8_0_OR_GREATER
15+
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
16+
#endif
917
public readonly Universe Universe;
18+
#if NET8_0_OR_GREATER
19+
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
20+
#endif
1021
public readonly Address Address;
22+
23+
#if NET8_0_OR_GREATER
24+
[JsonInclude]
25+
#endif
1126
public readonly ushort Combined;
1227

1328
public PortAddress(in Net net, in Subnet subnet, in Universe universe)
@@ -18,7 +33,11 @@ public PortAddress(in Net net, in Subnet subnet, in Universe universe)
1833
Address = new Address(subnet, universe);
1934
Combined = (ushort)((Net << 8) + Address.Combined);
2035
}
21-
public PortAddress(in ushort combined)
36+
37+
#if NET8_0_OR_GREATER
38+
[JsonConstructor]
39+
#endif
40+
public PortAddress(ushort combined)
2241
{
2342
if ((ushort)(combined & 0x7fff) != combined)
2443
throw new ArgumentException($"Value (0x{combined:x}) out of range! A valid value is between 0x0000 and 0x7fff.");

Diff for: WellKnownDataTypes/Light/ArtNet/Subnet.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
using System;
2-
3-
namespace org.dmxc.wkdt.Light.ArtNet
1+
namespace org.dmxc.wkdt.Light.ArtNet
42
{
3+
[Serializable]
54
public readonly struct Subnet : IEquatable<Subnet>, IComparable<Subnet>
65
{
76
public static readonly Subnet Default = new Subnet();
7+
8+
#if NET8_0_OR_GREATER
9+
[JsonInclude]
10+
#endif
811
public readonly byte Value;
912

10-
public Subnet(in byte value)
13+
#if NET8_0_OR_GREATER
14+
[JsonConstructor]
15+
#endif
16+
public Subnet(byte value)
1117
{
1218
if ((byte)(value & 0x0f) != value)
1319
throw new ArgumentException($"Value (0x{value:x}) out of range! A valid value is between 0x00 and 0x0f.");

Diff for: WellKnownDataTypes/Light/ArtNet/Universe.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
using System;
2-
3-
namespace org.dmxc.wkdt.Light.ArtNet
1+
namespace org.dmxc.wkdt.Light.ArtNet
42
{
3+
[Serializable]
54
public readonly struct Universe : IEquatable<Universe>, IComparable<Universe>
65
{
76
public static readonly Universe Default = new Universe();
7+
8+
#if NET8_0_OR_GREATER
9+
[JsonInclude]
10+
#endif
811
public readonly byte Value;
912

10-
public Universe(in byte value)
13+
#if NET8_0_OR_GREATER
14+
[JsonConstructor]
15+
#endif
16+
public Universe(byte value)
1117
{
1218
if ((byte)(value & 0x0f) != value)
1319
throw new ArgumentException($"Value (0x{value:x}) out of range! A valid value is between 0x00 and 0x0f.");

0 commit comments

Comments
 (0)