Class NullablePaymentTypeJsonConverter
- Namespace
- DatabentoDotNet.Reference.Json
- Assembly
- DatabentoDotNet.Reference.dll
Reads and writes a PaymentType whose wire code may be blank, mapping the blank to null.
public sealed class NullablePaymentTypeJsonConverter : JsonConverter<PaymentType?>
- Inheritance
-
NullablePaymentTypeJsonConverter
- Inherited Members
Remarks
PaymentType and Fraction are the only two of the nine char-coded reference
enums with a second converter, and the dictionary is why. The PAYTYPE group of
corporate_actions.list_enums carries 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<PaymentType> (corporate.rs:358). 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 PaymentType? field carrying only
PaymentTypeJsonConverter 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 PaymentType can only name one converter, and that is the non-nullable
PaymentTypeJsonConverter. The model field this exists for is
PaymentType, declared PaymentType? with
[JsonConverter(typeof(NullablePaymentTypeJsonConverter))] 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
PaymentTypeJsonConverter 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 PaymentType?.
public override PaymentType? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
Parameters
readerUtf8JsonReaderThe reader.
typeToConvertTypeThe type to convert.
optionsJsonSerializerOptionsAn object that specifies serialization options to use.
Returns
- PaymentType?
The converted value.
Write(Utf8JsonWriter, PaymentType?, JsonSerializerOptions)
Writes a specified value as JSON.
public override void Write(Utf8JsonWriter writer, PaymentType? value, JsonSerializerOptions options)
Parameters
writerUtf8JsonWriterThe writer to write to.
valuePaymentType?The value to convert to JSON.
optionsJsonSerializerOptionsAn object that specifies serialization options to use.