Module serial::json
JSON serialization (built on std/serial/value.h)
Making a struct JSON-serializable: SafeC has no field-name reflection (see value.h's top comment), so write a 'to_value()' method by hand and let this module handle the rest:
struct Point { double x; double y; };
struct Value Point::to_value() const {
struct Value v = Value::object();
v.object_set("x", Value::from_float(self.x));
v.object_set("y", Value::from_float(self.y));
return v;
}
...
struct String json = value_to_json(&p.to_value());
(a from_value()/from_json() the same way covers the decode direction)
Structs
| Json | The JSON backend as a Format (see value.h): v.encode(Json::new()), Value::decode(Json::new(), text, &ok) — the same as json_write / json_parse. |
Functions
| json_write | Appends v's JSON text representation to 'out' (does not clear 'out' first — lets you build up a larger document across multiple calls). |
| value_to_json | Convenience wrapper: a fresh String holding just v's JSON text. |
| json_parse | Parses a JSON document. On success, '*ok' (if non-NULL) is set to 1 and the parsed tree is returned; on failure, '*ok' is set to 0 and the return value is Value::null(). Ignores trailing content after the first complete value (matches most JSON libraries' default leniency). |