Table of Contents

Class BooleanOrStringConverter

Namespace
Cloudflare.NET.Core.Json
Assembly
Cloudflare.NET.dll

Custom JSON converter for boolean values that can also be represented as strings.

Cloudflare's v2 audit logs API returns the action result as a string ("success"/"failure") while the v1 API returns it as a boolean. This converter handles both formats.

public sealed class BooleanOrStringConverter : JsonConverter<bool>
Inheritance
BooleanOrStringConverter
Inherited Members

Remarks

  • true / false - Boolean values
  • "success" / "true" / "1" - String values mapped to true
  • "failure" / "false" / "0" - String values mapped to false
  • "" (empty string) or null - Mapped to false (unknown/indeterminate result)

Methods

Read(ref Utf8JsonReader, Type, JsonSerializerOptions)

Reads and converts the JSON to type bool.

public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)

Parameters

reader Utf8JsonReader

The reader.

typeToConvert Type

The type to convert.

options JsonSerializerOptions

An object that specifies serialization options to use.

Returns

bool

The converted value.

Write(Utf8JsonWriter, bool, JsonSerializerOptions)

Writes a specified value as JSON.

public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)

Parameters

writer Utf8JsonWriter

The writer to write to.

value bool

The value to convert to JSON.

options JsonSerializerOptions

An object that specifies serialization options to use.