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
reader
JsonReaderThe JSON reader.
token
JsonTokenThe 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
JTokenThe JSON token.
type
TypeThe 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
token
JTokenThe 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
stringThe 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
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
JsonReaderThe JSON reader.
cancellationToken
CancellationTokenA 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
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
objectThe object to serialize.
indent
boolTrue to format the JSON string; otherwise, false.
settings
JsonSerializerSettingsThe JSON serializer settings.
Returns
- string
The JSON string.