QJSonObject: is there any way to know where the instance is located within the JSON Document?
-
Once the JSON document is parsed and I start navigating the tree by means of value(),
is there any way that I can retrieve where a JSONObject instance is located in the JSON Document?Thanks in advance
-
Once the JSON document is parsed and I start navigating the tree by means of value(),
is there any way that I can retrieve where a JSONObject instance is located in the JSON Document?Thanks in advance
@BaroneAshura
Note sure what exactly you mean by a "a JSONObject instance"`. Did you save up a pointer to a JSON object you used to manually build the JSON document? It doesn't sound like it since you say "Once the JSON document is parsed", so I would guess you have used QJsonDocument QJsonDocument::fromJson() to parse text to JSON? In which case you do not have any "JSONObject instance" of your own? So I guess you have an instance of one of the Qt-created ones from something which found it in the document and now you want to know "where it is" in the document?If you walk around the document visiting
QJsonValue
value()
s, and you are looking at one whose type isQJsonObject
(QJsonObject QJsonValue::toObject() const) then, yes, you can compare that for equality with anotherQJsonObject
because there is a bool QJsonObject::operator==(const QJsonObject &other) const operator.You cannot immediately see the location of a JSON object/value/whatever because there is no "parent" in Qt's implementation. So to find where one that you have is you will have to walk the whole tree doing the equality test.
-
Sorry if the question did not end up partilarly clear.
I'll try re-wording the whole thing.
Given the following JSON text:
{
"screen": {
"size": {
"w": "800",
"h": "480"
}
}
}execute the following statement (please allow some shortcuts):
QJsonObject obj = QJsonDocument::fromJson(content).object();I can then traverse the JSON to extract data and I may end up looking for data in the following way:
string s = obj.value("screen").toObject().value("size").toObject().value("w").toString();now suppose that I want to perform some semantic checks on the content of the 'w' value, or maybe check that the "size" object contains both the "w" and the "h" values; in the event the the JSON document violates the checks, I'd like print where in the document the semantic error occurred.
Is there any kind of information in QJsonObject (or QJsonValue whatsoever) class that relates the QJsonObject (or QJsonValue whatsoever) class instance to the portion of JSON Document it was parsed from?
Hope I managed to make the question more understandable, but I am not completely sure -.-
-
Sorry if the question did not end up partilarly clear.
I'll try re-wording the whole thing.
Given the following JSON text:
{
"screen": {
"size": {
"w": "800",
"h": "480"
}
}
}execute the following statement (please allow some shortcuts):
QJsonObject obj = QJsonDocument::fromJson(content).object();I can then traverse the JSON to extract data and I may end up looking for data in the following way:
string s = obj.value("screen").toObject().value("size").toObject().value("w").toString();now suppose that I want to perform some semantic checks on the content of the 'w' value, or maybe check that the "size" object contains both the "w" and the "h" values; in the event the the JSON document violates the checks, I'd like print where in the document the semantic error occurred.
Is there any kind of information in QJsonObject (or QJsonValue whatsoever) class that relates the QJsonObject (or QJsonValue whatsoever) class instance to the portion of JSON Document it was parsed from?
Hope I managed to make the question more understandable, but I am not completely sure -.-
@BaroneAshura
Certainly there is no link to the *text" (or file) from which it was parsed. For example, if you wanted to know what line number"size"
was at you would have no luck. After parsing all you have is a JSON structure.I also said that no "parent" relationship is maintained in the Qt JSON structure, so for example given a
QJsonObject
retrieved from somewhere in aQJsonDocument
you cannot look at it on its own and see where it is in the hierarchy.But like I said you can traverse the hierarchy to locate, say, some
QJsonObject
you got from it earlier, using==
to compare for equality.string s = obj.value("screen").toObject().value("size").toObject().value("w").toString();
By the time you have done the
toString()
you have a plain string and have no chance to (safely) determine where that came from. But if you stop at/recordobj.value("screen").toObject().value("size") // or obj.value("screen").toObject().value("size").toObject()
you have a
QJsonValue
(which happens to be aQJsonObject
) or aQJsonObject
, and you should be able to find that by traversing theQJsonDocument
. However, once you go down toobj.value("screen").toObject().value("size").toObject().value("w")
that
QJsonValue
is aQJsonValue.Double
(if it's a number) or aQJsonValue.String
(if it's a string) and I don't think you can find that by traversing, because I assume bool QJsonValue::operator==(const QJsonValue &other) constReturns true if the value is equal to other.
does just a numeric/string comparison of the underlying value, and so would match anywhere which has the same simple value. In short, I would think you can find
QJsonValue
s which per enum QJsonValue::Type are eitherQJsonValue::Array
orQJsonValue::Object
type, but not the other "plain" types.At any rate, the only "look up" you can do is by traversing and comparing (
==
) so you should be able to ascertain my claims by investigating.in the event the the JSON document violates the checks, I'd like print where in the document the semantic error occurred.
No to "where", in the sense that, as I said, no chance that you can relate anything parsed to where it came from in the source. You could of course show whatever you like of the in-memory hierarchy to help the user go look, but you're not going to be able to relate it to a line in a big text document. You could print out its "parentage" up/down the tree (like I said, by traversing and finding only) but that may be ambiguous as well as difficult for the user to locate, though it might be better than nothing. For example, if the
w
value was bad you could tell them it was at/screen/size/w
or if they were missing something in a deeply nestedsize
inside objects and arrays you could point them to/screen/somearray[10]/someobjectkey/.../size
. If they have a "JSON folding editor" they might track it down. -
@BaroneAshura
Certainly there is no link to the *text" (or file) from which it was parsed. For example, if you wanted to know what line number"size"
was at you would have no luck. After parsing all you have is a JSON structure.I also said that no "parent" relationship is maintained in the Qt JSON structure, so for example given a
QJsonObject
retrieved from somewhere in aQJsonDocument
you cannot look at it on its own and see where it is in the hierarchy.But like I said you can traverse the hierarchy to locate, say, some
QJsonObject
you got from it earlier, using==
to compare for equality.string s = obj.value("screen").toObject().value("size").toObject().value("w").toString();
By the time you have done the
toString()
you have a plain string and have no chance to (safely) determine where that came from. But if you stop at/recordobj.value("screen").toObject().value("size") // or obj.value("screen").toObject().value("size").toObject()
you have a
QJsonValue
(which happens to be aQJsonObject
) or aQJsonObject
, and you should be able to find that by traversing theQJsonDocument
. However, once you go down toobj.value("screen").toObject().value("size").toObject().value("w")
that
QJsonValue
is aQJsonValue.Double
(if it's a number) or aQJsonValue.String
(if it's a string) and I don't think you can find that by traversing, because I assume bool QJsonValue::operator==(const QJsonValue &other) constReturns true if the value is equal to other.
does just a numeric/string comparison of the underlying value, and so would match anywhere which has the same simple value. In short, I would think you can find
QJsonValue
s which per enum QJsonValue::Type are eitherQJsonValue::Array
orQJsonValue::Object
type, but not the other "plain" types.At any rate, the only "look up" you can do is by traversing and comparing (
==
) so you should be able to ascertain my claims by investigating.in the event the the JSON document violates the checks, I'd like print where in the document the semantic error occurred.
No to "where", in the sense that, as I said, no chance that you can relate anything parsed to where it came from in the source. You could of course show whatever you like of the in-memory hierarchy to help the user go look, but you're not going to be able to relate it to a line in a big text document. You could print out its "parentage" up/down the tree (like I said, by traversing and finding only) but that may be ambiguous as well as difficult for the user to locate, though it might be better than nothing. For example, if the
w
value was bad you could tell them it was at/screen/size/w
or if they were missing something in a deeply nestedsize
inside objects and arrays you could point them to/screen/somearray[10]/someobjectkey/.../size
. If they have a "JSON folding editor" they might track it down.@JonB said in QJSonObject: is there any way to know where the instance is located within the JSON Document?:
@BaroneAshura
if you wanted to know what line number"size"
was at you would have no luck.Thanks for the confirmation of my suspects. Looking for this confirmation was the purpose of the question
You could print out its "parentage" up/down the tree (like I said, by traversing and finding only) but that may be ambiguous as well as difficult for the user to locate, though it might be better than nothing. For example, if the
w
value was bad you could tell them it was at/screen/size/w
or if they were missing something in a deeply nestedsize
inside objects and arrays you could pint them to/screen/somearray[10]/someobjectkey/.../size
. If they have a "JSON folding editor" they might track it down.I may end up working on solutions based on the above possibilities...
Thanks for your time :)
-
B BaroneAshura has marked this topic as solved on