Table of Contents

Class PhoneNumberJsonContext

Namespace
PhoneNumbers.Extensions
Assembly
PhoneNumbers.Extensions.dll

Source-generated JsonSerializerContext for PhoneNumber, so consumers building trimmed or Native AOT apps do not need to hand-write one.

[JsonSourceGenerationOptions]
[JsonSerializable(typeof(PhoneNumber))]
public class PhoneNumberJsonContext : JsonSerializerContext, IJsonTypeInfoResolver
Inheritance
PhoneNumberJsonContext
Implements
Inherited Members

Remarks

The metadata this context generates for PhoneNumber is a plain, member-based (reflection-free) serializer — it knows nothing about PhoneNumberConverter. Do not use it to actually serialize a PhoneNumber: PhoneNumber.DefaultInstanceForType is a public get-only property that returns the type's static default instance, which is itself a PhoneNumber exposing the same DefaultInstanceForType property, so member-based serialization walks straight into infinite recursion (a InvalidOperationException for exceeding the writer's max depth, or a real stack overflow at larger depth limits) instead of ever producing JSON. This context exists only so PhoneNumber can be *resolved* (given a JsonTypeInfo to satisfy the type graph) when it appears as a member of another type covered by source generation — e.g. a consumer's own JsonSerializerContext combined with this one via JsonTypeInfoResolver.Combine — provided a converter is also registered so that resolved-but-broken metadata is never actually invoked.

Actual serialization always goes through PhoneNumberConverter, never through the metadata generated here. Prefer Default (or Create(JsonSerializerOptions)), which wires both together correctly. If you build your own JsonSerializerOptions instead, you must set both:

options.TypeInfoResolver = PhoneNumberJsonContext.Default;
options.Converters.Add(new PhoneNumberConverter());

and always serialize via JsonSerializer.Serialize(value, options) / JsonSerializer.Deserialize(json, typeof(T), options) — never via the raw PhoneNumberJsonContext.Default.PhoneNumber JsonTypeInfo<T> directly. That overload uses the JsonTypeInfo<T>'s own baked-in (member-based) converter and bypasses Converters entirely, hitting the recursion above.

Constructors

PhoneNumberJsonContext()

public PhoneNumberJsonContext()

PhoneNumberJsonContext(JsonSerializerOptions)

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

public PhoneNumberJsonContext(JsonSerializerOptions options)

Parameters

options JsonSerializerOptions

The run time provided options for the context instance.

Properties

Boolean

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

public JsonTypeInfo<bool> Boolean { get; }

Property Value

JsonTypeInfo<bool>

CountryCodeSource

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

public JsonTypeInfo<PhoneNumber.Types.CountryCodeSource> CountryCodeSource { get; }

Property Value

JsonTypeInfo<PhoneNumber.Types.CountryCodeSource>

Default

The default JsonSerializerContext associated with a default JsonSerializerOptions instance.

public static PhoneNumberJsonContext Default { get; }

Property Value

PhoneNumberJsonContext

GeneratedSerializerOptions

The source-generated options associated with this context.

protected override JsonSerializerOptions? GeneratedSerializerOptions { get; }

Property Value

JsonSerializerOptions

Int32

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

public JsonTypeInfo<int> Int32 { get; }

Property Value

JsonTypeInfo<int>

PhoneNumber

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

public JsonTypeInfo<PhoneNumber> PhoneNumber { get; }

Property Value

JsonTypeInfo<PhoneNumber>

String

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

public JsonTypeInfo<string> String { get; }

Property Value

JsonTypeInfo<string>

UInt64

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

public JsonTypeInfo<ulong> UInt64 { get; }

Property Value

JsonTypeInfo<ulong>

Methods

GetTypeInfo(Type)

Gets metadata for the specified type.

public override JsonTypeInfo? GetTypeInfo(Type type)

Parameters

type Type

The type to fetch metadata for.

Returns

JsonTypeInfo

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