JsonHelper
Provides helper methods for JSON serialization and deserialization.
Methods
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.
public static JsonSerializerSettings CreateJsonSerializerSettings()
result = JsonHelper.CreateJsonSerializerSettings()
Creates and configures a new instance of JsonSerializerSettings.
Returns: The configured JsonSerializerSettings instance.
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.
Returns: The deserialized object.
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.
Returns: The deserialized object.
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.
Returns: The deserialized object.
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.
Returns: The deserialized object.
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.
Returns: The deserialized object.
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.
Returns: A task that represents the asynchronous read operation.
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.
Returns: The byte array without the BOM.
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).
Returns: 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.
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.
Returns: The JSON string.
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.
Returns: The JSON string.
Fields
public static readonly Encoding UTF8NoBom
value = JsonHelper.UTF8NoBom
Provides a UTF-8 encoding without a byte order mark (BOM).