Class JsonSerializer
Provides methods for serializing/deserializing .NET value types to/from JSON as well as facilities for converting objects and arrays if appropriate callbacks are supplied to assist with the conversion.
Namespace: FM.LiveSwitch
Assembly: FM.LiveSwitch.dll
Syntax
public class JsonSerializer : object
Methods
DeserializeBoolean(String)
Deserializes a boolean value.
Declaration
public static bool? DeserializeBoolean(string valueJson)
Parameters
Type | Name | Description |
---|---|---|
System.String | valueJson | The boolean JSON to deserialize. |
Returns
Type | Description |
---|---|
System.Nullable<System.Boolean> | The deserialized boolean value. |
DeserializeBooleanArray(String)
Deserializes a boolean array from JSON.
Declaration
public static bool[] DeserializeBooleanArray(string arrayJson)
Parameters
Type | Name | Description |
---|---|---|
System.String | arrayJson | A JSON-serialized boolean array. |
Returns
Type | Description |
---|---|
System.Boolean[] | An array of boolean values. |
DeserializeDecimal(String)
Deserializes a decimal value.
Declaration
public static decimal? DeserializeDecimal(string valueJson)
Parameters
Type | Name | Description |
---|---|---|
System.String | valueJson | The decimal JSON to deserialize. |
Returns
Type | Description |
---|---|
System.Nullable<System.Decimal> | The deserialized decimal value. |
DeserializeDecimalArray(String)
Deserializes a decimal array from JSON.
Declaration
public static decimal[] DeserializeDecimalArray(string arrayJson)
Parameters
Type | Name | Description |
---|---|---|
System.String | arrayJson | A JSON-serialized decimal array. |
Returns
Type | Description |
---|---|
System.Decimal[] | An array of decimal values. |
DeserializeDictionary<T>(String, Function0<Dictionary<String, T>>, Function1<String, T>)
Deserializes a JSON string into a dictionary.
Declaration
public static Dictionary<string, T> DeserializeDictionary<T>(string dictionaryJson, Function0<Dictionary<string, T>> createDictionaryCallback, Function1<string, T> deserializeValueCallback)
Parameters
Type | Name | Description |
---|---|---|
System.String | dictionaryJson | The JSON-encoded dictionary. |
Function0<Dictionary<System.String, T>> | createDictionaryCallback | The callback that creates a blank dictionary. |
Function1<System.String, T> | deserializeValueCallback | The callback that deserializes a value. |
Returns
Type | Description |
---|---|
Dictionary<System.String, T> | The deserialized dictionary. |
Type Parameters
Name | Description |
---|---|
T | The type of value stored in the dictionary. |
DeserializeDouble(String)
Deserializes a double value.
Declaration
public static double? DeserializeDouble(string valueJson)
Parameters
Type | Name | Description |
---|---|---|
System.String | valueJson | The double JSON to deserialize. |
Returns
Type | Description |
---|---|
System.Nullable<System.Double> | The deserialized double value. |
DeserializeDoubleArray(String)
Deserializes a double array from JSON.
Declaration
public static double[] DeserializeDoubleArray(string arrayJson)
Parameters
Type | Name | Description |
---|---|---|
System.String | arrayJson | A JSON-serialized double array. |
Returns
Type | Description |
---|---|
System.Double[] | An array of double values. |
DeserializeFloat(String)
Deserializes a float value.
Declaration
public static float? DeserializeFloat(string valueJson)
Parameters
Type | Name | Description |
---|---|---|
System.String | valueJson | The float JSON to deserialize. |
Returns
Type | Description |
---|---|
System.Nullable<System.Single> | The deserialized float value. |
DeserializeFloatArray(String)
Deserializes a float array from JSON.
Declaration
public static float[] DeserializeFloatArray(string arrayJson)
Parameters
Type | Name | Description |
---|---|---|
System.String | arrayJson | A JSON-serialized float array. |
Returns
Type | Description |
---|---|
System.Single[] | An array of float values. |
DeserializeGuid(String)
Deserializes a globally unique identifier.
Declaration
public static Guid? DeserializeGuid(string valueJson)
Parameters
Type | Name | Description |
---|---|---|
System.String | valueJson | The GUID JSON to deserialize. |
Returns
Type | Description |
---|---|
System.Nullable<Guid> | The deserialized GUID. |
DeserializeGuidArray(String)
Deserializes a GUID array from JSON.
Declaration
public static Guid[] DeserializeGuidArray(string arrayJson)
Parameters
Type | Name | Description |
---|---|---|
System.String | arrayJson | A JSON-serialized GUID array. |
Returns
Type | Description |
---|---|
Guid[] | An array of GUID values. |
DeserializeInteger(String)
Deserializes an integer value.
Declaration
public static int? DeserializeInteger(string valueJson)
Parameters
Type | Name | Description |
---|---|---|
System.String | valueJson | The integer JSON to deserialize. |
Returns
Type | Description |
---|---|
System.Nullable<System.Int32> | The deserialized integer value. |
DeserializeIntegerArray(String)
Deserializes a integer array from JSON.
Declaration
public static int[] DeserializeIntegerArray(string arrayJson)
Parameters
Type | Name | Description |
---|---|---|
System.String | arrayJson | A JSON-serialized integer array. |
Returns
Type | Description |
---|---|
System.Int32[] | An array of integer values. |
DeserializeLong(String)
Deserializes a long value.
Declaration
public static long? DeserializeLong(string valueJson)
Parameters
Type | Name | Description |
---|---|---|
System.String | valueJson | The long JSON to deserialize. |
Returns
Type | Description |
---|---|
System.Nullable<System.Int64> | The deserialized long value. |
DeserializeLongArray(String)
Deserializes a long array from JSON.
Declaration
public static long[] DeserializeLongArray(string arrayJson)
Parameters
Type | Name | Description |
---|---|---|
System.String | arrayJson | A JSON-serialized long array. |
Returns
Type | Description |
---|---|
System.Int64[] | An array of long values. |
DeserializeObject<T>(String, Function0<T>, Action3<T, String, String>)
Deserializes a JSON string into a target object type.
Declaration
public static T DeserializeObject<T>(string json, Function0<T> creator, Action3<T, string, string> callback)
Parameters
Type | Name | Description |
---|---|---|
System.String | json | The JSON-encoded string. |
Function0<T> | creator | The method used for creating a new object. |
Action3<T, System.String, System.String> | callback | The method used for deserializing a property. |
Returns
Type | Description |
---|---|
T | The deserialized object. |
Type Parameters
Name | Description |
---|---|
T | The type of the object to deserialize. |
DeserializeObjectArray<T>(String, Function1<String, T>)
Deserializes a JSON string into an array of target object types.
Declaration
public static List<T> DeserializeObjectArray<T>(string json, Function1<string, T> deserializer)
Parameters
Type | Name | Description |
---|---|---|
System.String | json | The JSON-encoded string. |
Function1<System.String, T> | deserializer | The callback used to deserialize each item in the array. |
Returns
Type | Description |
---|---|
List<T> | An array of deserialized objects. |
Type Parameters
Name | Description |
---|---|
T | The type of the object to deserialize. |
DeserializeObjectFast<T>(String, Function0<T>, Action3<T, String, String>)
Deserializes a JSON string into a Serializable target object type.
Declaration
public static T DeserializeObjectFast<T>(string json, Function0<T> creator, Action3<T, string, string> callback)
where T : Serializable
Parameters
Type | Name | Description |
---|---|---|
System.String | json | The JSON-encoded string. |
Function0<T> | creator | The method used for creating a new object. |
Action3<T, System.String, System.String> | callback | The method used for deserializing a property. |
Returns
Type | Description |
---|---|
T | The deserialized object. |
Type Parameters
Name | Description |
---|---|
T | The type of the object to deserialize. |
DeserializeRaw(String)
Deserializes a piece of raw JSON.
Declaration
public static string DeserializeRaw(string dataJson)
Parameters
Type | Name | Description |
---|---|---|
System.String | dataJson | The raw data. |
Returns
Type | Description |
---|---|
System.String | The deserialized data. |
DeserializeRawArray(String)
Deserializes a raw array from JSON.
Declaration
public static List<string> DeserializeRawArray(string json)
Parameters
Type | Name | Description |
---|---|---|
System.String | json | A JSON-serialized raw array. |
Returns
Type | Description |
---|---|
List<System.String> | An array of raw values. |
DeserializeString(String)
Deserializes a string.
Declaration
public static string DeserializeString(string valueJson)
Parameters
Type | Name | Description |
---|---|---|
System.String | valueJson | The string to deserialize. |
Returns
Type | Description |
---|---|
System.String | The deserialized string value. |
DeserializeStringArray(String)
Deserializes a simple string array from JSON.
Declaration
public static string[] DeserializeStringArray(string arrayJson)
Parameters
Type | Name | Description |
---|---|---|
System.String | arrayJson | A JSON-serialized string array. |
Returns
Type | Description |
---|---|
System.String[] | An array of string values. |
EscapeString(String)
Escapes any special characters in a string.
Declaration
public static string EscapeString(string text)
Parameters
Type | Name | Description |
---|---|---|
System.String | text | The string without escaped characters. |
Returns
Type | Description |
---|---|
System.String | The escaped string. |
IsValidJson(String)
Determines whether the specified JSON string is valid.
Declaration
public static bool IsValidJson(string json)
Parameters
Type | Name | Description |
---|---|---|
System.String | json | The JSON string to validate. |
Returns
Type | Description |
---|---|
System.Boolean | True if the JSON string is valid; false otherwise. |
SerializeBoolean(Nullable<Boolean>)
Serializes a boolean value.
Declaration
public static string SerializeBoolean(bool? value)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.Boolean> | value | The boolean to serialize. |
Returns
Type | Description |
---|---|
System.String | The serialized boolean value. |
SerializeBooleanArray(Boolean[])
Serializes a boolean array to JSON.
Declaration
public static string SerializeBooleanArray(bool[] array)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean[] | array | An array of boolean values. |
Returns
Type | Description |
---|---|
System.String | A JSON-serialized boolean array. |
SerializeDecimal(Nullable<Decimal>)
Serializes a decimal value.
Declaration
public static string SerializeDecimal(decimal? value)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.Decimal> | value | The decimal to serialize. |
Returns
Type | Description |
---|---|
System.String | The serialized decimal value. |
SerializeDecimalArray(Decimal[])
Serializes a decimal array to JSON.
Declaration
public static string SerializeDecimalArray(decimal[] array)
Parameters
Type | Name | Description |
---|---|---|
System.Decimal[] | array | An array of decimal values. |
Returns
Type | Description |
---|---|
System.String | A JSON-serialized decimal array. |
SerializeDictionary<T>(Dictionary<String, T>, Function1<T, String>)
Serializes a dictionary into a JSON string.
Declaration
public static string SerializeDictionary<T>(Dictionary<string, T> dictionary, Function1<T, string> serializeValueCallback)
Parameters
Type | Name | Description |
---|---|---|
Dictionary<System.String, T> | dictionary | The dictionary being serialized. |
Function1<T, System.String> | serializeValueCallback | The callback that serializes a value. |
Returns
Type | Description |
---|---|
System.String | The dictionary as a JSON string. |
Type Parameters
Name | Description |
---|---|
T | The type of value stored in the dictionary. |
SerializeDouble(Nullable<Double>)
Serializes a double value.
Declaration
public static string SerializeDouble(double? value)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.Double> | value | The double to serialize. |
Returns
Type | Description |
---|---|
System.String | The serialized double value. |
SerializeDoubleArray(Double[])
Serializes a double array to JSON.
Declaration
public static string SerializeDoubleArray(double[] array)
Parameters
Type | Name | Description |
---|---|---|
System.Double[] | array | An array of double values. |
Returns
Type | Description |
---|---|
System.String | A JSON-serialized double array. |
SerializeFloat(Nullable<Single>)
Serializes a float value.
Declaration
public static string SerializeFloat(float? value)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.Single> | value | The float to serialize. |
Returns
Type | Description |
---|---|
System.String | The serialized float value. |
SerializeFloatArray(Single[])
Serializes a float array to JSON.
Declaration
public static string SerializeFloatArray(float[] array)
Parameters
Type | Name | Description |
---|---|---|
System.Single[] | array | An array of float values. |
Returns
Type | Description |
---|---|
System.String | A JSON-serialized float array. |
SerializeGuid(Nullable<Guid>)
Serializes a globally unique identifier.
Declaration
public static string SerializeGuid(Guid? value)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<Guid> | value | The GUID to serialize. |
Returns
Type | Description |
---|---|
System.String | The serialized GUID. |
SerializeGuidArray(Guid[])
Serializes a GUID array to JSON.
Declaration
public static string SerializeGuidArray(Guid[] array)
Parameters
Type | Name | Description |
---|---|---|
Guid[] | array | An array of GUID values. |
Returns
Type | Description |
---|---|
System.String | A JSON-serialized GUID array. |
SerializeInteger(Nullable<Int32>)
Serializes an integer value.
Declaration
public static string SerializeInteger(int? value)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.Int32> | value | The integer to serialize. |
Returns
Type | Description |
---|---|
System.String | The serialized integer value. |
SerializeIntegerArray(Int32[])
Serializes a integer array to JSON.
Declaration
public static string SerializeIntegerArray(int[] array)
Parameters
Type | Name | Description |
---|---|---|
System.Int32[] | array | An array of integer values. |
Returns
Type | Description |
---|---|
System.String | A JSON-serialized integer array. |
SerializeLong(Nullable<Int64>)
Serializes a long value.
Declaration
public static string SerializeLong(long? value)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.Int64> | value | The long to serialize. |
Returns
Type | Description |
---|---|
System.String | The serialized long value. |
SerializeLongArray(Int64[])
Serializes a long array to JSON.
Declaration
public static string SerializeLongArray(long[] array)
Parameters
Type | Name | Description |
---|---|---|
System.Int64[] | array | An array of long values. |
Returns
Type | Description |
---|---|
System.String | A JSON-serialized long array. |
SerializeObject<T>(T, Action2<T, Dictionary<String, String>>)
Serializes an object into a JSON string.
Declaration
public static string SerializeObject<T>(T source, Action2<T, Dictionary<string, string>> callback)
Parameters
Type | Name | Description |
---|---|---|
T | source | The object being serialized. |
Action2<T, Dictionary<System.String, System.String>> | callback | The method used for serializing properties. |
Returns
Type | Description |
---|---|
System.String | The object as a JSON string. |
Type Parameters
Name | Description |
---|---|
T | The type of the object to serialize. |
SerializeObjectArray<T>(T[], Function1<T, String>)
Serializes an object array into a JSON string.
Declaration
public static string SerializeObjectArray<T>(T[] objects, Function1<T, string> serializer)
Parameters
Type | Name | Description |
---|---|---|
T[] | objects | The object array being serialized. |
Function1<T, System.String> | serializer | The callback used to serialize each item in the array. |
Returns
Type | Description |
---|---|
System.String | The object array as a JSON string. |
Type Parameters
Name | Description |
---|---|
T | The type of the object to serialize. |
SerializeObjectFast<T>(T, Action2<T, Dictionary<String, String>>)
Serializes a Serializable object into a JSON string.
Declaration
public static string SerializeObjectFast<T>(T source, Action2<T, Dictionary<string, string>> callback)
where T : Serializable
Parameters
Type | Name | Description |
---|---|---|
T | source | The object being serialized. |
Action2<T, Dictionary<System.String, System.String>> | callback | The method used for serializing properties. |
Returns
Type | Description |
---|---|
System.String | The object as a JSON string. |
Type Parameters
Name | Description |
---|---|
T | The type of the object to serialize. |
SerializeRaw(String)
Serializes a piece of raw JSON.
Declaration
public static string SerializeRaw(string dataJson)
Parameters
Type | Name | Description |
---|---|---|
System.String | dataJson | The raw data. |
Returns
Type | Description |
---|---|
System.String | The serialized data. |
SerializeRawArray(String[])
Serializes a raw array to JSON.
Declaration
public static string SerializeRawArray(string[] jsons)
Parameters
Type | Name | Description |
---|---|---|
System.String[] | jsons | An array of raw values. |
Returns
Type | Description |
---|---|
System.String | A JSON-serialized raw array. |
SerializeString(String)
Serializes a string.
Declaration
public static string SerializeString(string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The string to serialize. |
Returns
Type | Description |
---|---|
System.String | The serialized string value. |
SerializeStringArray(String[])
Serializes a string array to JSON.
Declaration
public static string SerializeStringArray(string[] array)
Parameters
Type | Name | Description |
---|---|---|
System.String[] | array | An array of string values. |
Returns
Type | Description |
---|---|
System.String | A JSON-serialized string array. |
TrimQuotes(String)
Trims the quotes from a JavaScript string value.
Declaration
public static string TrimQuotes(string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The JavaScript string value. |
Returns
Type | Description |
---|---|
System.String | The string without quotes. |
UnescapeString(String)
Unescapes any special characters from a string.
Declaration
public static string UnescapeString(string text)
Parameters
Type | Name | Description |
---|---|---|
System.String | text | The string with escaped characters. |
Returns
Type | Description |
---|---|
System.String | The unescaped string. |