Class SyntaxNodeSubtypeJsonConverter<T>
Bridges polymorphic (de)serialization for a property or collection element declared as a narrower abstract SyntaxNode subtype (e.g. ExpressionNode) rather than SyntaxNode itself.
public sealed class SyntaxNodeSubtypeJsonConverter<T> : JsonConverter<T> where T : SyntaxNode
Type Parameters
T
- Inheritance
-
SyntaxNodeSubtypeJsonConverter<T>
- Inherited Members
Remarks
[JsonPolymorphic]/[JsonDerivedType] is declared once, on SyntaxNode.
System.Text.Json resolves polymorphism from the property's own declared
type, and does not apply an ancestor's attributes to a narrower abstract type that carries none of
its own — so a property like IfBlockStatementNode.ConditionExpression : ExpressionNode
would otherwise throw NotSupportedException ("Deserialization of interface or abstract
types is not supported") the moment it round-trips through JSON, since ExpressionNode
itself carries no polymorphism attributes. Delegating through SyntaxNode reuses the
one declaration instead of repeating the whole derived-type list on every abstract subtype.
Methods
Read(ref Utf8JsonReader, Type, JsonSerializerOptions)
Reads and converts the JSON to type T.
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
Parameters
readerUtf8JsonReaderThe reader.
typeToConvertTypeThe type to convert.
optionsJsonSerializerOptionsAn object that specifies serialization options to use.
Returns
- T
The converted value.
Write(Utf8JsonWriter, T, JsonSerializerOptions)
Writes a specified value as JSON.
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
Parameters
writerUtf8JsonWriterThe writer to write to.
valueTThe value to convert to JSON.
optionsJsonSerializerOptionsAn object that specifies serialization options to use.