JsonHelper

Ecng.Serialization

Provides helper methods for JSON serialization and deserialization.

Methoden

CheckExpectedToken
public static void CheckExpectedToken(JsonReader reader, JsonToken token)
JsonHelper.CheckExpectedToken(reader, token)

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

reader
The JSON reader.
token
The expected JSON token.
CreateJsonSerializerSettings
public static JsonSerializerSettings CreateJsonSerializerSettings()
result = JsonHelper.CreateJsonSerializerSettings()

Creates and configures a new instance of JsonSerializerSettings.

Rückgabe: The configured JsonSerializerSettings instance.

DeserializeObject
public static object DeserializeObject(string content, Type type)
result = JsonHelper.DeserializeObject(content, type)

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

content
The JSON string.
type
The target type.

Rückgabe: The deserialized object.

DeserializeObject
public static object DeserializeObject(JToken token, Type type)
result = JsonHelper.DeserializeObject(token, type)

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

token
The JSON token.
type
The target type.

Rückgabe: The deserialized object.

DeserializeObject``1
public static T DeserializeObject<T>(string content)
result = JsonHelper.DeserializeObject(content)

Deserializes the JSON string into an object of type T.

content
The JSON string.

Rückgabe: The deserialized object.

DeserializeObject``1
public static T DeserializeObject<T>(JToken token)
result = JsonHelper.DeserializeObject(token)

Deserializes the JSON token into an object of type T.

token
The JSON token.

Rückgabe: The deserialized object.

FromJson
public static object FromJson(string json, Type type)
result = JsonHelper.FromJson(json, type)

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

json
The JSON string.
type
The target type.

Rückgabe: The deserialized object.

ReadWithCheckAsync
public static Task ReadWithCheckAsync(JsonReader reader, CancellationToken cancellationToken)
result = JsonHelper.ReadWithCheckAsync(reader, cancellationToken)

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

reader
The JSON reader.
cancellationToken
A cancellation token.

Rückgabe: A task that represents the asynchronous read operation.

SkipBom
public static byte[] SkipBom(byte[] array)
result = JsonHelper.SkipBom(array)

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

array
The byte array.

Rückgabe: The byte array without the BOM.

SkipBom
public static string SkipBom(string str)
result = JsonHelper.SkipBom(str)

Removes a leading Unicode byte order mark (BOM, U+FEFF) from the specified string, if present.

str
The input string (can be null or empty).

Rückgabe: The input string without a leading BOM character; if the string is null, empty, or does not start with a BOM, the original value is returned.

ToJson
public static string ToJson(object obj, bool indent)
result = JsonHelper.ToJson(obj, indent)

Serializes the object into a JSON string with optional indentation.

obj
The object to serialize.
indent
True to format the JSON string; otherwise, false.

Rückgabe: The JSON string.

ToJson
public static string ToJson(object obj, bool indent, JsonSerializerSettings settings)
result = JsonHelper.ToJson(obj, indent, settings)

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

obj
The object to serialize.
indent
True to format the JSON string; otherwise, false.
settings
The JSON serializer settings.

Rückgabe: The JSON string.

Felder

UTF8NoBom
public static readonly Encoding UTF8NoBom
value = JsonHelper.UTF8NoBom

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