Argentum got a JSON module with both Parser and Writer.
Let's try Writer in action:
We'll use the same classes as in JSON Parser example:
class Point{
x = 0f;
y = 0f;
}
class Polygon {
name = "";
points = Array(Point);
isActive = false;
}
This function converts an array of Polygons into a JSON string:
fn polygonsToJson(data Array(Polygon)) str {
Writer.useTabs().arr {
data.each `poly _.obj {
_("name").str(poly.name);
_("active").bool(poly.isActive);
_("points").arr\poly.points.each `pt _.obj {
_("x").num(double(pt.x));
_("y").num(double(pt.y))
}
}
}.toStr()
}
- As with parser, the JSON writer does not create any intermediate DOM-like data structures and as such has no memory and CPU overheads.
- It can produce both compact and pretty-printed JSONs, having configurable indentations.
- As with parser, the JSON-writing code has absolutely no syntax overhead - all it contains is field-name-to data mapping, data formatters and handlers of nested objects and arrays. No DSL could have more concise syntax.
- Combining parser and writer we can produce feature-rich JSON-handling applications running at a speed of native code and enjoying safety of managed one.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.