|
| 1 | +--- |
| 2 | +title: Object Serializer |
| 3 | +author: simop |
| 4 | +description: IObjectSerializer is an interface that you can implement to provide a serializer of your choice to ObjectStorageHelper. |
| 5 | +keywords: windows 10, uwp, windows community toolkit, uwp community toolkit, uwp toolkit, serialization |
| 6 | +dev_langs: |
| 7 | + - csharp |
| 8 | + - vb |
| 9 | +--- |
| 10 | + |
| 11 | +# ObjectSerializer |
| 12 | + |
| 13 | +You should implement IObjectSerializer when you need to write data using this toolkit's helpers with a custom serializer. If you don't, a default JSON serializer will be used otherwise. |
| 14 | + |
| 15 | +## Methods |
| 16 | + |
| 17 | +| Methods | Return Type | Description | |
| 18 | +|---------|-------------|-------------| |
| 19 | +| Serialize<T>(T) | string | Serialize an object of type T into a string. | |
| 20 | +| Deserialize<T>(string) | T | Deserialize a string to an object of type T. | |
| 21 | + |
| 22 | +## Examples |
| 23 | + |
| 24 | +### Json.NET |
| 25 | + |
| 26 | +```csharp |
| 27 | +using Microsoft.Toolkit.Uwp.Helpers; |
| 28 | +using Newtonsoft.Json; |
| 29 | + |
| 30 | +namespace Contoso.Helpers |
| 31 | +{ |
| 32 | + public class JsonNetObjectSerializer : IObjectSerializer |
| 33 | + { |
| 34 | + // Specify your serialization settings |
| 35 | + private readonly JsonSerializerSettings settings = new JsonSerializerSettings(); |
| 36 | + |
| 37 | + public string Serialize<T>(T value) => JsonConvert.SerializeObject(value, typeof(T), Formatting.Indented, settings); |
| 38 | + |
| 39 | + public T Deserialize<T>(string value) => JsonConvert.DeserializeObject<T>(value, settings); |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +### DataContract |
| 45 | + |
| 46 | +```csharp |
| 47 | +using System.IO; |
| 48 | +using System.Runtime.Serialization; |
| 49 | +using System.Xml; |
| 50 | + |
| 51 | +namespace Contoso.Helpers |
| 52 | +{ |
| 53 | + public class DataContractObjectSerializer : IObjectSerializer |
| 54 | + { |
| 55 | + // Specify your serialization settings |
| 56 | + private readonly DataContractSerializerSettings settings = new DataContractSerializerSettings(); |
| 57 | + |
| 58 | + public string Serialize<T>(T value) |
| 59 | + { |
| 60 | + var serializer = new DataContractSerializer(typeof(T), settings); |
| 61 | + |
| 62 | + using (var stringWriter = new StringWriter()) |
| 63 | + using (var xmlWriter = XmlWriter.Create(stringWriter)) |
| 64 | + { |
| 65 | + serializer.WriteObject(xmlWriter, value); |
| 66 | + return stringWriter.ToString(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public T Deserialize<T>(string value) |
| 71 | + { |
| 72 | + var serializer = new DataContractSerializer(typeof(T), settings); |
| 73 | + |
| 74 | + using (var stringReader = new StringReader(value)) |
| 75 | + using (var xmlReader = XmlReader.Create(stringReader)) |
| 76 | + { |
| 77 | + return serializer.ReadObject(xmlReader) as T; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +## Requirements |
| 85 | + |
| 86 | +| Device family | Universal, 10.0.16299.0 or higher | |
| 87 | +| --- | --- | |
| 88 | +| Namespace | Microsoft.Toolkit.Uwp | |
| 89 | +| NuGet package | [Microsoft.Toolkit.Uwp](https://www.nuget.org/packages/Microsoft.Toolkit.Uwp/) | |
| 90 | + |
| 91 | +## API |
| 92 | + |
| 93 | +* [IObjectSerializer source code](https://github.com/windows-toolkit/WindowsCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/IObjectSerializer.cs) |
| 94 | + |
| 95 | +## Related topics |
| 96 | + |
| 97 | +* [ObjectStorage](https://docs.microsoft.com/en-us/windows/communitytoolkit/helpers/objectstorage) |
0 commit comments