Class ValueObjectConverter
Namespace: AdCodicem.ValueObjects.NewtonsoftJson
Assembly: AdCodicem.ValueObjects.NewtonsoftJson.dll
Reads and writes any value object as its bare underlying value.
public sealed class ValueObjectConverter : JsonConverter
Inheritance
object ← JsonConverter ← ValueObjectConverter
Inherited Members
JsonConverter.WriteJson(JsonWriter, object?, JsonSerializer), JsonConverter.ReadJson(JsonReader, Type, object?, JsonSerializer), JsonConverter.CanConvert(Type), JsonConverter.CanRead, JsonConverter.CanWrite, object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.ReferenceEquals(object?, object?), object.ToString()
Remarks
One converter covers every value object, because the work is delegated to the runtime descriptor rather than to a type-specific implementation. Newtonsoft.Json resolves converters by reflection anyway, so there is nothing to gain from a generic converter per type here.
It follows the System.Text.Json converter the generator emits: the same rules, and the same values on the wire.
The text is the same too, but for a whole 1250.0 where System.Text.Json writes 1250.
Each serializer reads the other's text as the same value.
Methods
CanConvert(Type)
Determines whether this instance can convert the specified object type.
public override bool CanConvert(Type objectType)
Parameters
objectType Type
Type of the object.
Returns
true if this instance can convert the specified object type; otherwise, false.
ReadJson(JsonReader, Type, object?, JsonSerializer)
Reads the JSON representation of the object.
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
Parameters
reader JsonReader
The
objectType Type
Type of the object.
existingValue object?
The existing value of object being read.
serializer JsonSerializer
The calling serializer.
Returns
The object value.
Remarks
The value is read as the System.Text.Json converter the generator emits reads it: from the kind of token it writes, and through the value object's rules, so a rejection says which rule refused it.
Newtonsoft.Json reads the token under the serializer's settings before the converter sees it. Under its
default +00:00 for UTC.
Under
WriteJson(JsonWriter, object?, JsonSerializer)
Writes the JSON representation of the object.
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
Parameters
writer JsonWriter
The
value object?
The value.
serializer JsonSerializer
The calling serializer.
Remarks
The value is written as the System.Text.Json converter the generator emits writes it: a number or a boolean
as such, and anything else as a string,