Class NullableFractionJsonConverter
- Namespace
- DatabentoDotNet.Reference.Json
- Assembly
- DatabentoDotNet.Reference.dll
public sealed class NullableFractionJsonConverter : JsonConverter<Fraction?>
- Inheritance
-
NullableFractionJsonConverter
- Inherited Members
Remarks
Fraction and PaymentType are the only two of the nine char-coded reference
enums with a second converter, and the dictionary is why. The FRACCD and
FRACTIONS groups of corporate_actions.list_enums each carry an entry with a null
code and the description "A Blank value is possible"; the other six groups do not. Upstream
agrees by declaring the field Option<Fraction> (corporate.rs:373). So a
blank is a value here rather than a malformed response, and it maps to null.
The empty string is the case that needs this converter; JSON null is not.
System.Text.Json answers a null token for a Nullable<T> itself,
without reaching the underlying converter — so a Fraction? field carrying only
FractionJsonConverter would already read null as no value, and would then
throw on "", which is the same absence spelled differently. Verified against
System.Text.Json rather than assumed.
HandleNull is true, and it is stated rather than
required. System.Text.Json derives the property from whether
default(T) is null, so it comes out false here — a
Nullable<T> converter is not handed the null token by default, and the
framework answers null for it. That is the same answer this converter gives,
so the shipped behaviour is identical with the override and without it; probed rather than
reasoned about, and note that the nine non-nullable converters get the opposite default for the
same rule. It is written down anyway because reading a blank as no value is this file's
decision rather than the framework's, and it should stay this file's decision if that default
ever moves.
This one has to be named on the property, not on the type. A [JsonConverter]
attribute on Fraction can only name one converter, and that is the non-nullable
FractionJsonConverter. The model field this exists for is
Fraction, declared Fraction? with
[JsonConverter(typeof(NullableFractionJsonConverter))] on the property. The source
generator does honour a property-level attribute, so registering this converter in
Internal/CorporateActionsJson.cs's options as well would be duplication rather than
belt-and-braces — established by a test in which a "" reads as
null, which is the assertion that would fail if
FractionJsonConverter were reached instead.
Properties
HandleNull
true, so a JSON null is a value this converter answers rather than
one System.Text.Json answers on its own. Both give null; see
the type's remarks for why the override is kept regardless.
public override bool HandleNull { get; }
Property Value
Methods
Read(ref Utf8JsonReader, Type, JsonSerializerOptions)
Reads and converts the JSON to type Fraction?.
public override Fraction? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
Parameters
readerUtf8JsonReaderThe reader.
typeToConvertTypeThe type to convert.
optionsJsonSerializerOptionsAn object that specifies serialization options to use.
Returns
- Fraction?
The converted value.
Write(Utf8JsonWriter, Fraction?, JsonSerializerOptions)
Writes a specified value as JSON.
public override void Write(Utf8JsonWriter writer, Fraction? value, JsonSerializerOptions options)
Parameters
writerUtf8JsonWriterThe writer to write to.
valueFraction?The value to convert to JSON.
optionsJsonSerializerOptionsAn object that specifies serialization options to use.