These functions allow parsing and writing data in XML and JSON formats.
Data can be decoded and encoded from/to a text string with:
static String encode(const Xml &e, bool formatted=true)
Encodes the given XML document as XML, with or without formatting.
static Xml decode(const String &xml)
Parses the given string as XML and returns the equivalent DOM tree.
static Var decode(const String &json)
Decodes the JSON-encoded string into a Var that will contain all the structure.
static String encode(const Var &v, Mode mode=NONE)
Encodes the given Var into a JSON-format representation.
The recommended way to load and parse a file is:
if(!data.ok()) {...}
if(!xml) {...}
static Xml read(const String &file)
Reads and decodes a file as XML and returns its root element.
static Var read(const String &file)
Reads and decodes data from a file in JSON format.
And the recommended way to encode and write data in these formats to a file is:
static bool write(const Xml &e, const String &file)
Writes an XML document to a file.
static bool write(const Var &v, const String &file, Mode mode=PRETTY)
Writes a var to a file in JSON format.
if(!garage)
return;
int capacity = garage["capacity"];
bool open = garage["open"];
float height = (garage.has("dimensions", Var::ARRAY) && garage["dimensions"].length() == 3) ?
garage["dimensions"][2] : 0;
for(auto& vehicle : garage["vehicles"])
{
double length = vehicle.has("length", Var::NUMBER) ? vehicle["length"] : 0.0;
String brand = vehicle["brand"];
my_garage << vehicle["$type"] == "Car" ? new Car(brand, length) : new Truck(brand, length);
}
We may construct a new Var or modify the one just parsed, and rewrite it as a JSON string.
garage["vehicles"] << Var{{"$type", "Car"}, {"brand", "Limo"}, {"length", 8.34}};
garage["open"] = false;
|
| struct | Json |
| | Functions to encode/decode data as JSON. More...
|
| |
| struct | Xdl |
| | Static functions to encode/decode XDL data. More...
|
| |
| class | Xml |
| | This class represents an XML element and can be used to manipulate a document DOM tree. More...
|
| |