Table of Contents

Class BaseJsonSerializer2

Namespace
Geotab.Serialization
Assembly
Geotab.Checkmate.ObjectModel.dll

Converts an object to a JSON string and back.

public class BaseJsonSerializer2
Inheritance
BaseJsonSerializer2
Derived
Inherited Members

Constructors

BaseJsonSerializer2()

Initializes a new instance of the BaseJsonSerializer2 class.

public BaseJsonSerializer2()

BaseJsonSerializer2(IEnumerable<JsonConverter>?, IEnumerable<JsonConverter>?, bool)

Initializes a new instance of the BaseJsonSerializer2 class.

protected BaseJsonSerializer2(IEnumerable<JsonConverter>? writeCapableConverters, IEnumerable<JsonConverter>? readCapableConverters, bool prettyPrint = false)

Parameters

writeCapableConverters IEnumerable<JsonConverter>

The additional converters.

readCapableConverters IEnumerable<JsonConverter>

Converts which are capable of reading.

prettyPrint bool

If true the JSON string will be human readable.

Fields

MaxDepth

Maximum allowed depth.

public const int MaxDepth = 100

Field Value

int

Properties

DateTimeResolution

Gets the resolution that DateTimes will be serialized to.

protected Iso8601Resolution DateTimeResolution { get; }

Property Value

Iso8601Resolution

The resolution that DateTimes will be serialized to.

Instance

Gets the singleton instance so contract resolvers are reused.

public static BaseJsonSerializer2 Instance { get; }

Property Value

BaseJsonSerializer2

The singleton instance so contract resolvers are reused.

MemoryGuardCheck

Gets or sets the method that keeps track of work done, initialized by MemoryGuard.

public static Action<long>? MemoryGuardCheck { get; set; }

Property Value

Action<long>

Action

ReportAllocations

Gets the method that keeps track of new object instances of allocated memory.

protected Action<long>? ReportAllocations { get; }

Property Value

Action<long>

The method that keeps track of new object instances of allocated memory.

Remarks

The default implementation is the value of MemoryGuardCheck.

Methods

JsonObjectToObject(object?, Type)

Converts a JsonElement to an object of the provided Type.

public object? JsonObjectToObject(object? jsonObject, Type type)

Parameters

jsonObject object

The object to convert to the concrete type.

type Type

The Type of object to deserialize to.

Returns

object

An object of the provided Type.

JsonObjectToObject<T>(object)

Converts a JsonElement to an object of the provided Type.

public T? JsonObjectToObject<T>(object jsonObject)

Parameters

jsonObject object

The object to convert to the concrete type.

Returns

T

An object of the provided Type.

Type Parameters

T

The type to convert to and return.

JsonToObject(Stream, Type)

Converts a JSON string to an object of the provided Type.

public object? JsonToObject(Stream stream, Type type)

Parameters

stream Stream

The Stream with JSON to convert.

type Type

The Type of object to deserialize to.

Returns

object

An object of the provided Type.

JsonToObject(string, Type)

Converts a JSON string to an object of the provided Type.

public object? JsonToObject(string json, Type type)

Parameters

json string

The JSON to convert.

type Type

The Type of object to deserialize to.

Returns

object

An object of the provided Type.

JsonToObjectAsync(Stream, Type, CancellationToken)

Converts a JSON string to an object of the provided Type.

public Task<object?> JsonToObjectAsync(Stream stream, Type type, CancellationToken cancellationToken = default)

Parameters

stream Stream

The Stream with JSON to convert.

type Type

The Type of object to deserialize to.

cancellationToken CancellationToken

The CancellationToken.

Returns

Task<object>

An object of the provided Type.

JsonToObjectAsync<T>(Stream, CancellationToken)

Converts a JSON string to an object of the provided Type.

public Task<T?> JsonToObjectAsync<T>(Stream stream, CancellationToken cancellationToken = default)

Parameters

stream Stream

The Stream with JSON to convert.

cancellationToken CancellationToken

The CancellationToken.

Returns

Task<T>

An object of the provided Type.

Type Parameters

T

The type to convert to and return.

JsonToObject<T>(string)

Converts a JSON string to an object of type.

public T? JsonToObject<T>(string json) where T : class

Parameters

json string

The JSON to convert.

Returns

T

An object of the provided Type.

Type Parameters

T

The type of object to create.

Merge(string, string)

Merges two JSON string to a single JSON string.

public string Merge(string originalJson, string newContent)

Parameters

originalJson string

The original JSON structure to copy to.

newContent string

The new JSON structure to copy from.

Returns

string

The merged JSON as string.

Exceptions

InvalidOperationException

Throws invalid operation exception if the types content cannot be merged.

ObjectToJson(Utf8JsonWriter, object)

Converts an object to a JSON string and writes it to the provided Utf8JsonWriter.

public void ObjectToJson(Utf8JsonWriter writer, object obj)

Parameters

writer Utf8JsonWriter

The Utf8JsonWriter to write the JSON to.

obj object

The object to convert.

ObjectToJsonAsync<T>(Stream, T, CancellationToken)

Converts an object to a JSON string and writes it to the provided Stream.

public Task ObjectToJsonAsync<T>(Stream stream, T obj, CancellationToken cancellationToken = default)

Parameters

stream Stream

The Stream to write the JSON to.

obj T

The object to convert.

cancellationToken CancellationToken

The cancellation token.

Returns

Task

The Task.

Type Parameters

T

The type to convert to a stream.

ObjectToJson<T>(T)

Converts an object to a JSON string.

public string ObjectToJson<T>(T obj)

Parameters

obj T

The object to convert.

Returns

string

A JSON string.

Type Parameters

T

The type of object to create.