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 could be applied in other environments.
JSON Input/Output Considerations in the .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.
This decision was based on the following considerations:
- The space required for internal implementation of JSON I/O (string processing, marshaling processes like reflection, etc.) is larger than expected (requiring many additional binaries to load). With this much required space, we could include an entire embedded JavaScript engine with proven stability in JavaScript syntax processing and still have similar or even less space usage.
- If we apply a simple public implementation for JSON I/O to improve space efficiency, or implement the JSON parser and serialization process directly, 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 want to maintain a JavaScript style in processing and accessing JSON format as much as possible. The standard libraries definitely don't follow JavaScript style.
- We need an approach that can 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 the ChakraCore Engine for JSON Parsing
These considerations ultimately led to the decision to create a JSON I/O class using the ChakraCore engine.
The actual implementation can be found in JsSerializer.cs (WelsonJS.Toolkit):
Once implemented, JSON I/O 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);
}
}
The approach is similar to how XML format is processed in the traditional .NET programming environment, and we can see that it has become 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), but 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.
Currently, Microsoft does not distribute official binaries for ChakraCore. While Microsoft remains the main maintainer after transitioning ChakraCore to open source, most decisions, including distribution, appear to be left to the autonomy of the open source community. If you need ChakraCore binaries, refer to the link below: