Skip to content

Commit

Permalink
Add Serializable Attribute
Browse files Browse the repository at this point in the history
Add JsonSerializer
Bump v0.0.2
  • Loading branch information
patrick-dmxc committed May 31, 2024
1 parent 0ec968d commit f8d8ff5
Show file tree
Hide file tree
Showing 20 changed files with 290 additions and 50 deletions.
12 changes: 12 additions & 0 deletions WellKnownDataTypes-Tests/Light/ArtNet/Address_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using org.dmxc.wkdt.Light.ArtNet;
using org.dmxc.wkdt.Tests;

namespace org.dmxc.wkdt.Tests.Light.ArtNet
{
Expand Down Expand Up @@ -59,5 +60,16 @@ public void TestAddress()

Assert.That(addresses.OrderByDescending(s => s.Universe).ThenBy(s => s).ToArray(), Has.Length.EqualTo(byte.MaxValue));
}

[Test]
public void TestSerializable()
{
Address address = new Address(2, 3);
var data = Tools.Serialize(address);
string json= System.Text.Encoding.Default.GetString(data);
Address result = Tools.Deserialize<Address>(data);

Assert.That(result, Is.EqualTo(address), json);
}
}
}
11 changes: 11 additions & 0 deletions WellKnownDataTypes-Tests/Light/ArtNet/Net_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,16 @@ public void TestNet()
Assert.That(new Net(1).Equals((Net)2), Is.False);
});
}

[Test]
public void TestSerializable()
{
Net net = new Net(2);
var data = Tools.Serialize(net);
string json = System.Text.Encoding.Default.GetString(data);
Net result = Tools.Deserialize<Net>(data);

Assert.That(result, Is.EqualTo(net), json);
}
}
}
11 changes: 11 additions & 0 deletions WellKnownDataTypes-Tests/Light/ArtNet/PortAddress_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,16 @@ public void TestPortAddress()
Assert.That(new PortAddress(1).Equals((PortAddress)2), Is.False);
});
}

[Test]
public void TestSerializable()
{
PortAddress portAddress = new PortAddress(2,3,4);
var data = Tools.Serialize(portAddress);
string json = System.Text.Encoding.Default.GetString(data);
PortAddress result = Tools.Deserialize<PortAddress>(data);

Assert.That(result, Is.EqualTo(portAddress), json);
}
}
}
10 changes: 10 additions & 0 deletions WellKnownDataTypes-Tests/Light/ArtNet/Subnet_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,15 @@ public void TestSubnet()
Assert.That(new Subnet(0).Equals(Subnet.Default), Is.True);
});
}
[Test]
public void TestSerializable()
{
Subnet subnet = new Subnet(13);
var data = Tools.Serialize(subnet);
string json = System.Text.Encoding.Default.GetString(data);
Subnet result = Tools.Deserialize<Subnet>(data);

Assert.That(result, Is.EqualTo(subnet), json);
}
}
}
10 changes: 10 additions & 0 deletions WellKnownDataTypes-Tests/Light/ArtNet/Universe_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,15 @@ public void TestUniverse()
Assert.That(new Universe(0).Equals(Universe.Default), Is.True);
});
}
[Test]
public void TestSerializable()
{
Universe universe = new Universe(13);
var data = Tools.Serialize(universe);
string json = System.Text.Encoding.Default.GetString(data);
Universe result = Tools.Deserialize<Universe>(data);

Assert.That(result, Is.EqualTo(universe), json);
}
}
}
10 changes: 10 additions & 0 deletions WellKnownDataTypes-Tests/Light/RDM/UID_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,15 @@ public void TestUID()
Assert.That(new UID(0).Equals(UID.Empty), Is.True);
});
}
[Test]
public void TestSerializable()
{
UID uid = new UID("1234:12345678");
var data = Tools.Serialize(uid);
string json = System.Text.Encoding.Default.GetString(data);
UID result = Tools.Deserialize<UID>(data);

Assert.That(result, Is.EqualTo(uid), json);
}
}
}
11 changes: 11 additions & 0 deletions WellKnownDataTypes-Tests/Network/IPv4Address_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,16 @@ public void TestIPv4Address()
Assert.Throws(typeof(ArgumentException), () => { var ip = (IPv4Address)System.Net.IPAddress.IPv6Any; });
});
}

[Test]
public void TestSerializable()
{
IPv4Address ipv4Address = new IPv4Address("192.168.178.33");
var data = Tools.Serialize(ipv4Address);
string json = System.Text.Encoding.Default.GetString(data);
IPv4Address result = Tools.Deserialize<IPv4Address>(data);

Assert.That(result, Is.EqualTo(ipv4Address), json);
}
}
}
11 changes: 11 additions & 0 deletions WellKnownDataTypes-Tests/Network/MACAddress_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,16 @@ public void TestMACAddress()
}
});
}

[Test]
public void TestSerializable()
{
MACAddress macAddress = new MACAddress("aa:bb:cc:dD:Ee:fF");
var data = Tools.Serialize(macAddress);
string json = System.Text.Encoding.Default.GetString(data);
MACAddress result = Tools.Deserialize<MACAddress>(data);

Assert.That(result, Is.EqualTo(macAddress), json);
}
}
}
49 changes: 49 additions & 0 deletions WellKnownDataTypes-Tests/Tools.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace org.dmxc.wkdt.Tests
{
public static class Tools
{
public static byte[] Serialize(object value)
{
#if !NET8_0_OR_GREATER
byte[]? data = null;
using (MemoryStream ms = new MemoryStream())
{
//Format the object as Binary

BinaryFormatter formatter = new BinaryFormatter();
//It serialize the employee object
#pragma warning disable SYSLIB0011 // Typ oder Element ist veraltet
formatter.Serialize(ms, value);
#pragma warning restore SYSLIB0011 // Typ oder Element ist veraltet
data = ms.GetBuffer();

ms.Close();
}
return data;
#else
return JsonSerializer.SerializeToUtf8Bytes(value);
#endif
}
public static T? Deserialize<T>(byte[] data)
{

#if !NET8_0_OR_GREATER
using (MemoryStream ms = new MemoryStream(data))
{
BinaryFormatter formatter = new BinaryFormatter();
#pragma warning disable SYSLIB0011 // Typ oder Element ist veraltet
return (T)formatter.Deserialize(ms);
#pragma warning restore SYSLIB0011 // Typ oder Element ist veraltet
}
#else
return JsonSerializer.Deserialize<T>(new ReadOnlySpan<byte>(data));
#endif
}
}
}
17 changes: 3 additions & 14 deletions WellKnownDataTypes-Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
global using NUnit.Framework;

/* Nicht gemergte Änderung aus Projekt "WellKnownDataTypes-Tests (net7.0)"
Vor:
global using org.dmxc.wkdt;
Nach:
global using System;
*/

/* Nicht gemergte Änderung aus Projekt "WellKnownDataTypes-Tests (net8.0)"
Vor:
global using org.dmxc.wkdt;
Nach:
global using System;
*/
global using System.Buffers.Text;
global using System.Runtime.Serialization.Formatters.Binary;
global using System.Text.Json;
21 changes: 17 additions & 4 deletions WellKnownDataTypes/Light/ArtNet/Address.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
using System;

namespace org.dmxc.wkdt.Light.ArtNet
namespace org.dmxc.wkdt.Light.ArtNet
{
[Serializable]
public readonly struct Address : IEquatable<Address>, IComparable<Address>
{
#if NET8_0_OR_GREATER
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
#endif
public readonly Subnet Subnet;
#if NET8_0_OR_GREATER
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
#endif
public readonly Universe Universe;

#if NET8_0_OR_GREATER
[JsonInclude]
#endif
public readonly byte Combined;

public Address(in Subnet subnet, in Universe universe)
Expand All @@ -14,7 +23,11 @@ public Address(in Subnet subnet, in Universe universe)
Universe = universe;
Combined = (byte)((Subnet & 0xf) << 4 | (Universe & 0xf));
}
public Address(in byte combined)

#if NET8_0_OR_GREATER
[JsonConstructor]
#endif
public Address(byte combined)
{
Subnet = (Subnet)((combined >> 4) & 0xf);
Universe = (Universe)(combined & 0xf);
Expand Down
14 changes: 10 additions & 4 deletions WellKnownDataTypes/Light/ArtNet/Net.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
using System;

namespace org.dmxc.wkdt.Light.ArtNet
namespace org.dmxc.wkdt.Light.ArtNet
{
[Serializable]
public readonly struct Net : IEquatable<Net>, IComparable<Net>
{
#if NET8_0_OR_GREATER
[JsonInclude]
#endif
public readonly byte Value;

public Net(in byte value)

#if NET8_0_OR_GREATER
[JsonConstructor]
#endif
public Net(byte value)
{
if ((byte)(value & 0x7f) != value)
throw new ArgumentException($"Value (0x{value:x}) out of range! A valid value is between 0x00 and 0x7f.");
Expand Down
27 changes: 23 additions & 4 deletions WellKnownDataTypes/Light/ArtNet/PortAddress.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
using System;

namespace org.dmxc.wkdt.Light.ArtNet
namespace org.dmxc.wkdt.Light.ArtNet
{
[Serializable]
public readonly struct PortAddress : IEquatable<PortAddress>, IComparable<PortAddress>
{
#if NET8_0_OR_GREATER
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
#endif
public readonly Net Net;
#if NET8_0_OR_GREATER
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
#endif
public readonly Subnet Subnet;
#if NET8_0_OR_GREATER
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
#endif
public readonly Universe Universe;
#if NET8_0_OR_GREATER
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
#endif
public readonly Address Address;

#if NET8_0_OR_GREATER
[JsonInclude]
#endif
public readonly ushort Combined;

public PortAddress(in Net net, in Subnet subnet, in Universe universe)
Expand All @@ -18,7 +33,11 @@ public PortAddress(in Net net, in Subnet subnet, in Universe universe)
Address = new Address(subnet, universe);
Combined = (ushort)((Net << 8) + Address.Combined);
}
public PortAddress(in ushort combined)

#if NET8_0_OR_GREATER
[JsonConstructor]
#endif
public PortAddress(ushort combined)
{
if ((ushort)(combined & 0x7fff) != combined)
throw new ArgumentException($"Value (0x{combined:x}) out of range! A valid value is between 0x0000 and 0x7fff.");
Expand Down
14 changes: 10 additions & 4 deletions WellKnownDataTypes/Light/ArtNet/Subnet.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
using System;

namespace org.dmxc.wkdt.Light.ArtNet
namespace org.dmxc.wkdt.Light.ArtNet
{
[Serializable]
public readonly struct Subnet : IEquatable<Subnet>, IComparable<Subnet>
{
public static readonly Subnet Default = new Subnet();

#if NET8_0_OR_GREATER
[JsonInclude]
#endif
public readonly byte Value;

public Subnet(in byte value)
#if NET8_0_OR_GREATER
[JsonConstructor]
#endif
public Subnet(byte value)
{
if ((byte)(value & 0x0f) != value)
throw new ArgumentException($"Value (0x{value:x}) out of range! A valid value is between 0x00 and 0x0f.");
Expand Down
14 changes: 10 additions & 4 deletions WellKnownDataTypes/Light/ArtNet/Universe.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
using System;

namespace org.dmxc.wkdt.Light.ArtNet
namespace org.dmxc.wkdt.Light.ArtNet
{
[Serializable]
public readonly struct Universe : IEquatable<Universe>, IComparable<Universe>
{
public static readonly Universe Default = new Universe();

#if NET8_0_OR_GREATER
[JsonInclude]
#endif
public readonly byte Value;

public Universe(in byte value)
#if NET8_0_OR_GREATER
[JsonConstructor]
#endif
public Universe(byte value)
{
if ((byte)(value & 0x0f) != value)
throw new ArgumentException($"Value (0x{value:x}) out of range! A valid value is between 0x00 and 0x0f.");
Expand Down
Loading

0 comments on commit f8d8ff5

Please sign in to comment.