Skip to content

Commit

Permalink
Document source generation for System.Text.Json (#25451)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdykstra authored Aug 24, 2021
1 parent 1256e28 commit bb8b554
Show file tree
Hide file tree
Showing 24 changed files with 763 additions and 5 deletions.
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>
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>
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>
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();
}
}
}
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>
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>
Loading

0 comments on commit bb8b554

Please sign in to comment.