Table of Contents

Struct JsonElementConverter

Namespace
DynaSharp.Serialization
Assembly
DynaSharp.dll

Converts JsonElement to and from the native DynamoDB value that holds the same JSON: an object is a map (M), an array a list (L), a string S, a number N with its digits as written, true and false BOOL, and null NULL. It is the built-in converter of a property of type JsonElement, so a document whose schema belongs to someone else, such as the payload of an event, is stored as a map that filters, projections and updates reach by path.

public readonly struct JsonElementConverter : IDynamoDbConverter<JsonElement>
Implements
Inherited Members

Remarks

A null in an object is left out of the map, so a field that was null reads back missing. A null in an array is NULL, which keeps the positions of the others. A number keeps its text on the way in and back, but DynamoDB stores numbers in a canonical form, so 1.50 reads back as 1.5 and 1E+2 as 100.

A write throws ArgumentException for what DynamoDB would refuse, naming the place in the JSON such as $.lines[2].weight: a number with more than 38 significant digits or a magnitude outside 1E-130 to below 1E+126, a value nested deeper than 32 levels counted from the attribute, an empty name, a name twice in one object, text that is not valid UTF-8 or UTF-16, and a default JsonElement, which holds no JSON. A Nullable<T> property leaves the attribute out for null. The levels are counted from this value: inside a list or a document it stands deeper, and DynamoDB counts that.

A read takes any value: a string set and a number set read as an array, and a binary value and a binary set as the base64 text of the bytes, which a write stores as a string. The element owns its memory, so it outlives the read.

Explicit Interface Implementations

Read(ref DynamoDbReader, AttributeType)

Reads the attribute value the reader is positioned on.

static JsonElement Read(ref DynamoDbReader reader, AttributeType type)

Parameters

reader DynamoDbReader

The 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.

type AttributeType

The 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

JsonElement

The value.

Exceptions

FormatException

A number is not in the form of a JSON number, which DynamoDB never sends.

Write(DynamoDbWriter, JsonElement)

Writes a value as one complete attribute value, for example with WriteNumber<T>(T).

static void Write(DynamoDbWriter writer, JsonElement value)

Parameters

writer DynamoDbWriter

The 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.

value JsonElement

The value.

Exceptions

ArgumentException

DynamoDB cannot store the JSON, see the remarks, or value is a default instance.