Meadow.Foundation.Serialization.MicroJson
MicroJson | |
---|---|
Status | |
Source code | GitHub |
NuGet package |
Code Example
public static void Main(string[] args)
{
var resourceData = LoadResource("menu.json");
var menuJson = new string(System.Text.Encoding.UTF8.GetChars(resourceData));
DeserializeTypeSafe(menuJson);
DeserializeAsHashtable(menuJson);
}
private static void DeserializeTypeSafe(string menuJson)
{
string testJsonItem = "{\"ScreenX\":290,\"ScreenY\":210,\"RawX\":3341,\"RawY\":3353}";
var point = MicroJson.Deserialize<CalibrationPoint>(testJsonItem);
string testJsonArray = "[{\"ScreenX\":30,\"ScreenY\":30,\"RawX\":522,\"RawY\":514},{\"ScreenX\":290,\"ScreenY\":210,\"RawX\":3341,\"RawY\":3353}]";
var points = MicroJson.Deserialize<CalibrationPoint[]>(testJsonArray);
var menu = MicroJson.Deserialize<MenuContainer>(menuJson);
}
private static void DeserializeAsHashtable(string menuJson)
{
var menuData = MicroJson.DeserializeString(menuJson) as Hashtable;
if (menuData["menu"] == null)
{
throw new ArgumentException("JSON root must contain a 'menu' item");
}
Console.WriteLine($"Root element is {menuData["menu"]}");
var items = (ArrayList)menuData["menu"];
foreach (Hashtable item in items)
{
Console.WriteLine($"Found {item["text"]}");
}
}
private static byte[] LoadResource(string filename)
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName = $"MicroJson_Sample.{filename}";
using Stream stream = assembly.GetManifestResourceStream(resourceName);
using var ms = new MemoryStream();
stream.CopyTo(ms);
return ms.ToArray();
}
Sample project(s) available on GitHub
Class MicroJson
JSON Serialization and Deserialization library for .NET
Assembly: MicroJson.dll
View Source
public static class MicroJson
Methods
Deserialize(string)
Desrializes a Json string into an object.
View Source
public static object? Deserialize(string json)
Returns
System.Object
: An ArrayList, a Hashtable, a double, a long, a string, null, true, or false
Parameters
Type | Name |
---|---|
System.String | json |
DeserializeString(string)
Deserializes a Json string into an object.
View Source
public static object? DeserializeString(string json)
Returns
System.Object
: An ArrayList, a Hashtable, a double, a long, a string, null, true, or false
Parameters
Type | Name |
---|---|
System.String | json |
EscapeString(string)
Escapes special characters in a string to ensure it is JSON-compliant.
View Source
public static string EscapeString(string value)
Returns
System.String
: The escaped string with special characters properly encoded.
Parameters
Type | Name | Description |
---|---|---|
System.String | value | The string to escape. |
Serialize(object, DateTimeFormat, bool)
Converts an object to a JSON string.
View Source
public static string? Serialize(object o, MicroJson.DateTimeFormat dateTimeFormat = DateTimeFormat.ISO8601, bool convertNamesToCamelCase = true)
Returns
System.String
: The JSON object as a string or null when the value type is not supported.
Parameters
Type | Name | Description |
---|---|---|
System.Object | o | The value to convert. |
Meadow.Foundation.Serialization.MicroJson.DateTimeFormat | dateTimeFormat | The format to use for DateTime values. Defaults to ISO 8601 format. |
System.Boolean | convertNamesToCamelCase | True to convert all properties to camel case during serialization |
SerializeString(string)
Safely serialize a String into a JSON string value, escaping all backslash and quote characters.
View Source
public static string SerializeString(string input)
Returns
System.String
: The serialized JSON string.
Parameters
Type | Name | Description |
---|---|---|
System.String | input | The string to serialize. |
DeserializeList<T>(ArrayList)
Deserializes a JSON array into a list of objects of type T.
View Source
public static List<T> DeserializeList<T>(ArrayList array) where T : new()
Returns
System.Collections.Generic.List<<T>>
: A list of objects of type T.
Parameters
Type | Name | Description |
---|---|---|
System.Collections.ArrayList | array | The JSON array to deserialize. |
Type Parameters
Name | Description |
---|---|
T | The type of objects in the list. |
DeserializeArray<T>(ArrayList)
Deserializes a JSON array into an array of objects of type T.
View Source
public static T[] DeserializeArray<T>(ArrayList array) where T : new()
Returns
<T>[]
: An array of objects of type T.
Parameters
Type | Name | Description |
---|---|---|
System.Collections.ArrayList | array | The JSON array to deserialize. |
Type Parameters
Name | Description |
---|---|
T | The type of objects in the array. |
Deserialize<T>(byte[])
Deserializes an object of type T from a JSON string.
View Source
public static T Deserialize<T>(byte[] encodedData)
Returns
<T>
: An object of type T.
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | encodedData | A UTF8-encoded JSON string to deserialize. |
Type Parameters
Name | Description |
---|---|
T | The type of object to deserialize. |
Deserialize<T>(string)
Deserializes an object of type T from a JSON string.
View Source
public static T Deserialize<T>(string json)
Returns
<T>
: An object of type T.
Parameters
Type | Name | Description |
---|---|---|
System.String | json | The JSON string to deserialize. |
Type Parameters
Name | Description |
---|---|
T | The type of object to deserialize. |
Deserialize(string, Type)
Deserializes an object of type T from a JSON string.
View Source
public static object Deserialize(string json, Type type)
Returns
System.Object
: An object of the specified type
Parameters
Type | Name | Description |
---|---|---|
System.String | json | The JSON string to deserialize. |
System.Type | type | The type of object to deserialize. |