-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document source generation for System.Text.Json (#25451)
- Loading branch information
Showing
24 changed files
with
763 additions
and
5 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
...rd/serialization/snippets/system-text-json-source-generation/csharp/BothModesNoOptions.cs
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// <All> | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace BothModesNoOptions | ||
{ | ||
// <WF> | ||
public class WeatherForecast | ||
{ | ||
public DateTime Date { get; set; } | ||
public int TemperatureCelsius { get; set; } | ||
public string Summary { get; set; } | ||
} | ||
// </WF> | ||
|
||
// <DefineContext> | ||
[JsonSourceGenerationOptions(WriteIndented = true)] | ||
[JsonSerializable(typeof(WeatherForecast))] | ||
internal partial class SourceGenerationContext : JsonSerializerContext | ||
{ | ||
} | ||
// </DefineContext> | ||
|
||
public class Program | ||
{ | ||
public static void Main() | ||
{ | ||
string jsonString = | ||
@"{ | ||
""Date"": ""2019-08-01T00:00:00"", | ||
""TemperatureCelsius"": 25, | ||
""Summary"": ""Hot"" | ||
} | ||
"; | ||
WeatherForecast weatherForecast; | ||
|
||
// <DeserializeWithTypeInfo> | ||
weatherForecast = JsonSerializer.Deserialize<WeatherForecast>( | ||
jsonString, SourceGenerationContext.Default.WeatherForecast); | ||
// </DeserializeWithTypeInfo> | ||
Console.WriteLine($"Date={weatherForecast.Date}"); | ||
// output: | ||
//Date=8/1/2019 12:00:00 AM | ||
|
||
// <DeserializeWithContext> | ||
weatherForecast = (WeatherForecast)JsonSerializer.Deserialize( | ||
jsonString, typeof(WeatherForecast), SourceGenerationContext.Default); | ||
// </DeserializeWithContext> | ||
Console.WriteLine($"Date={weatherForecast.Date}"); | ||
// output: | ||
//Date=8/1/2019 12:00:00 AM | ||
|
||
// <SerializeWithTypeInfo> | ||
jsonString = JsonSerializer.Serialize( | ||
weatherForecast, SourceGenerationContext.Default.WeatherForecast); | ||
// </SerializeWithTypeInfo> | ||
Console.WriteLine(jsonString); | ||
// output: | ||
//{"Date":"2019-08-01T00:00:00","TemperatureCelsius":25,"Summary":"Hot"} | ||
|
||
// <SerializeWithContext> | ||
jsonString = JsonSerializer.Serialize( | ||
weatherForecast, typeof(WeatherForecast), SourceGenerationContext.Default); | ||
// </SerializeWithContext> | ||
Console.WriteLine(jsonString); | ||
// output: | ||
//{"Date":"2019-08-01T00:00:00","TemperatureCelsius":25,"Summary":"Hot"} | ||
|
||
// <SerializeDirect> | ||
using MemoryStream stream = new(); | ||
using Utf8JsonWriter writer = new(stream); | ||
SourceGenerationContext.Default.WeatherForecast.Serialize( | ||
writer, weatherForecast); | ||
writer.Flush(); | ||
// </SerializeDirect> | ||
Console.WriteLine(Encoding.UTF8.GetString(stream.ToArray())); | ||
// output: | ||
//{"Date":"2019-08-01T00:00:00","TemperatureCelsius":25,"Summary":"Hot"} | ||
} | ||
} | ||
} | ||
// </All> |
59 changes: 59 additions & 0 deletions
59
...zation/snippets/system-text-json-source-generation/csharp/JsonSerializerOptionsExample.cs
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// <All> | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace JsonSerializerOptionsExample | ||
{ | ||
public class WeatherForecast | ||
{ | ||
public DateTime Date { get; set; } | ||
public int TemperatureCelsius { get; set; } | ||
public string Summary { get; set; } | ||
} | ||
|
||
// <DefineContext> | ||
[JsonSerializable(typeof(WeatherForecast))] | ||
internal partial class OptionsExampleContext : JsonSerializerContext | ||
{ | ||
} | ||
// </DefineContext> | ||
|
||
public class Program | ||
{ | ||
public static void Main() | ||
{ | ||
string jsonString = | ||
@"{ | ||
""date"": ""2019-08-01T00:00:00"", | ||
""temperatureCelsius"": 25, | ||
""summary"": ""Hot"" | ||
} | ||
"; | ||
WeatherForecast weatherForecast; | ||
|
||
// <Deserialize> | ||
weatherForecast = (WeatherForecast)JsonSerializer.Deserialize( | ||
jsonString, | ||
typeof(WeatherForecast), | ||
new OptionsExampleContext( | ||
new JsonSerializerOptions(JsonSerializerDefaults.Web))); | ||
// </Deserialize> | ||
Console.WriteLine($"Date={weatherForecast.Date}"); | ||
// output: | ||
//Date=8/1/2019 12:00:00 AM | ||
|
||
// <Serialize> | ||
jsonString = JsonSerializer.Serialize( | ||
weatherForecast, | ||
typeof(WeatherForecast), | ||
new OptionsExampleContext( | ||
new JsonSerializerOptions(JsonSerializerDefaults.Web))); | ||
// </Serialize> | ||
Console.WriteLine(jsonString); | ||
// output: | ||
//{ "date":"2019-08-01T00:00:00","temperatureCelsius":25,"summary":"Hot"} | ||
} | ||
} | ||
} | ||
// </All> |
82 changes: 82 additions & 0 deletions
82
...serialization/snippets/system-text-json-source-generation/csharp/MetadataOnlyNoOptions.cs
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// <All> | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace MetadataOnlyNoOptions | ||
{ | ||
public class WeatherForecast | ||
{ | ||
public DateTime Date { get; set; } | ||
public int TemperatureCelsius { get; set; } | ||
public string Summary { get; set; } | ||
} | ||
|
||
// <JsonSerializableGenMode> | ||
[JsonSerializable(typeof(WeatherForecast), GenerationMode = JsonSourceGenerationMode.Metadata)] | ||
internal partial class MetadataOnlyWeatherForecastOnlyContext : JsonSerializerContext | ||
{ | ||
} | ||
// </JsonSerializableGenMode> | ||
|
||
// <JsonSourceGenerationOptionsGenMode> | ||
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata)] | ||
[JsonSerializable(typeof(WeatherForecast))] | ||
internal partial class MetadataOnlyContext : JsonSerializerContext | ||
{ | ||
} | ||
// </JsonSourceGenerationOptionsGenMode> | ||
|
||
public class Program | ||
{ | ||
public static void Main() | ||
{ | ||
string jsonString = | ||
@"{ | ||
""Date"": ""2019-08-01T00:00:00"", | ||
""TemperatureCelsius"": 25, | ||
""Summary"": ""Hot"" | ||
} | ||
"; | ||
WeatherForecast weatherForecast; | ||
|
||
// Deserialize with context that selects metadata mode only for WeatherForecast only. | ||
// <DeserializeWFContext> | ||
weatherForecast = JsonSerializer.Deserialize<WeatherForecast>( | ||
jsonString, MetadataOnlyWeatherForecastOnlyContext.Default.WeatherForecast); | ||
// </DeserializeWFContext> | ||
Console.WriteLine($"Date={weatherForecast.Date}"); | ||
// output: | ||
//Date=8/1/2019 12:00:00 AM | ||
|
||
// Serialize with context that selects metadata mode only for WeatherForecast only. | ||
// <SerializeWFContext> | ||
jsonString = JsonSerializer.Serialize( | ||
weatherForecast, | ||
MetadataOnlyWeatherForecastOnlyContext.Default.WeatherForecast); | ||
// </SerializeWFContext> | ||
Console.WriteLine(jsonString); | ||
// output: | ||
//{"Date":"2019-08-01T00:00:00","TemperatureCelsius":25,"Summary":"Hot"} | ||
|
||
// Deserialize with context that selects metadata mode only. | ||
// <DeserializeMetadataOnlyContext> | ||
weatherForecast = JsonSerializer.Deserialize<WeatherForecast>( | ||
jsonString, MetadataOnlyContext.Default.WeatherForecast); | ||
// </DeserializeMetadataOnlyContext> | ||
Console.WriteLine($"Date={weatherForecast.Date}"); | ||
// output: | ||
//Date=8/1/2019 12:00:00 AM | ||
|
||
// Serialize with context that selects metadata mode only. | ||
// <SerializeMetadataOnlyContext> | ||
jsonString = JsonSerializer.Serialize( | ||
weatherForecast, MetadataOnlyContext.Default.WeatherForecast); | ||
// </SerializeMetadataOnlyContext> | ||
Console.WriteLine(jsonString); | ||
// output: | ||
//{"Date":"2019-08-01T00:00:00","TemperatureCelsius":0,"Summary":"Hot"} | ||
} | ||
} | ||
} | ||
// </All> |
24 changes: 24 additions & 0 deletions
24
docs/standard/serialization/snippets/system-text-json-source-generation/csharp/Program.cs
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace SystemTextJsonSamples | ||
{ | ||
public class Program | ||
{ | ||
static void Main() | ||
{ | ||
Console.WriteLine("\n============================= BothModesNoOptions\n"); | ||
BothModesNoOptions.Program.Main(); | ||
Console.WriteLine("\n============================= SerializeOnlyNoOptions\n"); | ||
SerializeOnlyNoOptions.Program.Main(); | ||
Console.WriteLine("\n============================= MetadataOnlyNoOptions\n"); | ||
MetadataOnlyNoOptions.Program.Main(); | ||
Console.WriteLine("\n============================= SerializeOnlyWithOptions\n"); | ||
SerializeOnlyWithOptions.Program.Main(); | ||
Console.WriteLine("\n============================= JsonSerializerOptionsExample\n"); | ||
JsonSerializerOptionsExample.Program.Main(); | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...erialization/snippets/system-text-json-source-generation/csharp/SerializeOnlyNoOptions.cs
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// <All> | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace SerializeOnlyNoOptions | ||
{ | ||
public class WeatherForecast | ||
{ | ||
public DateTime Date { get; set; } | ||
public int TemperatureCelsius { get; set; } | ||
public string Summary { get; set; } | ||
} | ||
|
||
// <JsonSourceGenerationOptionsGenmode> | ||
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Serialization)] | ||
[JsonSerializable(typeof(WeatherForecast))] | ||
internal partial class SerializeOnlyContext : JsonSerializerContext | ||
{ | ||
} | ||
// </JsonSourceGenerationOptionsGenmode> | ||
|
||
// <JsonSerializableGenmode> | ||
[JsonSerializable(typeof(WeatherForecast), GenerationMode = JsonSourceGenerationMode.Serialization)] | ||
internal partial class SerializeOnlyWeatherForecastOnlyContext : JsonSerializerContext | ||
{ | ||
} | ||
// </JsonSerializableGenmode> | ||
|
||
public class Program | ||
{ | ||
public static void Main() | ||
{ | ||
WeatherForecast weatherForecast = new() | ||
{ Date = DateTime.Parse("2019-08-01"), TemperatureCelsius = 25, Summary = "Hot" }; | ||
|
||
// Use a context that selects Serialization mode. | ||
// <SerializeOnlyContext> | ||
using MemoryStream memoryStream1 = new(); | ||
using Utf8JsonWriter writer1 = new(memoryStream1); | ||
SerializeOnlyContext.Default.WeatherForecast.Serialize(writer1, weatherForecast); | ||
writer1.Flush(); | ||
// </SerializeOnlyContext> | ||
Console.WriteLine(Encoding.UTF8.GetString(memoryStream1.ToArray())); | ||
// output: | ||
//{"Date":"2019-08-01T00:00:00","TemperatureCelsius":25,"Summary":"Hot"} | ||
|
||
// Use context that selects Serialization mode only for WeatherForecast. | ||
// <SerializeWFContext> | ||
using MemoryStream memoryStream2 = new(); | ||
using Utf8JsonWriter writer2 = new(memoryStream2); | ||
SerializeOnlyContext.Default.WeatherForecast.Serialize(writer2, weatherForecast); | ||
writer2.Flush(); | ||
// </SerializeWFContext> | ||
Console.WriteLine(Encoding.UTF8.GetString(memoryStream2.ToArray())); | ||
// output: | ||
//{"Date":"2019-08-01T00:00:00","TemperatureCelsius":25,"Summary":"Hot"} | ||
} | ||
} | ||
} | ||
// </All> |
77 changes: 77 additions & 0 deletions
77
...ialization/snippets/system-text-json-source-generation/csharp/SerializeOnlyWithOptions.cs
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// <All> | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace SerializeOnlyWithOptions | ||
{ | ||
public class WeatherForecast | ||
{ | ||
public DateTime Date { get; set; } | ||
public int TemperatureCelsius { get; set; } | ||
public string Summary { get; set; } | ||
} | ||
|
||
// <JsonSourceGenerationOptions> | ||
[JsonSourceGenerationOptions( | ||
WriteIndented = true, | ||
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, | ||
GenerationMode = JsonSourceGenerationMode.Serialization)] | ||
[JsonSerializable(typeof(WeatherForecast))] | ||
internal partial class SerializationModeOptionsContext : JsonSerializerContext | ||
{ | ||
} | ||
// </JsonSourceGenerationOptions> | ||
public class Program | ||
{ | ||
public static void Main() | ||
{ | ||
string jsonString; | ||
WeatherForecast weatherForecast = new() | ||
{ Date = DateTime.Parse("2019-08-01"), TemperatureCelsius = 25, Summary = "Hot" }; | ||
|
||
// Serialize using Default context | ||
// and options specified by [JsonSourceGenerationOptions]. | ||
// <SerializeWithContext> | ||
jsonString = JsonSerializer.Serialize( | ||
weatherForecast, typeof(WeatherForecast), SerializationModeOptionsContext.Default); | ||
// </SerializeWithContext> | ||
Console.WriteLine(jsonString); | ||
// output: | ||
//{ | ||
// "date": "2019-08-01T00:00:00", | ||
// "temperatureCelsius": 0, | ||
// "summary": "Hot" | ||
//} | ||
|
||
// Serialize using TypeInfo<TValue> provided by the context | ||
// and options specified by [JsonSourceGenerationOptions]. | ||
// <SerializeWithTypeInfo> | ||
jsonString = JsonSerializer.Serialize( | ||
weatherForecast, SerializationModeOptionsContext.Default.WeatherForecast); | ||
// </SerializeWithTypeInfo> | ||
Console.WriteLine(jsonString); | ||
// output: | ||
//{ | ||
// "date": "2019-08-01T00:00:00", | ||
// "temperatureCelsius": 0, | ||
// "summary": "Hot" | ||
//} | ||
|
||
// <SerializeDirect> | ||
using MemoryStream stream = new(); | ||
using Utf8JsonWriter writer = new(stream); | ||
SerializationModeOptionsContext.Default.WeatherForecast.Serialize( | ||
writer, weatherForecast); | ||
writer.Flush(); | ||
// </SerializeDirect> | ||
Console.WriteLine(Encoding.UTF8.GetString(stream.ToArray())); | ||
// output: | ||
//{"date":"2019-08-01T00:00:00","temperatureCelsius":25,"summary":"Hot"} | ||
} | ||
} | ||
} | ||
// </All> |
Oops, something went wrong.