Skip to content

Commit 081eff1

Browse files
committed
Update ObjectStorage with new constructor parameter
1 parent 0214b58 commit 081eff1

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

docs/helpers/ObjectSerializer.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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)

docs/helpers/ObjectStorage.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ The Object Storage Helper will help you handle storage of generic objects within
1818
> [!div class="nextstepaction"]
1919
> [Try it in the sample app](uwpct://Helpers?sample=Object%20Storage)
2020
21+
## Constructors
22+
23+
LocalObjectStorageHelper and RoamingObjectStorageHelper have the same constructor with an optional parameter:
24+
25+
| Constructor | Description |
26+
|-------------|-------------|
27+
| ObjectStorageHelper(IObjectSerializer = null) | Initializes a new instance of the ObjectStorageHelper class with the provided [IObjectSerializer](ObjectSerializer.md), that will be used when serializing and deserializing data in Settings or in Storage; if no serializer is provided, a default JSON serializer will be used. |
28+
2129
## Properties
2230

2331
| Property | Type | Description |
@@ -219,3 +227,4 @@ Await helper.SaveFileAsync(keySimpleObject, o)
219227

220228
* [LocalObjectStorageHelper source code](https://github.com/Microsoft/WindowsCommunityToolkit//blob/master/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/LocalObjectStorageHelper.cs)
221229
* [RoamingObjectStorageHelper source code](https://github.com/Microsoft/WindowsCommunityToolkit//blob/master/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/RoamingObjectStorageHelper.cs)
230+
* [IObjectSerializer source code](https://github.com/windows-toolkit/WindowsCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/IObjectSerializer.cs)

docs/toc.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
## [IncrementalLoadingCollection](helpers/IncrementalLoadingCollection.md)
141141
## [NetworkHelper](helpers/NetworkHelper.md)
142142
## [ObjectStorage](helpers/ObjectStorage.md)
143+
## [ObjectSerializer](helpers/ObjectSerializer.md)
143144
## [PrintHelper](helpers/PrintHelper.md)
144145
## [RemoteDeviceHelper](helpers/RemoteDeviceHelper.md)
145146
## [StorageFileHelper](helpers/StorageFiles.md)

0 commit comments

Comments
 (0)