System.Json Module

The System.Json module contains support for JSON serialization.

The JSON serialization support is implemented in half by the compiler and in half by the Json library. The compiler processes attributes named json and jsonFieldName that are attached to user-defined JSON serializable classes and generates serialization code for them. The Json library contains conversion function overloads that call the compiler generated serialization code.

When a user-defined class has a json attribute, an object of that class can be serialized to a JSON string. Construction of the JSON string from a JSON serializable class is a three-step process. First an object of the class to serialize is created with new and assigned to a UniquePtr . Then a non-member ToJson library function is called with the unique pointer argument. The function returns a unique pointer to a JsonValue abstract class. Finally the JSON string is generated by calling the ToString member function of the JsonValue .

A user-defined class having a json attribute adds also support for parsing an object of that class back from a JSON string. The parsing is a multi-step process. First a unique pointer to a JsonValue is constructed by calling the ParseJson library function with a JSON string. Then empty unique pointer of a user-defined class is constructed. Then the FromJson library function is called with the JsonValue and the constructed user-defined class arguments. This assigns a value to the unique pointer of the user-defined class. Finally the actual type of the object can be queried by is or as operators, casted to the right user-defined type and processed.

Example

This vehicles example program shows how to construct JSON serializable classes, how to serialize them to JSON strings and parse them back from JSON strings.

The SerializeOneVehicle() function of the example program contructs a single JSON serializable Car object, serializes it to a JSON string, parses it back from the JSON string and writes the data of it to console. The program called with argument "1" prints:

 {"class":"Vehicles.Car", "object":{"model":"Porsche", "registrationNumber":"ABC-123", "speed":200}} 
Porsche: ABC-123 200

The SerializeListOfVehicles() function of the example program constructs a List of Vehicle s, serializer them to a JSON string, parses them back from the JSON string and writes their data to console. The program called with no arguments prints:

 [{"class":"Vehicles.Truck", "object":{"model":"Volvo", "registrationNumber":"XYZ-987", "speed":100, "weight":1000}}, 
{"class":"Vehicles.Car", "object":{"model":"Peugeot", "registrationNumber":"PRQ-567", "speed":150}},
{"class":"Vehicles.Bicycle", "object":{"brand":"Pegoretti", "speed":30}}]
Volvo: XYZ-987 100 1000
Peugeot: PRQ-567 150
Pegoretti: 30

Grammars

JSON Lexical Grammar
JSON Grammar

Namespaces

JsonTokens
System
Json

Files

System.Json/Json.cm
System.Json/JsonClassFactory.cm
System.Json/JsonClassMap.cm
System.Json/JsonExport.cm
System.Json/JsonImport.cm
System.Json/JsonLexer.cm
System.Json/JsonLexer.lexer
System.Json/JsonParser.cm
System.Json/JsonParser.parser
System.Json/JsonTokens.cm
System.Json/TokenValueParsers.cm

Classes

public static class JsonClassMap
public class JsonLexer
public static class JsonParser