JsonWax for Qt

NOW WITH SERIALIZATION

------ JsonWax JsonWax JsonWax --- JsonWax JsonWax JsonWax ------------ JsonWax JsonWax JsonWax ---- JsonWax JsonWax JsonWax ------------- JsonWax JsonWax JsonWax ---------- JsonWax JsonWax JsonWax ----------- JsonWax JsonWax JsonWax -------- JsonWax JsonWax JsonWax ------------ JsonWax JsonWax JsonWax ------- JsonWax JsonWax JsonWax JsonWax JsonWax JsonWax --------- JsonWax JsonWax JsonWax ----------- JsonWax JsonWax JsonWax -- JsonWax JsonWax JsonWax -------------- JsonWax JsonWax JsonWax ---------- JsonWax JsonWax JsonWax -------- JsonWax JsonWax JsonWax ---- JsonWax JsonWax JsonWax --------- JsonWax JsonWax JsonWax ---- JsonWax JsonWax JsonWax -- JsonWax JsonWax JsonWax ---------- JsonWax JsonWax JsonWax --------- JsonWax JsonWax JsonWax ------------- JsonWax JsonWax JsonWax - JsonWax JsonWax JsonWax ---------- JsonWax JsonWax JsonWax ----------- JsonWax JsonWax JsonWax ------------ JsonWax JsonWax JsonWax ---------- JsonWax JsonWax JsonWax -------------- JsonWax JsonWax JsonWax ------- JsonWax JsonWax JsonWax JsonWax JsonWax JsonWax -------- JsonWax JsonWax JsonWax ----- JsonWax JsonWax JsonWax JsonWax JsonWax JsonWax

EXAMPLES

Here you'll see how to solve a couple of different problems.

READING VALUE FROM OBJECTS IN AN ARRAY

We create an array of people with interests. We want to know all of their interests.

JsonWax json;
json.setValue({0, "name"}, "Viggo");
json.setValue({0, "interest"}, "eating");
json.setValue({1, "name"}, "Egon");
json.setValue({1, "interest"}, "space travel");
json.setValue({2, "name"}, "Linda");
json.setValue({2, "interest"}, "motorcycles");

for (int i = 0; i < json.size({}); ++i)
  qDebug() << "interest" << i << json.value({i, "interest"}).toString();

Result (printed to console):

interest 0 "eating"
interest 1 "space travel"
interest 2 "motorcycles"

The for-loop could also be a while-loop, which gives the same result as above:

int key = 0;
while (json.exists({key}))
{
  qDebug() << "interest" << key << json.value({key, "interest"}).toString();
  ++key;
}

LOAD A FILE AND CHANGE ITS VALUES

We have a JSON-document as the file "in.json":

{
  "information": {
    "a": true,
    "b": "blob",
    "c": 15.72,
    "d": 17
  }
}

We want to change all of these values, and overwrite the file with an updated one.

JsonWax json ("in.json");
json.setValue({"information","a"}, !json.value({"information","a"}).toBool());
json.setValue({"information","b"}, json.value({"information","b"}).toString() + "Q");
json.setValue({"information","c"}, json.value({"information","c"}).toDouble() + 1);
json.setValue({"information","d"}, json.value({"information","d"}).toInt() + 1);
json.save();

Note that it is recommended to use .toDouble() when retrieving floating point numbers, to avoid creating rounding error decimals, which would be inserted in the document. But you could use .toFloat() if you wanted.

{
  "information": {
    "a": false,
    "b": "blobQ",
    "c": 16.72,
    "d": 18
  }
}

SEARCH FOR VALUE, USE KEY TO FIND OTHER VALUE

The idea here is that we know the value, but not the key to find that value. We use keys() to list all keys in a location. With a foreach loop we check all the values under these keys, until we find the right one. The key is used to retrieve a value from another place in the document.

JsonWax json;
json.setValue({"a","a"},"narwhal");
json.setValue({"a","b"},"zebra");
json.setValue({"a","c"},"lion");
json.setValue({"a","X"},"secret");
json.setValue({"a","e"},"cat");
json.setValue({"b","X"},"diplodocus");

for (QVariant& key : json.keys({"a"}))
  if (json.value({"a", key}) == "secret")
    qDebug() << json.value({"b", key}).toString();

Output:

"diplodocus"

MANAGING KEYS

If you want to keep a specific "directory" in a variable.

JsonWax json;
QVariantList keys = {"a","b"};
json.setValue( keys + QVariantList{"X"}, "hello");
json.setValue( keys + QVariantList{"Y"}, 1325);
qDebug() << json.toString();

Result:

{
  "a": {
    "b" {
      "X": "hello",
      "Y": 1325
    }
  }
}