Class MosaicJsonContext

System.Text.Json source-generation context for MOSAIC configuration types.

Inheritance
MosaicJsonContext
Namespace: MOSAIC.Components.Basics
Assembly: MOSAIC.dll
Syntax
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, ReadCommentHandling = JsonCommentHandling.Skip, AllowTrailingCommas = true, NumberHandling = JsonNumberHandling.AllowReadingFromString, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(JsonModel))]
[JsonSerializable(typeof(Dictionary<string, JsonModel>))]
internal class MosaicJsonContext : JsonSerializerContext, IJsonTypeInfoResolver
Remarks

This context enables fast, reflection-free serialization and deserialization and is suitable for ahead-of-time (AOT) compilation scenarios.

Current options:

  • camelCase property naming
  • skip comments
  • allow trailing commas
  • allow numeric values encoded as strings
  • omit nulls when writing

Adjust the JsonSourceGenerationOptionsAttribute on this type to control the overall JSON contract. Add additional JsonSerializableAttribute entries for any new types that need serialization support.

Constructors

MosaicJsonContext()

Declaration
public MosaicJsonContext()

MosaicJsonContext(JsonSerializerOptions)

Creates an instance of JsonSerializerContext and binds it with the indicated JsonSerializerOptions.

Declaration
public MosaicJsonContext(JsonSerializerOptions options)
Parameters
Type Name Description
JsonSerializerOptions options

The run time provided options for the context instance.

Fields

InstanceMemberBindingFlags

Declaration
private const BindingFlags InstanceMemberBindingFlags = Instance | Public | NonPublic
Field Value
Type Description
BindingFlags

_DictionaryStringJsonModel

Declaration
private JsonTypeInfo<Dictionary<string, JsonModel>>? _DictionaryStringJsonModel
Field Value
Type Description
JsonTypeInfo<Dictionary<string, JsonModel>>

_Double

Declaration
private JsonTypeInfo<double>? _Double
Field Value
Type Description
JsonTypeInfo<double>

_IReadOnlyListObject

Declaration
private JsonTypeInfo<IReadOnlyList<object>>? _IReadOnlyListObject
Field Value
Type Description
JsonTypeInfo<IReadOnlyList<object>>

_IReadOnlyListString

Declaration
private JsonTypeInfo<IReadOnlyList<string>>? _IReadOnlyListString
Field Value
Type Description
JsonTypeInfo<IReadOnlyList<string>>

_Int32

Declaration
private JsonTypeInfo<int>? _Int32
Field Value
Type Description
JsonTypeInfo<int>

_JsonModel

Declaration
private JsonTypeInfo<JsonModel>? _JsonModel
Field Value
Type Description
JsonTypeInfo<JsonModel>

_NullableDouble

Declaration
private JsonTypeInfo<double?>? _NullableDouble
Field Value
Type Description
JsonTypeInfo<double?>

_Object

Declaration
private JsonTypeInfo<object>? _Object
Field Value
Type Description
JsonTypeInfo<object>

_String

Declaration
private JsonTypeInfo<string>? _String
Field Value
Type Description
JsonTypeInfo<string>

s_defaultOptions

Declaration
private static readonly JsonSerializerOptions s_defaultOptions
Field Value
Type Description
JsonSerializerOptions

Properties

Default

The default JsonSerializerContext associated with a default JsonSerializerOptions instance.

Declaration
public static MosaicJsonContext Default { get; }
Property Value
Type Description
MosaicJsonContext

DictionaryStringJsonModel

Defines the source generated JSON serialization contract metadata for a given type.

Declaration
public JsonTypeInfo<Dictionary<string, JsonModel>> DictionaryStringJsonModel { get; }
Property Value
Type Description
JsonTypeInfo<Dictionary<string, JsonModel>>

Double

Defines the source generated JSON serialization contract metadata for a given type.

Declaration
public JsonTypeInfo<double> Double { get; }
Property Value
Type Description
JsonTypeInfo<double>

GeneratedSerializerOptions

The source-generated options associated with this context.

Declaration
protected override JsonSerializerOptions? GeneratedSerializerOptions { get; }
Property Value
Type Description
JsonSerializerOptions
Overrides
JsonSerializerContext.GeneratedSerializerOptions

IReadOnlyListObject

Defines the source generated JSON serialization contract metadata for a given type.

Declaration
public JsonTypeInfo<IReadOnlyList<object>> IReadOnlyListObject { get; }
Property Value
Type Description
JsonTypeInfo<IReadOnlyList<object>>

IReadOnlyListString

Defines the source generated JSON serialization contract metadata for a given type.

Declaration
public JsonTypeInfo<IReadOnlyList<string>> IReadOnlyListString { get; }
Property Value
Type Description
JsonTypeInfo<IReadOnlyList<string>>

Int32

Defines the source generated JSON serialization contract metadata for a given type.

Declaration
public JsonTypeInfo<int> Int32 { get; }
Property Value
Type Description
JsonTypeInfo<int>

JsonModel

Defines the source generated JSON serialization contract metadata for a given type.

Declaration
public JsonTypeInfo<JsonModel> JsonModel { get; }
Property Value
Type Description
JsonTypeInfo<JsonModel>

NullableDouble

Defines the source generated JSON serialization contract metadata for a given type.

Declaration
public JsonTypeInfo<double?> NullableDouble { get; }
Property Value
Type Description
JsonTypeInfo<double?>

Object

Defines the source generated JSON serialization contract metadata for a given type.

Declaration
public JsonTypeInfo<object> Object { get; }
Property Value
Type Description
JsonTypeInfo<object>

String

Defines the source generated JSON serialization contract metadata for a given type.

Declaration
public JsonTypeInfo<string> String { get; }
Property Value
Type Description
JsonTypeInfo<string>

Methods

Create_DictionaryStringJsonModel(JsonSerializerOptions)

Declaration
private JsonTypeInfo<Dictionary<string, JsonModel>> Create_DictionaryStringJsonModel(JsonSerializerOptions options)
Parameters
Type Name Description
JsonSerializerOptions options
Returns
Type Description
JsonTypeInfo<Dictionary<string, JsonModel>>

Create_Double(JsonSerializerOptions)

Declaration
private JsonTypeInfo<double> Create_Double(JsonSerializerOptions options)
Parameters
Type Name Description
JsonSerializerOptions options
Returns
Type Description
JsonTypeInfo<double>

Create_IReadOnlyListObject(JsonSerializerOptions)

Declaration
private JsonTypeInfo<IReadOnlyList<object>> Create_IReadOnlyListObject(JsonSerializerOptions options)
Parameters
Type Name Description
JsonSerializerOptions options
Returns
Type Description
JsonTypeInfo<IReadOnlyList<object>>

Create_IReadOnlyListString(JsonSerializerOptions)

Declaration
private JsonTypeInfo<IReadOnlyList<string>> Create_IReadOnlyListString(JsonSerializerOptions options)
Parameters
Type Name Description
JsonSerializerOptions options
Returns
Type Description
JsonTypeInfo<IReadOnlyList<string>>

Create_Int32(JsonSerializerOptions)

Declaration
private JsonTypeInfo<int> Create_Int32(JsonSerializerOptions options)
Parameters
Type Name Description
JsonSerializerOptions options
Returns
Type Description
JsonTypeInfo<int>

Create_JsonModel(JsonSerializerOptions)

Declaration
private JsonTypeInfo<JsonModel> Create_JsonModel(JsonSerializerOptions options)
Parameters
Type Name Description
JsonSerializerOptions options
Returns
Type Description
JsonTypeInfo<JsonModel>

Create_NullableDouble(JsonSerializerOptions)

Declaration
private JsonTypeInfo<double?> Create_NullableDouble(JsonSerializerOptions options)
Parameters
Type Name Description
JsonSerializerOptions options
Returns
Type Description
JsonTypeInfo<double?>

Create_Object(JsonSerializerOptions)

Declaration
private JsonTypeInfo<object> Create_Object(JsonSerializerOptions options)
Parameters
Type Name Description
JsonSerializerOptions options
Returns
Type Description
JsonTypeInfo<object>

Create_String(JsonSerializerOptions)

Declaration
private JsonTypeInfo<string> Create_String(JsonSerializerOptions options)
Parameters
Type Name Description
JsonSerializerOptions options
Returns
Type Description
JsonTypeInfo<string>

DictionaryStringJsonModelSerializeHandler(Utf8JsonWriter, Dictionary<string, JsonModel>?)

Declaration
private void DictionaryStringJsonModelSerializeHandler(Utf8JsonWriter writer, Dictionary<string, JsonModel>? value)
Parameters
Type Name Description
Utf8JsonWriter writer
Dictionary<string, JsonModel> value

ExpandConverter(Type, JsonConverter, JsonSerializerOptions, bool)

Declaration
private static JsonConverter ExpandConverter(Type type, JsonConverter converter, JsonSerializerOptions options, bool validateCanConvert = true)
Parameters
Type Name Description
Type type
JsonConverter converter
JsonSerializerOptions options
bool validateCanConvert
Returns
Type Description
JsonConverter

GetRuntimeConverterForType(Type, JsonSerializerOptions)

Declaration
private static JsonConverter? GetRuntimeConverterForType(Type type, JsonSerializerOptions options)
Parameters
Type Name Description
Type type
JsonSerializerOptions options
Returns
Type Description
JsonConverter

GetTypeInfo(Type)

Gets metadata for the specified type.

Declaration
public override JsonTypeInfo? GetTypeInfo(Type type)
Parameters
Type Name Description
Type type

The type to fetch metadata for.

Returns
Type Description
JsonTypeInfo

The metadata for the specified type, or null if the context has no metadata for the type.

Overrides
JsonSerializerContext.GetTypeInfo(Type)

IReadOnlyListStringSerializeHandler(Utf8JsonWriter, IReadOnlyList<string>?)

Declaration
private void IReadOnlyListStringSerializeHandler(Utf8JsonWriter writer, IReadOnlyList<string>? value)
Parameters
Type Name Description
Utf8JsonWriter writer
IReadOnlyList<string> value

JsonModelCtorParamInit()

Declaration
private static JsonParameterInfoValues[] JsonModelCtorParamInit()
Returns
Type Description
JsonParameterInfoValues[]

JsonModelPropInit(JsonSerializerOptions)

Declaration
private static JsonPropertyInfo[] JsonModelPropInit(JsonSerializerOptions options)
Parameters
Type Name Description
JsonSerializerOptions options
Returns
Type Description
JsonPropertyInfo[]

TryGetTypeInfoForRuntimeCustomConverter<TJsonMetadataType>(JsonSerializerOptions, out JsonTypeInfo<TJsonMetadataType>)

Declaration
private static bool TryGetTypeInfoForRuntimeCustomConverter<TJsonMetadataType>(JsonSerializerOptions options, out JsonTypeInfo<TJsonMetadataType> jsonTypeInfo)
Parameters
Type Name Description
JsonSerializerOptions options
JsonTypeInfo<TJsonMetadataType> jsonTypeInfo
Returns
Type Description
bool
Type Parameters
Name Description
TJsonMetadataType

Explicit Interface Implementations

IJsonTypeInfoResolver.GetTypeInfo(Type, JsonSerializerOptions)

Resolves a JsonTypeInfo contract for the requested type and options.

Declaration
JsonTypeInfo? IJsonTypeInfoResolver.GetTypeInfo(Type type, JsonSerializerOptions options)
Parameters
Type Name Description
Type type

Type to be resolved.

JsonSerializerOptions options

Configuration used when resolving the metadata.

Returns
Type Description
JsonTypeInfo

A JsonTypeInfo instance matching the requested type, or null if no contract could be resolved.

Implements

IJsonTypeInfoResolver

Search documentation

Start typing to search the documentation.