Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. QJSonObject: is there any way to know where the instance is located within the JSON Document?
QtWS25 Last Chance

QJSonObject: is there any way to know where the instance is located within the JSON Document?

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
5 Posts 2 Posters 473 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BaroneAshura
    wrote on 12 Jun 2024, 14:54 last edited by
    #1

    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

    J 1 Reply Last reply 12 Jun 2024, 15:33
    0
    • B BaroneAshura
      12 Jun 2024, 14:54

      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

      J Offline
      J Offline
      JonB
      wrote on 12 Jun 2024, 15:33 last edited by JonB 6 Dec 2024, 15:35
      #2

      @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 is QJsonObject (QJsonObject QJsonValue::toObject() const) then, yes, you can compare that for equality with another QJsonObject 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.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        BaroneAshura
        wrote on 12 Jun 2024, 15:52 last edited by BaroneAshura 6 Dec 2024, 15:53
        #3

        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 -.-

        J 1 Reply Last reply 12 Jun 2024, 16:58
        0
        • B BaroneAshura
          12 Jun 2024, 15:52

          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 -.-

          J Offline
          J Offline
          JonB
          wrote on 12 Jun 2024, 16:58 last edited by JonB
          #4

          @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 a QJsonDocument 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/record

           obj.value("screen").toObject().value("size")
          // or
           obj.value("screen").toObject().value("size").toObject()
          

          you have a QJsonValue (which happens to be a QJsonObject) or a QJsonObject, and you should be able to find that by traversing the QJsonDocument. However, once you go down to

           obj.value("screen").toObject().value("size").toObject().value("w")
          

          that QJsonValue is a QJsonValue.Double (if it's a number) or a QJsonValue.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) const

          Returns 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 QJsonValues which per enum QJsonValue::Type are either QJsonValue::Array or QJsonValue::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 nested size 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.

          B 1 Reply Last reply 13 Jun 2024, 09:28
          2
          • J JonB
            12 Jun 2024, 16:58

            @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 a QJsonDocument 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/record

             obj.value("screen").toObject().value("size")
            // or
             obj.value("screen").toObject().value("size").toObject()
            

            you have a QJsonValue (which happens to be a QJsonObject) or a QJsonObject, and you should be able to find that by traversing the QJsonDocument. However, once you go down to

             obj.value("screen").toObject().value("size").toObject().value("w")
            

            that QJsonValue is a QJsonValue.Double (if it's a number) or a QJsonValue.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) const

            Returns 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 QJsonValues which per enum QJsonValue::Type are either QJsonValue::Array or QJsonValue::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 nested size 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.

            B Offline
            B Offline
            BaroneAshura
            wrote on 13 Jun 2024, 09:28 last edited by
            #5

            @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 nested size 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 :)

            1 Reply Last reply
            1
            • B BaroneAshura has marked this topic as solved on 13 Jun 2024, 09:28

            4/5

            12 Jun 2024, 16:58

            • Login

            • Login or register to search.
            4 out of 5
            • First post
              4/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved