-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,57 @@ | ||
# Working with JSON | ||
# Serializing to and from JSON | ||
|
||
<note> | ||
This topic is incomplete and is currently being improved. | ||
</note> | ||
In this tutorial, we'll see how to serialize and deserialize value objects. | ||
|
||
As well as System.Text.Json (STJ), Vogen also generates the code to work with Newtonsoft.Json (NSJ) | ||
Vogen can automatically generate the code required for this. | ||
It supports both System.Text.Json (STJ), and Newtonsoft.Json (NSJ) | ||
|
||
They are controlled by the `Conversions` enum. The following has serializers for NSJ and STJ: | ||
First, let's see what we get with no conversion generated. | ||
In a C# project that references Vogen, create the following type: | ||
|
||
```c# | ||
[ValueObject<float>(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson)] | ||
[ValueObject<float>(conversions: Conversions.None)] | ||
public readonly partial struct Celsius { } | ||
``` | ||
|
||
See the examples folder for more information. | ||
Now, serialize an instance of `Celsius` to a JSON string using Newtonsoft.Json: | ||
|
||
```C# | ||
var weather = new | ||
{ | ||
Summary = "Sunny and hot", | ||
TemperatureTodayInCelsius = Celsius.From(30) | ||
}; | ||
|
||
Console.WriteLine(JsonSerializer.Serialize(weather)); | ||
``` | ||
|
||
You'll see: | ||
|
||
```Bash | ||
{"Summary":"Sunny and hot","TemperatureTodayInCelsius":{"Value":30}} | ||
``` | ||
|
||
Note that the serializer has written the temperature as a composite object (`Value:30`). | ||
|
||
This isn't ideal as you probably want the primitive value written. | ||
And also, Vogen won't be able to serialize that composite value back into a value object. | ||
|
||
To get just the primitive value written, change `Celcius` to this and rerun. | ||
|
||
```c# | ||
[ValueObject<float>(Conversions.SystemTextJson)] | ||
public readonly partial struct Celsius { } | ||
``` | ||
|
||
This outputs: | ||
|
||
```Bash | ||
{ Summary = Sunny and hot, TemperatureTodayInCelsius = 30 } | ||
``` | ||
As well as treating the value object as a primitive, it also allows it to be deserialized back into a value object. | ||
Not that System.Text.Json is generated by default, so you could remove the `Conversions` parameter altogether. | ||
In this tutorial, we've seen how JSON is serialized if no conversions are specified, and then we've seen the difference | ||
that specifying the conversion makes. | ||