All Projects → qt-json → Qt Json

qt-json / Qt Json

Licence: gpl-3.0
A simple class for parsing JSON data into a QVariant hierarchy and vice versa.

Labels

Projects that are alternatives of or similar to Qt Json

Cppwebframework
​The C++ Web Framework (CWF) is a MVC web framework, Open Source, under MIT License, using C++ with Qt to be used in the development of web applications.
Stars: ✭ 348 (+27.47%)
Mutual labels:  json, qt
Cutelyst
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
Stars: ✭ 671 (+145.79%)
Mutual labels:  json, qt
Jqview
simplest possible native GUI for inspecting JSON objects with jq
Stars: ✭ 355 (+30.04%)
Mutual labels:  json, qt
Bitsofbytes
Code and projects from my blog posts.
Stars: ✭ 89 (-67.4%)
Mutual labels:  json, qt
Qsimpleupdater
Updater system for Qt applications
Stars: ✭ 429 (+57.14%)
Mutual labels:  json, qt
Qxorm
QxOrm library - C++ Qt ORM (Object Relational Mapping) and ODM (Object Document Mapper) library - Official repository
Stars: ✭ 176 (-35.53%)
Mutual labels:  json, qt
Jsonmodels
jsonmodels is library to make it easier for you to deal with structures that are converted to, or read from JSON.
Stars: ✭ 262 (-4.03%)
Mutual labels:  json
Mojojson
A simple and fast JSON parser.
Stars: ✭ 271 (-0.73%)
Mutual labels:  json
Sqawk
Like Awk but with SQL and table joins
Stars: ✭ 263 (-3.66%)
Mutual labels:  json
Klayout
KLayout Main Sources
Stars: ✭ 261 (-4.4%)
Mutual labels:  qt
Oscal
Open Security Controls Assessment Language (OSCAL)
Stars: ✭ 272 (-0.37%)
Mutual labels:  json
Surrealist
to_json but I wrote it myself
Stars: ✭ 271 (-0.73%)
Mutual labels:  json
Webpack Assets Manifest
This Webpack plugin will generate a JSON file that matches the original filename with the hashed version.
Stars: ✭ 269 (-1.47%)
Mutual labels:  json
Survey Library
JavaScript Survey and Form Library
Stars: ✭ 3,060 (+1020.88%)
Mutual labels:  json
Slicer
Multi-platform, free open source software for visualization and image computing.
Stars: ✭ 263 (-3.66%)
Mutual labels:  qt
Json Schema To Ts
Infer TS types from JSON schemas 📝
Stars: ✭ 261 (-4.4%)
Mutual labels:  json
Crjdt
A conflict-free replicated JSON datatype (CRDT) in Scala
Stars: ✭ 271 (-0.73%)
Mutual labels:  json
Jackson Databind
General data-binding package for Jackson (2.x): works on streaming API (core) implementation(s)
Stars: ✭ 2,959 (+983.88%)
Mutual labels:  json
Evision
计算机视觉实践和探索/Practice and explorations in computer vision.
Stars: ✭ 268 (-1.83%)
Mutual labels:  qt
Edizon cheatsconfigsandscripts
The official EdiZon Editor Config and Editor Script repository.
Stars: ✭ 271 (-0.73%)
Mutual labels:  json

The qt-json project is a simple collection of functions for parsing and serializing JSON data to and from QVariant hierarchies.

NOTE: Qt5 introduced a native JSON object class. If you are targeting Qt5, you should use that instead.

HOW TO USE

Parsing JSON

The parser is really easy to use. Let's say we have the following QString of JSON data:

{
  "encoding" : "UTF-8",
  "plug-ins" : [
    "python",
    "c++",
    "ruby"
  ],
  "indent" : {
    "length" : 3,
    "use_space" : true
  }
}

We would first call the parse-function:

#include "json.h"

bool ok;
// json is a QString containing the JSON data
QtJson::JsonObject result = QtJson::parse(json, ok).toMap();

if(!ok) {
  qFatal("An error occurred during parsing");

Assuming the parser completed without errors, we can then go through the hierarchy:

qDebug() << "encoding:" << result["encoding"].toString();
qDebug() << "plugins:";

foreach(QVariant plugin, result["plug-ins"].toList()) {
    qDebug() << "  -" << plugin.toString();
}

QtJson::JsonObject nested = result["indent"].toMap();
qDebug() << "length:" << nested["length"].toInt();
qDebug() << "use_space:" << nested["use_space"].toBool();

The previous code would print out the following:

encoding: "UTF-8"
plugins:
  - "python"
  - "c++"
  - "ruby"
length: 3
use_space: true

Serializing JSON

To write JSON data from Qt object is as simple as creating and assigning data to a QVariantMap/JsonObject:

QtJson::JsonObject contributor;
contributor["name"] = "Luis Gustavo";
contributor["age"] = 22;

QByteArray data = QtJson::serialize(contributor);

The byte array 'data' contains valid JSON data:

{
  "name": "Luis Gustavo",
  "age": 22
}

Serializing JSON pretty-print

By default, the serialization will create a minified version, like following:

{"name":"Luis Gustavo","age":22}

If you are debugging or logging, you may prefer to enable pretty-print mode globally, before serialize:

QtJson::setPrettySerialize(true);

QByteArray data = QtJson::serialize(contributor);
// ...
QByteArray data = QtJson::serialize(other_contributor);

Obviously, you can disable it with:

QtJson::setPrettySerialize(false);

After creating the QVariantMap, you can create a QVariantList/JsonArray and append the QVariantMaps.

QtJson::JsonObject friend1, friend2, friend3;
friend1["id"] = 1;
friend1["name"] = "Mackenzie Hamphrey";

friend2["id"] = 2;
friend2["name"] = "Melanie Molligan";

friend3["id"] = 3;
friend3["name"] = "Sydney Calhoun";

QtJson::JsonArray friends;
friends.append(friend1);
friends.append(friend2);
friends.append(friend3);

QtJson::JsonObject obj;
obj["friends"] = friends;

This way you create a nested structure:

{
    "friends": [
        {
            "id": 1,
            "name": "MackenzieHamphrey"
        },
        {
            "id": 2,
            "name": "MelanieMolligan"
        },
        {
            "id": 3,
            "name": "SydneyCalhoun"
        }
    ]
}

If you continue this process recursively, you nest more levels into the JSON structure.

Using Builders

For simplicity you can use builders, if you prefer.

For example, create a JsonObject:

QtJson::JsonObject json = QtJson::objectBuilder()
    ->set("field_1", 10)
    ->set("field_2", "A string")
    ->set("field_3", true)
    ->set("field_4", QtJson::objectBuilder()
        ->set("sub_field_1", 10.4)
        ->set("sub_field_n", "Another string")
    )
    ->create();

Or create a JsonArray:

QtJson::JsonArray json = QtJson::arrayBuilder()
    ->add(5)
    ->add(90.2)
    ->add(true)
    ->add("anything else")
    ->create();

Take a look at this example that rewrite the previous one:

QtJson::JsonObject obj = QtJson::objectBuilder()
    ->set("friends", QtJson::arrayBuilder()
        ->add(QtJson::objectBuilder()
            ->set("id", 1)
            ->set("name", "Mackenzie Hamphrey")
        )
        ->add(QtJson::objectBuilder()
            ->set("id", 2)
            ->set("name", "Melanie Molligan")
        )
        ->add(QtJson::objectBuilder()
            ->set("id", 3)
            ->set("name", "Sydney Calhoun")
        )
    )
    ->create();

3. CONTRIBUTING

Send in a pull request and bug the maintainer until it gets merged and published. Make sure to add yourself to AUTHORS.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].