Class PhoneNumberJsonOptions
- Namespace
- PhoneNumbers.Extensions
- Assembly
- PhoneNumbers.Extensions.dll
Ready-made JsonSerializerOptions for serializing PhoneNumber under Native AOT / trimming, where the reflection-based default resolver is unavailable. Combines PhoneNumberJsonContext (source-generated type metadata) with PhoneNumberConverter (the actual read/write logic) so callers cannot wire the two together incorrectly — see the remarks on PhoneNumberJsonContext for why doing it by hand is easy to get wrong.
public static class PhoneNumberJsonOptions
- Inheritance
-
PhoneNumberJsonOptions
- Inherited Members
Properties
Default
A shared, ready-to-use JsonSerializerOptions instance for a bare
PhoneNumber, e.g.
JsonSerializer.Serialize(number, PhoneNumberJsonOptions.Default). Instances of
JsonSerializerOptions are safe to reuse concurrently once first used, so this
is safe to share across a whole application.
public static JsonSerializerOptions Default { get; }
Property Value
Methods
Create(JsonSerializerOptions)
Builds a new JsonSerializerOptions with PhoneNumberJsonContext and PhoneNumberConverter wired together correctly. Use this instead of Default when you need to layer in your own settings (e.g. combining Default with your own JsonSerializerContext via JsonTypeInfoResolver.Combine for a DTO that has a PhoneNumber property), or when you don't want to mutate the shared Default instance.
public static JsonSerializerOptions Create(JsonSerializerOptions baseOptions = null)
Parameters
baseOptionsJsonSerializerOptionsOptional options to copy other settings from (e.g. WriteIndented). If
baseOptionsalready has a TypeInfoResolver set (e.g. your own JsonSerializerContext, or a resolver already combined viaJsonTypeInfoResolver.Combine), it is combined with Default rather than replaced, so a DTO type resolved by your own context can still contain a PhoneNumber property. A PhoneNumberConverter is always appended to Converters.
Returns
Deserialize(string)
Deserializes a PhoneNumber from its E.164 JSON string using
Default. See Serialize(PhoneNumber) for why this is preferable to calling
JsonSerializer.Deserialize<PhoneNumber>(json, PhoneNumberJsonOptions.Default)
directly under trim/AOT analysis.
[UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode", Justification = "Default always resolves PhoneNumber via PhoneNumberJsonContext + PhoneNumberConverter, never reflection.")]
[UnconditionalSuppressMessage("AOT", "IL3050:RequiresDynamicCode", Justification = "Default always resolves PhoneNumber via PhoneNumberJsonContext + PhoneNumberConverter, never reflection.")]
public static PhoneNumber Deserialize(string json)
Parameters
jsonstring
Returns
Serialize(PhoneNumber)
Serializes a PhoneNumber to its E.164 JSON string using
Default. Prefer this over calling
JsonSerializer.Serialize(number, PhoneNumberJsonOptions.Default) yourself: that call
goes through the JsonSerializerOptions-based overload, which the trimming/AOT
analyzers always flag (IL2026/IL3050) because they cannot see that Default
never actually falls back to reflection. This method carries that guarantee instead, so a
consumer project with trim/AOT analysis on gets no warning for using it.
[UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode", Justification = "Default always resolves PhoneNumber via PhoneNumberJsonContext + PhoneNumberConverter, never reflection.")]
[UnconditionalSuppressMessage("AOT", "IL3050:RequiresDynamicCode", Justification = "Default always resolves PhoneNumber via PhoneNumberJsonContext + PhoneNumberConverter, never reflection.")]
public static string Serialize(PhoneNumber number)
Parameters
numberPhoneNumber