Skip to content

Commit c7b89b8

Browse files
authored
Merge pull request #6 from Shuttle/v21
V21
2 parents 5b569bb + 108df60 commit c7b89b8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+366
-935
lines changed

README.md

Lines changed: 60 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,96 @@
11
# Shuttle.Core.Serialization
22

3-
```
4-
PM> Install-Package Shuttle.Core.Serialization
5-
```
6-
7-
The following implementations of the `ISerializer` interface is used to serialize objects into a `Stream`:
8-
9-
- `XmlSerializer` makes use of the standard .NET XML serialization functionality.
10-
- `JsonSerializer` makes use of the `System.Test.Json` serialization functionality.
3+
The `Shuttle.Core.Serialization` package provides a consistent interface for serializing and deserializing objects.
114

12-
## Usage
5+
## Installation
136

14-
### XmlSerializer
7+
```bash
8+
dotnet add package Shuttle.Core.Serialization
9+
```
1510

16-
``` c#
17-
services.AddXmlSerializer(builder => {
18-
builder.Options = new XmlSerializerOptions
19-
{
20-
};
11+
## `ISerializer` interface
2112

22-
// or
13+
The core of the library is the `ISerializer` interface:
2314

24-
buidler.Options.option = value;
25-
});
15+
```csharp
16+
public interface ISerializer
17+
{
18+
string Name { get; }
19+
Task<object> DeserializeAsync(Type type, Stream stream, CancellationToken cancellationToken = default);
20+
Task<Stream> SerializeAsync(object instance, CancellationToken cancellationToken = default);
21+
}
2622
```
2723

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).
24+
The following implementation is provided:
25+
26+
- `JsonSerializer`: uses `System.Text.Json` for serialization.
27+
- `SerializerService`: used to manage multiple `ISerializer` instances.
2928

30-
### JsonSerializer
29+
## Usage
3130

32-
``` c#
33-
services.AddJsonSerializer(builder => {
34-
builder.Options = new JsonSerializerOptions
35-
{
36-
};
31+
### `AddJsonSerializer`
3732

38-
// or
33+
To register the `JsonSerializer`, use the `AddJsonSerializer` extension method on `IServiceCollection`:
3934

40-
buidler.Options.option = value;
35+
```csharp
36+
services.AddJsonSerializer(options =>
37+
{
38+
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
4139
});
4240
```
4341

44-
The `builder.Options` is of type [JsonSerializerOptions](https://docs.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializeroptions?view=net-6.0).
42+
The `options` is of type [JsonSerializerOptions](https://docs.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializeroptions?view=net-6.0).
4543

46-
## Methods
44+
### `ISerializerService`
4745

48-
### Serialize
46+
The `ISerializerService` can be used to manage multiple serializers:
4947

50-
``` c#
51-
Task<Stream> SerializeAsync(object message);
48+
```csharp
49+
public interface ISerializerService
50+
{
51+
IEnumerable<ISerializer> Serializers { get; }
52+
ISerializerService Add(ISerializer serializer);
53+
bool Contains(string name);
54+
ISerializer Get(string name);
55+
}
5256
```
5357

54-
Returns the message `object` as a `Stream`.
58+
You can use the `SerializerService` implementation to store and retrieve serializers by their `Name`:
5559

56-
### Deserialize
60+
```csharp
61+
var serializerService = new SerializerService();
5762

58-
``` c#
59-
Task<object> DeserializeAsync(Type type, Stream stream);
60-
```
61-
62-
Deserializes the `Stream` into an `object` of the given type.
63+
serializerService.Add(new JsonSerializer(Options.Create(new JsonSerializerOptions())));
6364

64-
## ISerializerRootType
65+
if (serializerService.Contains("Json"))
66+
{
67+
var serializer = serializerService.Get("Json");
68+
}
69+
```
6570

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.
71+
## Methods
6772

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.
73+
### `SerializeAsync`
6974

70-
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:
75+
```csharp
76+
Task<Stream> SerializeAsync(object instance, CancellationToken cancellationToken = default);
77+
```
7178

72-
``` c#
73-
namespace Serializer.v1
74-
{
75-
public class MovedEvent
76-
{
77-
public string Where { get; set; }
78-
}
79-
}
79+
Returns the `object` as a `Stream`.
8080

81-
namespace Serializer.v2
82-
{
83-
public class MovedEvent
84-
{
85-
public string Where { get; set; }
86-
}
87-
}
81+
### `DeserializeAsync`
8882

89-
namespace Serializer
90-
{
91-
public class Complex
92-
{
93-
public v1.MovedEvent { get; set; }
94-
public v2.MovedEvent { get; set; }
95-
}
96-
}
83+
```csharp
84+
Task<object> DeserializeAsync(Type type, Stream stream, CancellationToken cancellationToken = default);
9785
```
9886

99-
By explicitly specifying the types the `XmlSerializer` will add a namespace that will cause the types to be correctly identified.
87+
Deserializes the `Stream` into an `object` of the given `Type`.
10088

101-
### AddSerializerType
89+
### `DeserializeAsync<T>` (Extension method)
10290

103-
``` c#
104-
void AddSerializerType(Type root, Type contained);
91+
```csharp
92+
Task<T> DeserializeAsync<T>(Stream stream, CancellationToken cancellationToken = default);
10593
```
10694

107-
Specify the `contained` type that is used within the `root` type.
95+
Deserializes the `Stream` into an `object` of the given type `T`.
96+

Shuttle.Core.Serialization.Tests/JsonSerializerFixture.cs

Lines changed: 0 additions & 64 deletions
This file was deleted.

Shuttle.Core.Serialization.Tests/Shuttle.Core.Serialization.Tests.csproj

Lines changed: 0 additions & 17 deletions
This file was deleted.

Shuttle.Core.Serialization.Tests/SimpleSerializerType.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

Shuttle.Core.Serialization.Tests/XmlSerializerFixture.cs

Lines changed: 0 additions & 57 deletions
This file was deleted.

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

Lines changed: 0 additions & 9 deletions
This file was deleted.

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

Lines changed: 0 additions & 10 deletions
This file was deleted.

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

Lines changed: 0 additions & 9 deletions
This file was deleted.

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

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)