{"id":13743425,"url":"https://github.com/qt-json/qt-json","last_synced_at":"2025-05-09T01:30:53.218Z","repository":{"id":45128917,"uuid":"1946131","full_name":"qt-json/qt-json","owner":"qt-json","description":"A simple class for parsing JSON data into a QVariant hierarchy and vice versa.","archived":false,"fork":false,"pushed_at":"2018-02-12T12:05:04.000Z","size":81,"stargazers_count":327,"open_issues_count":7,"forks_count":105,"subscribers_count":26,"default_branch":"master","last_synced_at":"2024-08-18T20:06:16.278Z","etag":null,"topics":["json","qt"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/qt-json.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-06-24T06:20:59.000Z","updated_at":"2024-07-23T08:43:03.000Z","dependencies_parsed_at":"2022-07-13T16:44:57.707Z","dependency_job_id":null,"html_url":"https://github.com/qt-json/qt-json","commit_stats":null,"previous_names":["gaudecker/qt-json"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qt-json%2Fqt-json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qt-json%2Fqt-json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qt-json%2Fqt-json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qt-json%2Fqt-json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/qt-json","download_url":"https://codeload.github.com/qt-json/qt-json/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253174230,"owners_count":21865836,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","qt"],"created_at":"2024-08-03T05:00:46.600Z","updated_at":"2025-05-09T01:30:52.930Z","avatar_url":"https://github.com/qt-json.png","language":"C++","funding_links":[],"categories":["Libraries."],"sub_categories":[],"readme":"The **qt-json** project is a simple collection of functions for parsing and serializing [JSON][js] data to and from [QVariant][var] \nhierarchies.\n\n**NOTE:** Qt5 introduced [a native JSON object class][qt5]. If you are targeting Qt5, you should use that instead.\n\n### HOW TO USE ###\n#### Parsing JSON ####\n\nThe parser is really easy to use. Let's say we have the following \nQString of JSON data:\n\n```json\n{\n  \"encoding\" : \"UTF-8\",\n  \"plug-ins\" : [\n    \"python\",\n    \"c++\",\n    \"ruby\"\n  ],\n  \"indent\" : {\n    \"length\" : 3,\n    \"use_space\" : true\n  }\n}\n```\n\nWe would first call the parse-function:\n\n```cpp\n#include \"json.h\"\n\nbool ok;\n// json is a QString containing the JSON data\nQtJson::JsonObject result = QtJson::parse(json, ok).toMap();\n\nif(!ok) {\n  qFatal(\"An error occurred during parsing\");\n```\n\nAssuming the parser completed without errors, we can then\ngo through the hierarchy:\n\n```cpp\nqDebug() \u003c\u003c \"encoding:\" \u003c\u003c result[\"encoding\"].toString();\nqDebug() \u003c\u003c \"plugins:\";\n\nforeach(QVariant plugin, result[\"plug-ins\"].toList()) {\n    qDebug() \u003c\u003c \"  -\" \u003c\u003c plugin.toString();\n}\n\nQtJson::JsonObject nested = result[\"indent\"].toMap();\nqDebug() \u003c\u003c \"length:\" \u003c\u003c nested[\"length\"].toInt();\nqDebug() \u003c\u003c \"use_space:\" \u003c\u003c nested[\"use_space\"].toBool();\n```\n\nThe previous code would print out the following:\n\n    encoding: \"UTF-8\"\n    plugins:\n      - \"python\"\n      - \"c++\"\n      - \"ruby\"\n    length: 3\n    use_space: true\n\n\n#### Serializing JSON ####\nTo write JSON data from Qt object is as simple as creating and assigning data to a [QVariantMap/JsonObject][varmap]:\n\n```cpp\nQtJson::JsonObject contributor;\ncontributor[\"name\"] = \"Luis Gustavo\";\ncontributor[\"age\"] = 22;\n\nQByteArray data = QtJson::serialize(contributor);\n```\n\nThe byte array 'data' contains valid JSON data:\n\n```json\n{\n  \"name\": \"Luis Gustavo\",\n  \"age\": 22\n}\n```\n\n#### Serializing JSON pretty-print ####\nBy default, the serialization will create a _minified_ version, like following:\n\n```json\n{\"name\":\"Luis Gustavo\",\"age\":22}\n```\n\nIf you are debugging or logging, you may prefer to enable **pretty-print** mode globally, before serialize:\n\n```cpp\nQtJson::setPrettySerialize(true);\n\nQByteArray data = QtJson::serialize(contributor);\n// ...\nQByteArray data = QtJson::serialize(other_contributor);\n```\n\nObviously, you can disable it with:\n```cpp\nQtJson::setPrettySerialize(false);\n```\n\n---\n\nAfter creating the QVariantMap, you can create a [QVariantList/JsonArray][varlist] and append the QVariantMaps. \n\n```cpp    \nQtJson::JsonObject friend1, friend2, friend3;\nfriend1[\"id\"] = 1;\nfriend1[\"name\"] = \"Mackenzie Hamphrey\";\n\nfriend2[\"id\"] = 2;\nfriend2[\"name\"] = \"Melanie Molligan\";\n\nfriend3[\"id\"] = 3;\nfriend3[\"name\"] = \"Sydney Calhoun\";\n\nQtJson::JsonArray friends;\nfriends.append(friend1);\nfriends.append(friend2);\nfriends.append(friend3);\n\nQtJson::JsonObject obj;\nobj[\"friends\"] = friends;\n```\n\nThis way you create a nested structure:\n\n```json\n{\n    \"friends\": [\n        {\n            \"id\": 1,\n            \"name\": \"MackenzieHamphrey\"\n        },\n        {\n            \"id\": 2,\n            \"name\": \"MelanieMolligan\"\n        },\n        {\n            \"id\": 3,\n            \"name\": \"SydneyCalhoun\"\n        }\n    ]\n}\n```\n\nIf you continue this process recursively, you nest more levels into the JSON structure.\n\n\n#### Using Builders ####\n\nFor simplicity you can use **builders**, if you prefer.\n\nFor example, create a `JsonObject`:\n\n```cpp\nQtJson::JsonObject json = QtJson::objectBuilder()\n    -\u003eset(\"field_1\", 10)\n    -\u003eset(\"field_2\", \"A string\")\n    -\u003eset(\"field_3\", true)\n    -\u003eset(\"field_4\", QtJson::objectBuilder()\n        -\u003eset(\"sub_field_1\", 10.4)\n        -\u003eset(\"sub_field_n\", \"Another string\")\n    )\n    -\u003ecreate();\n```\n\nOr create a `JsonArray`:\n\n```cpp\nQtJson::JsonArray json = QtJson::arrayBuilder()\n    -\u003eadd(5)\n    -\u003eadd(90.2)\n    -\u003eadd(true)\n    -\u003eadd(\"anything else\")\n    -\u003ecreate();\n```\n\nTake a look at this example that rewrite the previous one:\n\n```cpp\nQtJson::JsonObject obj = QtJson::objectBuilder()\n    -\u003eset(\"friends\", QtJson::arrayBuilder()\n        -\u003eadd(QtJson::objectBuilder()\n            -\u003eset(\"id\", 1)\n            -\u003eset(\"name\", \"Mackenzie Hamphrey\")\n        )\n        -\u003eadd(QtJson::objectBuilder()\n            -\u003eset(\"id\", 2)\n            -\u003eset(\"name\", \"Melanie Molligan\")\n        )\n        -\u003eadd(QtJson::objectBuilder()\n            -\u003eset(\"id\", 3)\n            -\u003eset(\"name\", \"Sydney Calhoun\")\n        )\n    )\n    -\u003ecreate();\n```\n\n\n### 3. CONTRIBUTING ###\n\nSend in a pull request and bug the maintainer until it gets merged and published. \nMake sure to add yourself to AUTHORS.\n\n\n[js]: http://www.json.org/ \"JSON Standard specification\"\n[var]: http://qt-project.org/doc/qt-4.8/qvariant.html \"QVariant class reference\"\n[qt5]: http://qt-project.org/doc/qt-5.0/qtcore/qjsonobject.html \"Qt5 QJsonObject class reference\"\n[varmap]: http://qt-project.org/doc/qt-4.8/qvariant.html#QVariantMap-typedef \"Qt4 QVariantMap class reference\"\n[varlist]: http://qt-project.org/doc/qt-4.8/qvariant.html#QVariantList-typedef \"Qt4 QVariantList class reference\"\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqt-json%2Fqt-json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqt-json%2Fqt-json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqt-json%2Fqt-json/lists"}