Table of Contents

Struct BoolAsNumberConverter

Namespace
DynaSharp.Serialization
Assembly
DynaSharp.dll

Converts bool to and from a number value (N): true as {"N":"1"} and false as {"N":"0"}, the form DynamoDBContext of the AWS SDK for .NET stores in its default conversion, V1. It is opt-in, with [DynamoDbConverter] on the property or [assembly: DynamoDbDefaultConverter(typeof(bool), typeof(BoolAsNumberConverter))], since a bool is a boolean value (BOOL) by default.

public readonly struct BoolAsNumberConverter : IDynamoDbConverter<bool>
Implements
Inherited Members

Remarks

Reading takes zero, in any spelling, as false and any other number as true, as the SDK does with what DynamoDB returns: {"N":"2"} reads as true. A boolean value (BOOL) is rejected like any other type, so a table that holds both needs a converter of its own.

Explicit Interface Implementations

Read(ref DynamoDbReader, AttributeType)

Reads the attribute value the reader is positioned on.

static bool 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

bool

The value.

Exceptions

JsonException

type is not Number.

OverflowException

The text is not a number, or one beyond the range of double, which DynamoDB never returns. The message does not quote the text.

Write(DynamoDbWriter, bool)

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

static void Write(DynamoDbWriter writer, bool 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 bool

The value.