System.Text.Json by Microsoft
Nuget / site data
Details
Info
Name: System.Text.Json
A minimalistic and fast JSON parser/deserializer, for full .NET
Author: Microsoft
NuGet: https://www.nuget.org/packages/System.Text.Json/
You can find more details at https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/source-generation
Source : https://github.com/dotnet/runtime
Original Readme
.NET Runtime
- What is .NET?
- How can I contribute?
- Reporting security issues and security bugs
- Filing issues
- Useful Links
- .NET Foundation
- License
This repo contains the code to build the .NET runtime, libraries and shared host (dotnet
) installers for
all supported platforms, as well as the sources to .NET runtime and libraries.
What is .NET?
Official Starting Page: https://dotnet.microsoft.com
- How to use .NET (with VS, VS Code, command-line CLI)
- Install official releases
- Install daily builds
- Documentation (Get Started, Tutorials, Porting from .NET Framework, API reference, ...)
- Supported OS versions
- Roadmap
- Releases
How can I contribute?
We welcome contributions! Many people all over the world have helped make this project better.
- Contributing explains what kinds of contributions we welcome
- Workflow Instructions explains how to build and test
- Get Up and Running on .NET Core explains how to get nightly builds of the runtime and its libraries to test them in your own projects.
Reporting security issues and security bugs
Security issues and bugs should be reported privately, via email, to the Microsoft Security Response Center (MSRC) secure@microsoft.com. You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Further information, including the MSRC PGP key, can be found in the Security TechCenter. You can also find these instructions in this repo's Security doc.
Also see info about related Microsoft .NET Core and ASP.NET Core Bug Bounty Program.
Filing issues
This repo should contain issues that are tied to the runtime, the class libraries and frameworks, the installation of the dotnet
binary (sometimes known as the muxer
) and installation of the .NET runtime and libraries.
For other issues, please file them to their appropriate sibling repos. We have links to many of them on our new issue page.
Useful Links
- .NET Core source index / .NET Framework source index
- API Reference docs
- .NET API Catalog (incl. APIs from daily builds and API usage info)
- API docs writing guidelines - useful when writing /// comments
- .NET Discord Server - a place to discuss the development of .NET and its ecosystem
.NET Foundation
.NET Runtime is a .NET Foundation project.
There are many .NET related projects on GitHub.
- .NET home repo - links to 100s of .NET projects, from Microsoft and the community.
- ASP.NET Core home - the best place to start learning about ASP.NET Core.
This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community. For more information, see the .NET Foundation Code of Conduct.
General .NET OSS discussions: .NET Foundation Discussions
License
.NET (including the runtime repo) is licensed under the MIT license.
About
Json Serialize without reflection
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- WeatherForecast.cs
- OptionsExampleContext.cs
This is the CSharp Project that references System.Text.Json
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of System.Text.Json in Program.cs
using JsonSerializerOptionsExample;
using System.Text.Json;
//for asp.net core
//services.AddControllers().AddJsonOptions(options => options.JsonSerializerOptions.AddContext<MyJsonContext>());
//https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/raw-string
string jsonString ="""
{
"date": "2019-08-01T00:00:00",
"temperatureCelsius": 25,
"summary": "Hot"
}
""";
WeatherForecast? weatherForecast= JsonSerializer.Deserialize(
jsonString,
typeof(WeatherForecast),
new OptionsExampleContext(
new JsonSerializerOptions(JsonSerializerDefaults.Web)))
as WeatherForecast;
Console.WriteLine($"Date={weatherForecast?.Date}");
// output:
//Date=8/1/2019 12:00:00 AM
jsonString = JsonSerializer.Serialize(
weatherForecast,
typeof(WeatherForecast),
new OptionsExampleContext(
new JsonSerializerOptions(JsonSerializerDefaults.Web)));
Console.WriteLine(jsonString);
jsonString = JsonSerializer.Serialize(
weatherForecast,
typeof(WeatherForecast),
new OptionsExampleContext(
new JsonSerializerOptions(JsonSerializerDefaults.General)));
Console.WriteLine(jsonString);
// output:
//{ "date":"2019-08-01T00:00:00","temperatureCelsius":25,"summary":"Hot"}
This is the use of System.Text.Json in WeatherForecast.cs
namespace JsonSerializerOptionsExample;
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureCelsius { get; set; }
public string? Summary { get; set; }
}
This is the use of System.Text.Json in OptionsExampleContext.cs
using System.Text.Json.Serialization;
namespace JsonSerializerOptionsExample;
[JsonSerializable(typeof(WeatherForecast))]
internal partial class OptionsExampleContext : JsonSerializerContext
{
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- OptionsExampleContext.DateTime.g.cs
- OptionsExampleContext.g.cs
- OptionsExampleContext.GetJsonTypeInfo.g.cs
- OptionsExampleContext.Int32.g.cs
- OptionsExampleContext.PropertyNames.g.cs
- OptionsExampleContext.String.g.cs
- OptionsExampleContext.WeatherForecast.g.cs
// <auto-generated/>
#nullable enable annotations
#nullable disable warnings
// Suppress warnings about [Obsolete] member usage in generated code.
#pragma warning disable CS0618
namespace JsonSerializerOptionsExample
{
internal partial class OptionsExampleContext
{
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.DateTime>? _DateTime;
/// <summary>
/// Defines the source generated JSON serialization contract metadata for a given type.
/// </summary>
public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.DateTime> DateTime
{
get => _DateTime ??= Create_DateTime(Options, makeReadOnly: true);
}
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.DateTime> Create_DateTime(global::System.Text.Json.JsonSerializerOptions options, bool makeReadOnly)
{
global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.DateTime>? jsonTypeInfo = null;
global::System.Text.Json.Serialization.JsonConverter? customConverter;
if (options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(options, typeof(global::System.DateTime))) != null)
{
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::System.DateTime>(options, customConverter);
}
else
{
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::System.DateTime>(options, global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.DateTimeConverter);
}
if (makeReadOnly)
{
jsonTypeInfo.MakeReadOnly();
}
return jsonTypeInfo;
}
}
}
// <auto-generated/>
#nullable enable annotations
#nullable disable warnings
// Suppress warnings about [Obsolete] member usage in generated code.
#pragma warning disable CS0618
namespace JsonSerializerOptionsExample
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Text.Json.SourceGeneration", "7.0.10.6629")]
internal partial class OptionsExampleContext
{
private static global::System.Text.Json.JsonSerializerOptions s_defaultOptions { get; } = new global::System.Text.Json.JsonSerializerOptions()
{
DefaultIgnoreCondition = global::System.Text.Json.Serialization.JsonIgnoreCondition.Never,
IgnoreReadOnlyFields = false,
IgnoreReadOnlyProperties = false,
IncludeFields = false,
WriteIndented = false,
};
private static global::JsonSerializerOptionsExample.OptionsExampleContext? s_defaultContext;
/// <summary>
/// The default <see cref="global::System.Text.Json.Serialization.JsonSerializerContext"/> associated with a default <see cref="global::System.Text.Json.JsonSerializerOptions"/> instance.
/// </summary>
public static global::JsonSerializerOptionsExample.OptionsExampleContext Default => s_defaultContext ??= new global::JsonSerializerOptionsExample.OptionsExampleContext(new global::System.Text.Json.JsonSerializerOptions(s_defaultOptions));
/// <summary>
/// The source-generated options associated with this context.
/// </summary>
protected override global::System.Text.Json.JsonSerializerOptions? GeneratedSerializerOptions { get; } = s_defaultOptions;
/// <inheritdoc/>
public OptionsExampleContext() : base(null)
{
}
/// <inheritdoc/>
public OptionsExampleContext(global::System.Text.Json.JsonSerializerOptions options) : base(options)
{
}
private static global::System.Text.Json.Serialization.JsonConverter? GetRuntimeProvidedCustomConverter(global::System.Text.Json.JsonSerializerOptions options, global::System.Type type)
{
global::System.Collections.Generic.IList<global::System.Text.Json.Serialization.JsonConverter> converters = options.Converters;
for (int i = 0; i < converters.Count; i++)
{
global::System.Text.Json.Serialization.JsonConverter? converter = converters[i];
if (converter.CanConvert(type))
{
if (converter is global::System.Text.Json.Serialization.JsonConverterFactory factory)
{
converter = factory.CreateConverter(type, options);
if (converter == null || converter is global::System.Text.Json.Serialization.JsonConverterFactory)
{
throw new global::System.InvalidOperationException(string.Format("The converter '{0}' cannot return null or a JsonConverterFactory instance.", factory.GetType()));
}
}
return converter;
}
}
return null;
}
}
}
// <auto-generated/>
#nullable enable annotations
#nullable disable warnings
// Suppress warnings about [Obsolete] member usage in generated code.
#pragma warning disable CS0618
namespace JsonSerializerOptionsExample
{
internal partial class OptionsExampleContext: global::System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver
{
/// <inheritdoc/>
public override global::System.Text.Json.Serialization.Metadata.JsonTypeInfo GetTypeInfo(global::System.Type type)
{
if (type == typeof(global::JsonSerializerOptionsExample.WeatherForecast))
{
return this.WeatherForecast;
}
if (type == typeof(global::System.DateTime))
{
return this.DateTime;
}
if (type == typeof(global::System.Int32))
{
return this.Int32;
}
if (type == typeof(global::System.String))
{
return this.String;
}
return null!;
}
global::System.Text.Json.Serialization.Metadata.JsonTypeInfo? global::System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver.GetTypeInfo(global::System.Type type, global::System.Text.Json.JsonSerializerOptions options)
{
if (type == typeof(global::JsonSerializerOptionsExample.WeatherForecast))
{
return Create_WeatherForecast(options, makeReadOnly: false);
}
if (type == typeof(global::System.DateTime))
{
return Create_DateTime(options, makeReadOnly: false);
}
if (type == typeof(global::System.Int32))
{
return Create_Int32(options, makeReadOnly: false);
}
if (type == typeof(global::System.String))
{
return Create_String(options, makeReadOnly: false);
}
return null;
}
}
}
// <auto-generated/>
#nullable enable annotations
#nullable disable warnings
// Suppress warnings about [Obsolete] member usage in generated code.
#pragma warning disable CS0618
namespace JsonSerializerOptionsExample
{
internal partial class OptionsExampleContext
{
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.Int32>? _Int32;
/// <summary>
/// Defines the source generated JSON serialization contract metadata for a given type.
/// </summary>
public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.Int32> Int32
{
get => _Int32 ??= Create_Int32(Options, makeReadOnly: true);
}
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.Int32> Create_Int32(global::System.Text.Json.JsonSerializerOptions options, bool makeReadOnly)
{
global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.Int32>? jsonTypeInfo = null;
global::System.Text.Json.Serialization.JsonConverter? customConverter;
if (options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(options, typeof(global::System.Int32))) != null)
{
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::System.Int32>(options, customConverter);
}
else
{
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::System.Int32>(options, global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.Int32Converter);
}
if (makeReadOnly)
{
jsonTypeInfo.MakeReadOnly();
}
return jsonTypeInfo;
}
}
}
// <auto-generated/>
#nullable enable annotations
#nullable disable warnings
// Suppress warnings about [Obsolete] member usage in generated code.
#pragma warning disable CS0618
namespace JsonSerializerOptionsExample
{
internal partial class OptionsExampleContext
{
private static readonly global::System.Text.Json.JsonEncodedText PropName_Date = global::System.Text.Json.JsonEncodedText.Encode("Date");
private static readonly global::System.Text.Json.JsonEncodedText PropName_TemperatureCelsius = global::System.Text.Json.JsonEncodedText.Encode("TemperatureCelsius");
private static readonly global::System.Text.Json.JsonEncodedText PropName_Summary = global::System.Text.Json.JsonEncodedText.Encode("Summary");
}
}
// <auto-generated/>
#nullable enable annotations
#nullable disable warnings
// Suppress warnings about [Obsolete] member usage in generated code.
#pragma warning disable CS0618
namespace JsonSerializerOptionsExample
{
internal partial class OptionsExampleContext
{
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.String>? _String;
/// <summary>
/// Defines the source generated JSON serialization contract metadata for a given type.
/// </summary>
public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.String> String
{
get => _String ??= Create_String(Options, makeReadOnly: true);
}
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.String> Create_String(global::System.Text.Json.JsonSerializerOptions options, bool makeReadOnly)
{
global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.String>? jsonTypeInfo = null;
global::System.Text.Json.Serialization.JsonConverter? customConverter;
if (options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(options, typeof(global::System.String))) != null)
{
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::System.String>(options, customConverter);
}
else
{
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::System.String>(options, global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.StringConverter);
}
if (makeReadOnly)
{
jsonTypeInfo.MakeReadOnly();
}
return jsonTypeInfo;
}
}
}
// <auto-generated/>
#nullable enable annotations
#nullable disable warnings
// Suppress warnings about [Obsolete] member usage in generated code.
#pragma warning disable CS0618
namespace JsonSerializerOptionsExample
{
internal partial class OptionsExampleContext
{
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::JsonSerializerOptionsExample.WeatherForecast>? _WeatherForecast;
/// <summary>
/// Defines the source generated JSON serialization contract metadata for a given type.
/// </summary>
public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::JsonSerializerOptionsExample.WeatherForecast> WeatherForecast
{
get => _WeatherForecast ??= Create_WeatherForecast(Options, makeReadOnly: true);
}
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::JsonSerializerOptionsExample.WeatherForecast> Create_WeatherForecast(global::System.Text.Json.JsonSerializerOptions options, bool makeReadOnly)
{
global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::JsonSerializerOptionsExample.WeatherForecast>? jsonTypeInfo = null;
global::System.Text.Json.Serialization.JsonConverter? customConverter;
if (options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(options, typeof(global::JsonSerializerOptionsExample.WeatherForecast))) != null)
{
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::JsonSerializerOptionsExample.WeatherForecast>(options, customConverter);
}
else
{
global::System.Text.Json.Serialization.Metadata.JsonObjectInfoValues<global::JsonSerializerOptionsExample.WeatherForecast> objectInfo = new global::System.Text.Json.Serialization.Metadata.JsonObjectInfoValues<global::JsonSerializerOptionsExample.WeatherForecast>()
{
ObjectCreator = static () => new global::JsonSerializerOptionsExample.WeatherForecast(),
ObjectWithParameterizedConstructorCreator = null,
PropertyMetadataInitializer = _ => WeatherForecastPropInit(options),
ConstructorParameterMetadataInitializer = null,
NumberHandling = default,
SerializeHandler = WeatherForecastSerializeHandler
};
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateObjectInfo<global::JsonSerializerOptionsExample.WeatherForecast>(options, objectInfo);
}
if (makeReadOnly)
{
jsonTypeInfo.MakeReadOnly();
}
return jsonTypeInfo;
}
private static global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[] WeatherForecastPropInit(global::System.Text.Json.JsonSerializerOptions options)
{
global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[] properties = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[3];
global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues<global::System.DateTime> info0 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues<global::System.DateTime>()
{
IsProperty = true,
IsPublic = true,
IsVirtual = false,
DeclaringType = typeof(global::JsonSerializerOptionsExample.WeatherForecast),
Converter = null,
Getter = static (obj) => ((global::JsonSerializerOptionsExample.WeatherForecast)obj).Date,
Setter = static (obj, value) => ((global::JsonSerializerOptionsExample.WeatherForecast)obj).Date = value!,
IgnoreCondition = null,
HasJsonInclude = false,
IsExtensionData = false,
NumberHandling = default,
PropertyName = "Date",
JsonPropertyName = null
};
global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo propertyInfo0 = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<global::System.DateTime>(options, info0);
properties[0] = propertyInfo0;
global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues<global::System.Int32> info1 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues<global::System.Int32>()
{
IsProperty = true,
IsPublic = true,
IsVirtual = false,
DeclaringType = typeof(global::JsonSerializerOptionsExample.WeatherForecast),
Converter = null,
Getter = static (obj) => ((global::JsonSerializerOptionsExample.WeatherForecast)obj).TemperatureCelsius,
Setter = static (obj, value) => ((global::JsonSerializerOptionsExample.WeatherForecast)obj).TemperatureCelsius = value!,
IgnoreCondition = null,
HasJsonInclude = false,
IsExtensionData = false,
NumberHandling = default,
PropertyName = "TemperatureCelsius",
JsonPropertyName = null
};
global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo propertyInfo1 = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<global::System.Int32>(options, info1);
properties[1] = propertyInfo1;
global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues<global::System.String> info2 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues<global::System.String>()
{
IsProperty = true,
IsPublic = true,
IsVirtual = false,
DeclaringType = typeof(global::JsonSerializerOptionsExample.WeatherForecast),
Converter = null,
Getter = static (obj) => ((global::JsonSerializerOptionsExample.WeatherForecast)obj).Summary!,
Setter = static (obj, value) => ((global::JsonSerializerOptionsExample.WeatherForecast)obj).Summary = value!,
IgnoreCondition = null,
HasJsonInclude = false,
IsExtensionData = false,
NumberHandling = default,
PropertyName = "Summary",
JsonPropertyName = null
};
global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo propertyInfo2 = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<global::System.String>(options, info2);
properties[2] = propertyInfo2;
return properties;
}
// Intentionally not a static method because we create a delegate to it. Invoking delegates to instance
// methods is almost as fast as virtual calls. Static methods need to go through a shuffle thunk.
private void WeatherForecastSerializeHandler(global::System.Text.Json.Utf8JsonWriter writer, global::JsonSerializerOptionsExample.WeatherForecast? value)
{
if (value == null)
{
writer.WriteNullValue();
return;
}
writer.WriteStartObject();
writer.WriteString(PropName_Date, value.Date);
writer.WriteNumber(PropName_TemperatureCelsius, value.TemperatureCelsius);
writer.WriteString(PropName_Summary, value.Summary);
writer.WriteEndObject();
}
}
}
Usefull
Download Example (.NET C# )
Share System.Text.Json
https://ignatandrei.github.io/RSCG_Examples/v2/docs/System.Text.Json