Struct NumberConverter<T>
- Namespace
- DynaSharp.Serialization
- Assembly
- DynaSharp.dll
Converts any .NET number type to and from a number value (N): integers, decimal, double,
float, Half, Int128, UInt128 and BigInteger.
public readonly struct NumberConverter<T> : IDynamoDbConverter<T>, IAddableConverter where T : INumberBase<T>
Type Parameters
TThe number type.
- Implements
- Inherited Members
Remarks
Numbers travel as invariant text in the shortest form that round-trips, so a decimal keeps every digit and its scale
(12.50 is written as 12.50). DynamoDB normalizes what it stores, 12.50 comes back as 12.5, so compare numbers
by value, not by text. DynamoDB keeps at most 38 significant digits and rejects magnitudes outside 1E-130 to 9.99E+125 with a validation
error: a double or a BigInteger can be outside that range, and the converter does not check.
Exponent notation is accepted, also for integer types, as long as
the value is integral (1E+2 reads as 100).
DynamoDB holds 38 significant digits and a decimal 28 to 29, with at most 28 of them after the point. A read of a decimal returns the exact value or
throws: it never rounds, and 1E-29 does not turn into 0. Trailing zeros and exponents that keep the value are fine (1.10, 1E+2, 0E+5).
A double, a float or a Half rounds to the nearest value it holds, which is what a binary float is, and a number below its range reads as 0. A number
that rounds to infinity throws, and so does the text NaN, which DynamoDB does not store.
A char counts as a number type in .NET but not here: it is stored as a string value of one character, and read back from one.
Explicit Interface Implementations
Read(ref DynamoDbReader, AttributeType)
Reads the attribute value the reader is positioned on.
static T Read(ref DynamoDbReader reader, AttributeType type)
Parameters
readerDynamoDbReaderThe reader, positioned on the value after ReadStartAttribute() or TryReadStartAttribute(out AttributeType). The caller reads the closing brace afterwards, so the converter must read the value completely (all elements of a list, map or set) and nothing more.
typeAttributeTypeThe DynamoDB type of the value. Check it with Expect(AttributeType, AttributeType), which throws JsonException for types the converter does not support. Null is passed to the converter too, so it can map it to null where that makes sense.
Returns
- T
The value.
Exceptions
- JsonException
typeis not Number.- OverflowException
The number does not fit
T: it is out of range, has a fractional part andTis an integer type, orTis decimal and cannot hold its exact value. A floating-point type counts a number that rounds to infinity, andNaN, as out of range. The message does not quote the number.
Write(DynamoDbWriter, T)
Writes a value as one complete attribute value, for example with WriteNumber<T>(T).
static void Write(DynamoDbWriter writer, T value)
Parameters
writerDynamoDbWriterThe writer, positioned where a value is expected (after an attribute name, or inside a list). It is a ref struct, so it is never null and needs no check.
valueTThe value.
Exceptions
- ArgumentOutOfRangeException
valueis NaN or infinite; DynamoDB cannot store either.