Utilizing ChakraCore as a JSON Decoder and Encoder
고남현 @gnh1201@hackers.pub
The Inherent Awkwardness Between Programming Languages and JSON Format
JSON (JavaScript Object Notation) is a universal data exchange format used across programming languages. However, except for JavaScript (ECMAScript) engines, most programming languages weren't originally designed with JSON in mind, resulting in inherent awkwardness when supporting JSON that can never be completely eliminated.
Nevertheless, JSON is widely used across programming languages because excellent libraries have been developed that minimize this awkwardness as much as possible, or because programming language runtimes themselves have made efforts to officially support JSON with impressive results.
This article discusses my approach to addressing this awkwardness. While I focus on a specific platform case, similar perspectives can be applied in other environments.
JSON Input/Output Considerations in .NET Environment
In Microsoft's .NET programming environment (C# or languages supporting the .NET runtime), JSON support typically uses the following standard libraries:
- System.Text.Json
- System.Text.Json.Serialization
- Newtonsoft.Json
However, this project required a reasonable method for JSON input/output without using these standard libraries.
The following considerations influenced this decision:
- The space required for JSON input/output support (string processing, marshaling processes like reflection, etc.) is larger than expected. (Many separate binaries need to be loaded.) With this space requirement, we could include an entire embedded JavaScript engine with proven stability in JavaScript syntax processing and still have similar or even less space usage.
- When applying a simple public implementation for JSON input/output to improve space efficiency, or directly implementing JSON parsing and serialization processes, it's difficult to ensure reliability in JSON processing.
- Although the current project language isn't JavaScript, since JSON format is based on JavaScript syntax, we wanted to maintain a JavaScript-style approach to processing and accessing JSON format as much as possible. The standard libraries definitely don't follow JavaScript style.
- We needed an approach that could potentially eliminate duplicate implementations for various requirements based on or influenced by JavaScript syntax (JSON being one of them).
To address these concerns, we decided to apply ChakraCore + native calls (P/Invoke).
Applying ChakraCore Engine for JSON Parsing
These considerations ultimately led to the decision to create a JSON input/output class using the ChakraCore engine.
The actual implementation can be found in JsSerializer.cs (WelsonJS.Toolkit):
Once implemented, JSON input/output can be performed as follows:
// Serialization (Encoding)
using (var ser = new JsSerializer())
{
var doc = new Dictionary<string, object>
{
["ok"] = true,
["hex"] = hex,
["name"] = name, // may be null → serializer will emit "name": null
["rgb"] = new Dictionary<string, object>
{
["r"] = color.R,
["g"] = color.G,
["b"] = color.B
}
};
string json = ser.Serialize(doc, 0);
Console.WriteLine(json);
}
// Parsing (Decoding)
using (var ser = new JsSerializer())
{
string imageJson, xJson, yJson;
using (var ser = new JsSerializer())
{
int id = ser.Load(body);
// Required: "image" as JSON string
imageJson = ser.ExtractFrom(id, "image");
// Required: "point": { "x": <num>, "y": <num> }
xJson = ser.ExtractFrom(id, "point", "x");
yJson = ser.ExtractFrom(id, "point", "y");
Console.WriteLine(xJson);
Console.WriteLine(yJson);
ser.Unload(id);
}
}
This approach has become similar to how XML format is processed in the traditional .NET programming environment, while also moving closer to JavaScript-style JSON input/output methods.
Application in Other Environments
In other programming environments with similar concerns related to JSON format, similar methods could be used to handle JSON input/output processing.
What is ChakraCore?
ChakraCore is a JavaScript engine developed by Microsoft and distributed as open source. It was originally developed for Microsoft's next-generation web browser (early versions of Microsoft Edge). However, after Microsoft discontinued its own web browser development, ChakraCore has focused on embedded use cases, leveraging its characteristics as a lightweight engine supporting modern syntax.