Class JsonUnion<TBase>
- Namespace
- Nerdbank.Json
- Assembly
- Nerdbank.Json.dll
An immutable description of a polymorphic union for the base type TBase, built from
explicitly supplied case type shapes.
public sealed class JsonUnion<TBase>
Type Parameters
TBaseThe base type of the union.
- Inheritance
-
JsonUnion<TBase>
- Inherited Members
Remarks
Use WithUnion<TBase>(JsonUnion<TBase>) to apply the definition to a serializer.
Methods
AddCase<TCase>(int, ITypeShape<TCase>)
Returns a definition with an additional case identified by an integer tag.
public JsonUnion<TBase> AddCase<TCase>(int tag, ITypeShape<TCase> caseShape) where TCase : TBase
Parameters
tagintThe integer discriminator for this case.
caseShapeITypeShape<TCase>The shape for the case type.
Returns
- JsonUnion<TBase>
A new definition.
Type Parameters
TCaseThe case type.
AddCase<TCase>(string, ITypeShape<TCase>)
Returns a definition with an additional case identified by a string name.
public JsonUnion<TBase> AddCase<TCase>(string name, ITypeShape<TCase> caseShape) where TCase : TBase
Parameters
namestringThe string discriminator for this case.
caseShapeITypeShape<TCase>The shape for the case type.
Returns
- JsonUnion<TBase>
A new definition.
Type Parameters
TCaseThe case type.
Create()
Creates an empty union definition that replaces the shape-generated cases with only the cases added to it.
public static JsonUnion<TBase> Create()
Returns
- JsonUnion<TBase>
A new definition.
IncludeGenerated()
Returns a definition that also includes the shape-generated cases (if any) in addition to the added cases.
public JsonUnion<TBase> IncludeGenerated()
Returns
- JsonUnion<TBase>
A new definition.
UseDuckTyping()
Returns a definition that uses the experimental discriminator-free "duck typing" strategy, which selects a case during deserialization from the unique set of required serialized property names present in a JSON object.
public JsonUnion<TBase> UseDuckTyping()
Returns
- JsonUnion<TBase>
A new definition.
Remarks
Duck typing does not use the [discriminator, payload] envelope: values are written as the bare case
object, and read by matching required property names. It is slower than the default strategy because it buffers
and re-scans each object, it rejects ambiguous or insufficient evidence, and it cannot use optional properties.