Skip to content

Commit ecd3acf

Browse files
committed
Refactored to include JsonSerializer.
1 parent 1887686 commit ecd3acf

17 files changed

Lines changed: 351 additions & 73 deletions

README.md

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,48 @@
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+
### JsonSerializer
29+
30+
``` c#
31+
services.AddJsonSerializer(builder => {
32+
builder.Options = new JsonSerializerOptions
33+
{
34+
};
35+
36+
// or
37+
38+
buidler.Options.option = value;
39+
});
40+
```
41+
42+
The `builder.Options` is of type [JsonSerializerOptions](https://docs.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializeroptions?view=net-6.0).
1043

1144
## Methods
1245

1346
### Serialize
1447

1548
``` c#
16-
Stream Serialize(object message);
1749
Task<Stream> SerializeAsync(object message);
1850
```
1951

@@ -22,17 +54,16 @@ Returns the message `object` as a `Stream`.
2254
### Deserialize
2355

2456
``` c#
25-
object Deserialize(Type type, Stream stream);
2657
Task<object> DeserializeAsync(Type type, Stream stream);
2758
```
2859

2960
Deserializes the `Stream` into an `object` of the given type.
3061

31-
# ISerializerRootType
62+
## ISerializerRootType
3263

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.
64+
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.
3465

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.
66+
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.
3667

3768
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:
3869

@@ -63,12 +94,12 @@ namespace Serializer
6394
}
6495
```
6596

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

68-
## AddSerializerType
99+
### AddSerializerType
69100

70101
``` c#
71102
void AddSerializerType(Type root, Type contained);
72103
```
73104

74-
Specify the `contained` tpe that is used within te `root` type somewhere.
105+
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
}
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+
}

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
}

Shuttle.Core.Serialization.Tests/DefaultSerializerFixture.cs renamed to Shuttle.Core.Serialization.Tests/XmlSerializerFixture.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
using System;
22
using System.IO;
33
using System.Threading.Tasks;
4+
using Microsoft.Extensions.Options;
45
using NUnit.Framework;
56

67
namespace Shuttle.Core.Serialization.Tests
78
{
8-
public class DefaultSerializerFixture
9+
public class XmlSerializerFixture
910
{
1011
[Test]
1112
public async Task Should_be_able_to_serialize_and_deserialize_a_simple_type_async()
1213
{
1314
var original = new SimpleSerializerType();
14-
var serializer = new DefaultSerializer();
15+
var serializer = new XmlSerializer(Options.Create(new XmlSerializerOptions()));
1516

1617
var stream = await serializer.SerializeAsync(original);
1718

@@ -28,7 +29,7 @@ public async Task Should_be_able_to_serialize_and_deserialize_a_simple_type_asyn
2829
public async Task Should_be_able_to_serialize_and_deserialize_a_complex_type_async()
2930
{
3031
var complex = new ComplexSerializerType();
31-
var serializer = new DefaultSerializer();
32+
var serializer = new XmlSerializer(Options.Create(new XmlSerializerOptions()));
3233

3334
serializer.AddSerializerType(typeof(ComplexSerializerType), typeof(v1.SomeSerializerType));
3435
serializer.AddSerializerType(typeof(ComplexSerializerType), typeof(v1.AnotherSerializerType));

Shuttle.Core.Serialization.Tests/v1/AnotherSerializerType.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ namespace Shuttle.Core.Serialization.Tests.v1
44
{
55
public class AnotherSerializerType
66
{
7-
public AnotherSerializerType()
8-
{
9-
Id = Guid.NewGuid();
10-
}
11-
12-
public Guid Id { get; set; }
7+
public Guid Id { get; set; } = Guid.NewGuid();
138
}
149
}

Shuttle.Core.Serialization.Tests/v1/SomeSerializerType.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@ namespace Shuttle.Core.Serialization.Tests.v1
44
{
55
public class SomeSerializerType
66
{
7-
public SomeSerializerType()
8-
{
9-
Id = Guid.NewGuid();
10-
AnotherSerializerType = new AnotherSerializerType();
11-
}
12-
13-
public Guid Id { get; set; }
14-
public AnotherSerializerType AnotherSerializerType { get; set; }
7+
public Guid Id { get; set; } = Guid.NewGuid();
8+
public AnotherSerializerType AnotherSerializerType { get; set; } = new();
159
}
1610
}

Shuttle.Core.Serialization.Tests/v2/AnotherSerializerType.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ namespace Shuttle.Core.Serialization.Tests.v2
44
{
55
public class AnotherSerializerType
66
{
7-
public AnotherSerializerType()
8-
{
9-
Id = Guid.NewGuid();
10-
}
11-
12-
public Guid Id { get; set; }
7+
public Guid Id { get; set; } = Guid.NewGuid();
138
}
149
}

Shuttle.Core.Serialization.Tests/v2/SomeSerializerType.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@ namespace Shuttle.Core.Serialization.Tests.v2
44
{
55
public class SomeSerializerType
66
{
7-
public SomeSerializerType()
8-
{
9-
Id = Guid.NewGuid();
10-
AnotherSerializerType = new AnotherSerializerType();
11-
}
12-
13-
public Guid Id { get; set; }
14-
public AnotherSerializerType AnotherSerializerType { get; set; }
7+
public Guid Id { get; set; } = Guid.NewGuid();
8+
public AnotherSerializerType AnotherSerializerType { get; set; } = new();
159
}
1610
}

Shuttle.Core.Serialization/.package/package.nuspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
<tags>shuttle serialization</tags>
1818
<dependencies>
1919
<dependency id="Shuttle.Core.Contract" version="20.0.0" />
20+
<dependency id="Microsoft.Extensions.DependencyInjection" version="8.0.1" />
21+
<dependency id="Microsoft.Extensions.Options" version="8.0.2" />
22+
<dependency id="System.Text.Json" version="8.0.5" />
2023
</dependencies>
2124
</metadata>
2225
<files>

0 commit comments

Comments
 (0)