ASL
XML, XDL, and JSON

Detailed Description

These functions allow parsing and writing data in XML, XDL and JSON formats.

Data can be decoded and encoded from/to a text string with:

Var data = Json::decode(json);
String json = Json::encode(data);
Var data = Xdl::decode(xdl);
String xdl = Xdl::encode(data, true); // optional flag to indent code
Xml root = Xml::decode(xmlstring);
String xmlstring = Xml::encode(root);
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.
static Var decode(const String &xdl)
Decodes the XDL-encoded string into a Var that will contain all the structure.
static String encode(const Var &v, int mode=Json::SIMPLE)
Encodes the given Var into an XDL-format representation.

The recommended way to load and parse a file is:

Var data = Json::read("file.json");
Var data = Xdl::read("file.xdl");
if(!data.ok()) {...} // incorrect format
Xml xml = Xml::read("file.xml");
if(!xml) {...} // incorrect format
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.
static Var read(const String &file)
Reads and decodes data from a file in XDL format.

And the recommended way to encode and write data in these formats to a file is:

Json::write(data, "file.json");
Xdl::write(data, "file.xdl");
Xml::write(xml, "file.xml");
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.
static bool write(const Var &v, const String &file, int mode=Json::NICE)
Writes a var to a file in XDL format.

XDL, eXtensible Data Language, is a format for describing structured data as text. It has some similarities with the VRML syntax and with the JSON semantics.

The main differences with VRML syntax are that field name and value are separated with a '=', that boolean values are Y nd N and that array or property elements in the same line are separated with a ','. The main differences with JSON are that property names are not quoted, that objects can have a class name prepended, as in VRML, that elements can be separated with a newline instead of a comma, and that there can be C/C++ style comments. Whitespace and newlines can be removed to produce more compact representations.

Consider this XDL code:

Garage {
capacity = 10
open = Y
dimensions = [10, 12, 2.5]
vehicles = [
Car {
brand = "VW"
}
Truck {
brand = "Volvo"
length = 6.5
}
]
}

That code represents an object of class Garage with 3 properties: an integer capacity, a boolean open flag, a numeric array dimensions and the vehicles array. And that array contains two objects of different classes. If that fragment is stored in String garagex it could be read like this:

Var garage = Xdl::decode(garagex);
if(!garage.is("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.is("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 an XDL string.

garage["vehicles"] << Var{{ASL_XDLCLASS, "Car"}, {"brand", "Limo"}, {"length", 8.34}};
garage["open"] = false;
garagex = Xdl::encode(garage, true);

The true parameter makes the function write the code with new lines and indentations. By default the result is compact.

The JSON functions Json::encode(), Json::decode()do the same using JSON syntax. Class names are represented by a propery named $type in this case (actually, the macro ASL_XDLCLASS). All of JSON syntax is supported.

Classes

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...