Table of Contents

Class JsonHelper

Namespace
Ecng.Serialization
Assembly
Ecng.Serialization.dll

Provides helper methods for JSON serialization and deserialization.

public static class JsonHelper
Inheritance
JsonHelper
Inherited Members

Fields

UTF8NoBom

Provides a UTF-8 encoding without a byte order mark (BOM).

public static readonly Encoding UTF8NoBom

Field Value

Encoding

Methods

ChechExpectedToken(JsonReader, JsonToken)

Checks that the current token of the JSON reader matches the expected token.

[Conditional("DEBUG")]
public static void ChechExpectedToken(this JsonReader reader, JsonToken token)

Parameters

reader JsonReader

The JSON reader.

token JsonToken

The expected JSON token.

CreateJsonSerializerSettings()

Creates and configures a new instance of JsonSerializerSettings.

public static JsonSerializerSettings CreateJsonSerializerSettings()

Returns

JsonSerializerSettings

The configured JsonSerializerSettings instance.

DeserializeObject(JToken, Type)

Deserializes the JSON token into an object of the specified type.

public static object DeserializeObject(this JToken token, Type type)

Parameters

token JToken

The JSON token.

type Type

The target type.

Returns

object

The deserialized object.

DeserializeObject(string, Type)

Deserializes the JSON string into an object of the specified type.

public static object DeserializeObject(this string content, Type type)

Parameters

content string

The JSON string.

type Type

The target type.

Returns

object

The deserialized object.

DeserializeObject<T>(JToken)

Deserializes the JSON token into an object of type T.

public static T DeserializeObject<T>(this JToken token)

Parameters

token JToken

The JSON token.

Returns

T

The deserialized object.

Type Parameters

T

The target type.

DeserializeObject<T>(string)

Deserializes the JSON string into an object of type T.

public static T DeserializeObject<T>(this string content)

Parameters

content string

The JSON string.

Returns

T

The deserialized object.

Type Parameters

T

The target type.

FromJson(string, Type)

Deserializes the JSON string into an object using Newtonsoft.Json.

public static object FromJson(this string json, Type type)

Parameters

json string

The JSON string.

type Type

The target type.

Returns

object

The deserialized object.

ReadWithCheckAsync(JsonReader, CancellationToken)

Asynchronously reads the next token from the JSON reader and checks for end-of-file.

public static Task ReadWithCheckAsync(this JsonReader reader, CancellationToken cancellationToken)

Parameters

reader JsonReader

The JSON reader.

cancellationToken CancellationToken

A cancellation token.

Returns

Task

A task that represents the asynchronous read operation.

SkipBom(byte[])

Skips the Byte Order Mark (BOM) from the beginning of a byte array if present.

public static byte[] SkipBom(this byte[] array)

Parameters

array byte[]

The byte array.

Returns

byte[]

The byte array without the BOM.

ToJson(object, bool)

Serializes the object into a JSON string with optional indentation.

public static string ToJson(this object obj, bool indent = true)

Parameters

obj object

The object to serialize.

indent bool

True to format the JSON string; otherwise, false.

Returns

string

The JSON string.

ToJson(object, bool, JsonSerializerSettings)

Serializes the object into a JSON string with optional indentation and custom serializer settings.

public static string ToJson(this object obj, bool indent, JsonSerializerSettings settings)

Parameters

obj object

The object to serialize.

indent bool

True to format the JSON string; otherwise, false.

settings JsonSerializerSettings

The JSON serializer settings.

Returns

string

The JSON string.