Create Json with particular structure with C ++
-
wrote on 26 Jan 2021, 20:44 last edited by
Hello everyone, could someone tell me if there is an external qt and c ++ library to create a json with a particular structure, I work with QJsonDocument but the variables are ordered alphabetically.
-
Hi,
You might want to check the excellent nlohmann json project.
-
Hello everyone, could someone tell me if there is an external qt and c ++ library to create a json with a particular structure, I work with QJsonDocument but the variables are ordered alphabetically.
@Gabriela20 said in Create Json with particular structure with C ++:
I work with QJsonDocument but the variables are ordered alphabetically.
May I ask why you need a particular order?
In JSON, Objects are unordered structures. Therefore
{"key1": 1, "key2": 2}
is the same value as{"key2": 2, "key1": 1}
-
@Gabriela20 said in Create Json with particular structure with C ++:
I work with QJsonDocument but the variables are ordered alphabetically.
May I ask why you need a particular order?
In JSON, Objects are unordered structures. Therefore
{"key1": 1, "key2": 2}
is the same value as{"key2": 2, "key1": 1}
wrote on 26 Jan 2021, 23:20 last edited by@JKSH I am working with Azure And I only accept a specific structure so that Azure can work
-
@JKSH I am working with Azure And I only accept a specific structure so that Azure can work
wrote on 26 Jan 2021, 23:42 last edited by@Gabriela20
Seems very strange, since JSON is unordered. I assume you'll have to implement your own writer. -
Hi,
You might want to check the excellent nlohmann json project.
wrote on 27 Jan 2021, 01:12 last edited by@SGaist I am a bit new to qt, could you explain me how to install the library
-
@Gabriela20
Seems very strange, since JSON is unordered. I assume you'll have to implement your own writer.wrote on 27 Jan 2021, 01:14 last edited by@JonB it works with QJsonDocument and sorts them alphabetically
-
@JonB it works with QJsonDocument and sorts them alphabetically
wrote on 27 Jan 2021, 06:45 last edited by JonB@Gabriela20
What works with QJsonDocument and what sorts them alphabetically?
Even if something outputs JSON sorted alphabetically --- and anything is free to do so, since JSON is unordered --- does that mean any reader requires the input to be sorted alphabetically in order to work? It certainly should not require this if it supports JSON. -
@Gabriela20
What works with QJsonDocument and what sorts them alphabetically?
Even if something outputs JSON sorted alphabetically --- and anything is free to do so, since JSON is unordered --- does that mean any reader requires the input to be sorted alphabetically in order to work? It certainly should not require this if it supports JSON.wrote on 28 Jan 2021, 23:28 last edited by@JonB Internally QJsonObject converted json objects to a QVariantMap (QMap(QString,QVariant)). This is always sorted by key: "With QMap, the items are always sorted by key."
https://doc.qt.io/qt-5/qjsonobject.htmlSo if reading a json document with QJsonDocument you will always get sorted maps of objects. I don't see why this is an issue though. You can access the values in any order.
-
@JKSH I am working with Azure And I only accept a specific structure so that Azure can work
@Gabriela20 said in Create Json with particular structure with C ++:
I am working with Azure And I only accept a specific structure so that Azure can work
Really? I would've expected Azure to accept unordered JSON... Do you have a link to any documentation (or an example) that shows that Azure expects a specific order?
-
@JonB Internally QJsonObject converted json objects to a QVariantMap (QMap(QString,QVariant)). This is always sorted by key: "With QMap, the items are always sorted by key."
https://doc.qt.io/qt-5/qjsonobject.htmlSo if reading a json document with QJsonDocument you will always get sorted maps of objects. I don't see why this is an issue though. You can access the values in any order.
wrote on 29 Jan 2021, 08:11 last edited by@fcarney said in Create Json with particular structure with C ++:
So if reading a json document with QJsonDocument you will always get sorted maps of objects.
Indeed. But that is only an implementation. The fact remains that JSON specifies no order for keys.
I am with @JKSH
Really? I would've expected Azure to accept unordered JSON... Do you have a link to any documentation (or an example) that shows that Azure expects a specific order?
I would like to see @Gabriela20 explain/substantiate
I am working with Azure And I only accept a specific structure so that Azure can work
If whatever s/he is using in Azure to read is JSON-compliant, where does it require, rather than accept, alphabetically-ordered keys?
-
wrote on 29 Jan 2021, 22:49 last edited by Kent-Dorfman
I use the nlohmann template class library...in fact, it is going to the moon.
-
@SGaist I am a bit new to qt, could you explain me how to install the library
wrote on 29 Jan 2021, 22:54 last edited by@Gabriela20 It is a header-only library. Include the header file(s) (per instructions from the git project page) and have fun.
2/13