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
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
readerJsonReaderThe JSON reader.
tokenJsonTokenThe 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
tokenJTokenThe JSON token.
typeTypeThe 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
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
tokenJTokenThe JSON token.
Returns
- T
The deserialized object.
Type Parameters
TThe target type.
DeserializeObject<T>(string)
Deserializes the JSON string into an object of type T.
public static T DeserializeObject<T>(this string content)
Parameters
contentstringThe JSON string.
Returns
- T
The deserialized object.
Type Parameters
TThe 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
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
readerJsonReaderThe JSON reader.
cancellationTokenCancellationTokenA 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
arraybyte[]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
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
objobjectThe object to serialize.
indentboolTrue to format the JSON string; otherwise, false.
settingsJsonSerializerSettingsThe JSON serializer settings.
Returns
- string
The JSON string.