Skip to main content

Meadow.Foundation.Serialization.MicroJson

MicroJson
StatusStatus badge: working
Source codeGitHub
NuGet packageNuGet Gallery for Meadow.Foundation.Serialization.MicroJson

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
Declaration
public static class MicroJson

Methods

Deserialize(string)

Desrializes a Json string into an object.

View Source
Declaration
public static object? Deserialize(string json)
Returns

System.Object: An ArrayList, a Hashtable, a double, a long, a string, null, true, or false

Parameters
TypeName
System.Stringjson

DeserializeString(string)

Deserializes a Json string into an object.

View Source
Declaration
public static object? DeserializeString(string json)
Returns

System.Object: An ArrayList, a Hashtable, a double, a long, a string, null, true, or false

Parameters
TypeName
System.Stringjson

EscapeString(string)

Escapes special characters in a string to ensure it is JSON-compliant.

View Source
Declaration
public static string EscapeString(string value)
Returns

System.String: The escaped string with special characters properly encoded.

Parameters
TypeNameDescription
System.StringvalueThe string to escape.

Serialize(object, DateTimeFormat, bool)

Converts an object to a JSON string.

View Source
Declaration
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
TypeNameDescription
System.ObjectoThe value to convert.
Meadow.Foundation.Serialization.MicroJson.DateTimeFormatdateTimeFormatThe format to use for DateTime values. Defaults to ISO 8601 format.
System.BooleanconvertNamesToCamelCaseTrue 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
Declaration
public static string SerializeString(string input)
Returns

System.String: The serialized JSON string.

Parameters
TypeNameDescription
System.StringinputThe string to serialize.

DeserializeList<T>(ArrayList)

Deserializes a JSON array into a list of objects of type T.

View Source
Declaration
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
TypeNameDescription
System.Collections.ArrayListarrayThe JSON array to deserialize.
Type Parameters
NameDescription
TThe type of objects in the list.

DeserializeArray<T>(ArrayList)

Deserializes a JSON array into an array of objects of type T.

View Source
Declaration
public static T[] DeserializeArray<T>(ArrayList array) where T : new()
Returns

<T>[]: An array of objects of type T.

Parameters
TypeNameDescription
System.Collections.ArrayListarrayThe JSON array to deserialize.
Type Parameters
NameDescription
TThe type of objects in the array.

Deserialize<T>(byte[])

Deserializes an object of type T from a JSON string.

View Source
Declaration
public static T Deserialize<T>(byte[] encodedData)
Returns

<T>: An object of type T.

Parameters
TypeNameDescription
System.Byte[]encodedDataA UTF8-encoded JSON string to deserialize.
Type Parameters
NameDescription
TThe type of object to deserialize.

Deserialize<T>(string)

Deserializes an object of type T from a JSON string.

View Source
Declaration
public static T Deserialize<T>(string json)
Returns

<T>: An object of type T.

Parameters
TypeNameDescription
System.StringjsonThe JSON string to deserialize.
Type Parameters
NameDescription
TThe type of object to deserialize.

Deserialize(string, Type)

Deserializes an object of type T from a JSON string.

View Source
Declaration
public static object Deserialize(string json, Type type)
Returns

System.Object: An object of the specified type

Parameters
TypeNameDescription
System.StringjsonThe JSON string to deserialize.
System.TypetypeThe type of object to deserialize.