Skip to content

Commit 63ef5c1

Browse files
authored
README: Add examples on json serialization
1 parent 01078d9 commit 63ef5c1

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,32 @@ For more details, see [Precision](https://github.com/angularsen/UnitsNet/wiki/Pr
288288

289289
* `UnitsNet.Serialization.JsonNet` ([nuget](https://www.nuget.org/packages/UnitsNet.Serialization.JsonNet), [src](https://github.com/angularsen/UnitsNet/tree/master/UnitsNet.Serialization.JsonNet), [tests](https://github.com/angularsen/UnitsNet/tree/master/UnitsNet.Serialization.JsonNet.Tests)) for JSON.NET
290290
291+
#### Example of JSON Serialization
292+
```c#
293+
var jsonSerializerSettings = new JsonSerializerSettings {Formatting = Formatting.Indented};
294+
jsonSerializerSettings.Converters.Add(new UnitsNetIQuantityJsonConverter());
295+
296+
string json = JsonConvert.SerializeObject(new { Name = "Raiden", Weight = Mass.FromKilograms(90) });
297+
298+
object obj = JsonConvert.DeserializeObject(json);
299+
```
300+
301+
JSON output:
302+
```json
303+
{
304+
"Name": "Raiden",
305+
"Weight": {
306+
"Unit": "MassUnit.Kilogram",
307+
"Value": 90.0
308+
}
309+
}
310+
```
311+
312+
If you need to support deserializing into properties/fields of type `IComparable` instead of type `IQuantity`, then you can add
313+
```c#
314+
jsonSerializerSettings.Converters.Add(new UnitsNetIQuantityJsonConverter());
315+
```
316+
291317
**Important!**
292318
We cannot guarantee backwards compatibility, although we will strive to do that on a "best effort" basis and bumping the major nuget version when a change is necessary.
293319

@@ -380,4 +406,4 @@ Harrington uses UnitsNet in their internal software to perform many different ca
380406
https://www.harringtonhoists.com<br>
381407
https://kito.com
382408
383-
*- Luke Westfall, Design Automation Engineer*
409+
*- Luke Westfall, Design Automation Engineer*

0 commit comments

Comments
 (0)