|
1 | 1 | # Shuttle.Core.Serialization |
2 | 2 |
|
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. |
11 | 4 |
|
12 | | -## Usage |
| 5 | +## Installation |
13 | 6 |
|
14 | | -### XmlSerializer |
| 7 | +```bash |
| 8 | +dotnet add package Shuttle.Core.Serialization |
| 9 | +``` |
15 | 10 |
|
16 | | -``` c# |
17 | | -services.AddXmlSerializer(builder => { |
18 | | - builder.Options = new XmlSerializerOptions |
19 | | - { |
20 | | - }; |
| 11 | +## `ISerializer` interface |
21 | 12 |
|
22 | | - // or |
| 13 | +The core of the library is the `ISerializer` interface: |
23 | 14 |
|
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 | +} |
26 | 22 | ``` |
27 | 23 |
|
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. |
29 | 28 |
|
30 | | -### JsonSerializer |
| 29 | +## Usage |
31 | 30 |
|
32 | | -``` c# |
33 | | -services.AddJsonSerializer(builder => { |
34 | | - builder.Options = new JsonSerializerOptions |
35 | | - { |
36 | | - }; |
| 31 | +### `AddJsonSerializer` |
37 | 32 |
|
38 | | - // or |
| 33 | +To register the `JsonSerializer`, use the `AddJsonSerializer` extension method on `IServiceCollection`: |
39 | 34 |
|
40 | | - buidler.Options.option = value; |
| 35 | +```csharp |
| 36 | +services.AddJsonSerializer(options => |
| 37 | +{ |
| 38 | + options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; |
41 | 39 | }); |
42 | 40 | ``` |
43 | 41 |
|
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). |
45 | 43 |
|
46 | | -## Methods |
| 44 | +### `ISerializerService` |
47 | 45 |
|
48 | | -### Serialize |
| 46 | +The `ISerializerService` can be used to manage multiple serializers: |
49 | 47 |
|
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 | +} |
52 | 56 | ``` |
53 | 57 |
|
54 | | -Returns the message `object` as a `Stream`. |
| 58 | +You can use the `SerializerService` implementation to store and retrieve serializers by their `Name`: |
55 | 59 |
|
56 | | -### Deserialize |
| 60 | +```csharp |
| 61 | +var serializerService = new SerializerService(); |
57 | 62 |
|
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()))); |
63 | 64 |
|
64 | | -## ISerializerRootType |
| 65 | +if (serializerService.Contains("Json")) |
| 66 | +{ |
| 67 | + var serializer = serializerService.Get("Json"); |
| 68 | +} |
| 69 | +``` |
65 | 70 |
|
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 |
67 | 72 |
|
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` |
69 | 74 |
|
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 | +``` |
71 | 78 |
|
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`. |
80 | 80 |
|
81 | | -namespace Serializer.v2 |
82 | | -{ |
83 | | - public class MovedEvent |
84 | | - { |
85 | | - public string Where { get; set; } |
86 | | - } |
87 | | -} |
| 81 | +### `DeserializeAsync` |
88 | 82 |
|
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); |
97 | 85 | ``` |
98 | 86 |
|
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`. |
100 | 88 |
|
101 | | -### AddSerializerType |
| 89 | +### `DeserializeAsync<T>` (Extension method) |
102 | 90 |
|
103 | | -``` c# |
104 | | -void AddSerializerType(Type root, Type contained); |
| 91 | +```csharp |
| 92 | +Task<T> DeserializeAsync<T>(Stream stream, CancellationToken cancellationToken = default); |
105 | 93 | ``` |
106 | 94 |
|
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 | + |
0 commit comments