Skip to content

Commit 5b569bb

Browse files
authored
Merge pull request #5 from Shuttle/v20
V20
2 parents 356d151 + 8c6d745 commit 5b569bb

30 files changed

+591
-452
lines changed

README.md

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,50 @@
44
PM> Install-Package Shuttle.Core.Serialization
55
```
66

7-
An implementation of the `ISerializer` interface is used to serialize objects into a `Stream`.
7+
The following implementations of the `ISerializer` interface is used to serialize objects into a `Stream`:
88

9-
The `DefaultSerializer` makes use of the standard .NET xml serialization functionality.
9+
- `XmlSerializer` makes use of the standard .NET XML serialization functionality.
10+
- `JsonSerializer` makes use of the `System.Test.Json` serialization functionality.
11+
12+
## Usage
13+
14+
### XmlSerializer
15+
16+
``` c#
17+
services.AddXmlSerializer(builder => {
18+
builder.Options = new XmlSerializerOptions
19+
{
20+
};
21+
22+
// or
23+
24+
buidler.Options.option = value;
25+
});
26+
```
27+
28+
The `builder.Options` contains properties that map to [XmlWriterSettings](https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlwritersettings?view=net-8.0) as well as [XmlDictionaryReaderQuotas](https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmldictionaryreaderquotas?view=net-8.0).
29+
30+
### JsonSerializer
31+
32+
``` c#
33+
services.AddJsonSerializer(builder => {
34+
builder.Options = new JsonSerializerOptions
35+
{
36+
};
37+
38+
// or
39+
40+
buidler.Options.option = value;
41+
});
42+
```
43+
44+
The `builder.Options` is of type [JsonSerializerOptions](https://docs.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializeroptions?view=net-6.0).
1045

1146
## Methods
1247

1348
### Serialize
1449

1550
``` c#
16-
Stream Serialize(object message);
1751
Task<Stream> SerializeAsync(object message);
1852
```
1953

@@ -22,17 +56,16 @@ Returns the message `object` as a `Stream`.
2256
### Deserialize
2357

2458
``` c#
25-
object Deserialize(Type type, Stream stream);
2659
Task<object> DeserializeAsync(Type type, Stream stream);
2760
```
2861

2962
Deserializes the `Stream` into an `object` of the given type.
3063

31-
# ISerializerRootType
64+
## ISerializerRootType
3265

33-
The `ISerializerRootType` interface is an optional interface that serializer implementations can use that allows the developer to specify explicit object types contained within a root type.
66+
The `XmlSerializer` implements the `ISerializerRootType` interface which is an optional interface that serializer implementations can use that allows the developer to specify explicit object types contained within a root type.
3467

35-
The `DefaultSerializer` implements this interface and it is recommended that you explicitly register types with the same name, but in different namespaes, that will be serialized within the same root type to avoid any conflicts later down the line.
68+
It is recommended that you explicitly register types with the same name, but in different namespaes, that will be serialized within the same root type to avoid any conflicts later down the line.
3669

3770
For instance, the following two types will cause issues when used in the root `Complex` type as they both serialize to the same name and the .Net serializer cannot seem to distinguish the difference:
3871

@@ -63,12 +96,12 @@ namespace Serializer
6396
}
6497
```
6598

66-
By explicitly specifying the types the `DefaultSerializer` will add a namespace that will cause the types to be correctly identified.
99+
By explicitly specifying the types the `XmlSerializer` will add a namespace that will cause the types to be correctly identified.
67100

68-
## AddSerializerType
101+
### AddSerializerType
69102

70103
``` c#
71104
void AddSerializerType(Type root, Type contained);
72105
```
73106

74-
Specify the `contained` tpe that is used within te `root` type somewhere.
107+
Specify the `contained` type that is used within the `root` type.
Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
using System;
2+
using Shuttle.Core.Serialization.Tests.v1;
23

3-
namespace Shuttle.Core.Serialization.Tests
4+
namespace Shuttle.Core.Serialization.Tests;
5+
6+
public class ComplexSerializerType
47
{
5-
public class ComplexSerializerType
6-
{
7-
public ComplexSerializerType()
8-
{
9-
Id = Guid.NewGuid();
10-
SomeSerializerType1 = new v1.SomeSerializerType();
11-
AnotherSerializerType1 = new v1.AnotherSerializerType();
12-
SomeSerializerType2 = new v2.SomeSerializerType();
13-
AnotherSerializerType2 = new v2.AnotherSerializerType();
14-
}
8+
public AnotherSerializerType AnotherSerializerType1 { get; set; } = new();
9+
public v2.AnotherSerializerType AnotherSerializerType2 { get; set; } = new();
1510

16-
public Guid Id { get; set; }
17-
public v1.SomeSerializerType SomeSerializerType1 { get; set; }
18-
public v1.AnotherSerializerType AnotherSerializerType1 { get; set; }
19-
public v2.SomeSerializerType SomeSerializerType2 { get; set; }
20-
public v2.AnotherSerializerType AnotherSerializerType2 { get; set; }
21-
}
11+
public Guid Id { get; set; } = Guid.NewGuid();
12+
public SomeSerializerType SomeSerializerType1 { get; set; } = new();
13+
public v2.SomeSerializerType SomeSerializerType2 { get; set; } = new();
2214
}

Shuttle.Core.Serialization.Tests/DefaultSerializerFixture.cs

Lines changed: 0 additions & 102 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.IO;
3+
using System.Text.Json;
4+
using System.Threading.Tasks;
5+
using Microsoft.Extensions.Options;
6+
using NUnit.Framework;
7+
8+
namespace Shuttle.Core.Serialization.Tests
9+
{
10+
public class JsonSerializerFixture
11+
{
12+
[Test]
13+
public async Task Should_be_able_to_serialize_and_deserialize_a_simple_type_async()
14+
{
15+
var original = new SimpleSerializerType();
16+
var serializer = new JsonSerializer(Options.Create(new JsonSerializerOptions()));
17+
18+
var stream = await serializer.SerializeAsync(original);
19+
20+
stream.Position = 0;
21+
22+
var json = await new StreamReader(stream).ReadToEndAsync();
23+
24+
Assert.That(json.Contains(original.Id.ToString()), Is.True);
25+
26+
stream.Position = 0;
27+
28+
Assert.That(((SimpleSerializerType)await serializer.DeserializeAsync(typeof(SimpleSerializerType), stream)).Id, Is.EqualTo(original.Id));
29+
}
30+
31+
[Test]
32+
public async Task Should_be_able_to_serialize_and_deserialize_a_complex_type_async()
33+
{
34+
var complex = new ComplexSerializerType();
35+
var serializer = new JsonSerializer(Options.Create(new JsonSerializerOptions()));
36+
37+
var stream = await serializer.SerializeAsync(complex);
38+
39+
stream.Position = 0;
40+
41+
var json = await new StreamReader(stream).ReadToEndAsync();
42+
43+
Assert.That(json.Contains(complex.Id.ToString()), Is.True);
44+
45+
stream.Position = 0;
46+
47+
Assert.That(((ComplexSerializerType)await serializer.DeserializeAsync(typeof(ComplexSerializerType), stream)).Id, Is.EqualTo(complex.Id));
48+
49+
Console.WriteLine(json);
50+
51+
var some1 = new v1.SomeSerializerType();
52+
var some2 = new v2.SomeSerializerType();
53+
54+
var some1Serialized = await serializer.SerializeAsync(some1);
55+
var some2Serialized = await serializer.SerializeAsync(some2);
56+
57+
some1Serialized.Position = 0;
58+
some2Serialized.Position = 0;
59+
60+
Assert.That(((v1.SomeSerializerType)await serializer.DeserializeAsync(typeof(v1.SomeSerializerType), some1Serialized)).Id, Is.EqualTo(some1.Id));
61+
Assert.That(((v2.SomeSerializerType)await serializer.DeserializeAsync(typeof(v2.SomeSerializerType), some2Serialized)).Id, Is.EqualTo(some2.Id));
62+
}
63+
}
64+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net6.0</TargetFrameworks>
4+
<TargetFrameworks>net8.0</TargetFrameworks>
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
9-
<PackageReference Include="NUnit" Version="3.14.0" />
10-
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
8+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
9+
<PackageReference Include="NUnit" Version="4.2.2" />
10+
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
1111
</ItemGroup>
1212

1313
<ItemGroup>
1414
<ProjectReference Include="..\Shuttle.Core.Serialization\Shuttle.Core.Serialization.csproj" />
1515
</ItemGroup>
1616

17-
</Project>
17+
</Project>

Shuttle.Core.Serialization.Tests/SimpleSerializerType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ namespace Shuttle.Core.Serialization.Tests
44
{
55
public class SimpleSerializerType
66
{
7-
public Guid Id { get; set; }
7+
public Guid Id { get; set; } = Guid.NewGuid();
88
}
99
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading.Tasks;
4+
using Microsoft.Extensions.Options;
5+
using NUnit.Framework;
6+
7+
namespace Shuttle.Core.Serialization.Tests
8+
{
9+
public class XmlSerializerFixture
10+
{
11+
[Test]
12+
public async Task Should_be_able_to_serialize_and_deserialize_a_simple_type_async()
13+
{
14+
var original = new SimpleSerializerType();
15+
var serializer = new XmlSerializer(Options.Create(new XmlSerializerOptions()));
16+
17+
var stream = await serializer.SerializeAsync(original);
18+
19+
var xml = await new StreamReader(stream).ReadToEndAsync();
20+
21+
Assert.That(xml.Contains(original.Id.ToString()), Is.True);
22+
23+
stream.Position = 0;
24+
25+
Assert.That(((SimpleSerializerType)await serializer.DeserializeAsync(typeof(SimpleSerializerType), stream)).Id, Is.EqualTo(original.Id));
26+
}
27+
28+
[Test]
29+
public async Task Should_be_able_to_serialize_and_deserialize_a_complex_type_async()
30+
{
31+
var complex = new ComplexSerializerType();
32+
var serializer = new XmlSerializer(Options.Create(new XmlSerializerOptions()));
33+
34+
serializer.AddSerializerType(typeof(ComplexSerializerType), typeof(v1.SomeSerializerType));
35+
serializer.AddSerializerType(typeof(ComplexSerializerType), typeof(v1.AnotherSerializerType));
36+
serializer.AddSerializerType(typeof(ComplexSerializerType), typeof(v2.SomeSerializerType));
37+
serializer.AddSerializerType(typeof(ComplexSerializerType), typeof(v2.AnotherSerializerType));
38+
39+
var stream = await serializer.SerializeAsync(complex);
40+
var xml = await new StreamReader(stream).ReadToEndAsync();
41+
42+
Assert.That(xml.Contains(complex.Id.ToString()), Is.True);
43+
44+
stream.Position = 0;
45+
46+
Assert.That(((ComplexSerializerType)await serializer.DeserializeAsync(typeof(ComplexSerializerType), stream)).Id, Is.EqualTo(complex.Id));
47+
48+
Console.WriteLine(xml);
49+
50+
var some1 = new v1.SomeSerializerType();
51+
var some2 = new v2.SomeSerializerType();
52+
53+
Assert.That(((v1.SomeSerializerType)await serializer.DeserializeAsync(typeof(v1.SomeSerializerType), await serializer.SerializeAsync(some1))).Id, Is.EqualTo(some1.Id));
54+
Assert.That(((v2.SomeSerializerType)await serializer.DeserializeAsync(typeof(v2.SomeSerializerType), await serializer.SerializeAsync(some2))).Id, Is.EqualTo(some2.Id));
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)