Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Failing to extract value from JSON why?
Forum Updated to NodeBB v4.3 + New Features

Failing to extract value from JSON why?

Scheduled Pinned Locked Moved Unsolved General and Desktop
25 Posts 8 Posters 3.3k Views 4 Watching
  • 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.
  • S SPlatten
    10 Dec 2020, 15:48

    @Charby , again I would expect toString to convert the type of the value to a string.

    K Offline
    K Offline
    KroMignon
    wrote on 10 Dec 2020, 15:54 last edited by
    #7

    @SPlatten said in Failing to extract value from JSON why?:

    again I would expect toString to convert the type of the value to a string.

    You are wrong, the JSON you have create defines "position" as an integer { "position" : 1 } and not as a string { "position" : "1" }.
    QJsonObject.value() returns a QJsonValue and, as written in documentation:

    QString QJsonValue::toString() const
    Converts the value to a QString and returns it.
    If type() is not String, a null QString will be returned.

    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

    S 1 Reply Last reply 10 Dec 2020, 19:46
    3
    • S SPlatten
      10 Dec 2020, 15:47

      @KroMignon isn’t the purpose of toString to convert the value to a string?

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 10 Dec 2020, 16:04 last edited by
      #8

      @SPlatten said in Failing to extract value from JSON why?:

      isn’t the purpose of toString to convert the value to a string?

      The purpose of a function is to do what's documented... but I know reading documentation just takes too long...

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      S F 2 Replies Last reply 10 Dec 2020, 19:44
      4
      • C Christian Ehrlicher
        10 Dec 2020, 16:04

        @SPlatten said in Failing to extract value from JSON why?:

        isn’t the purpose of toString to convert the value to a string?

        The purpose of a function is to do what's documented... but I know reading documentation just takes too long...

        S Offline
        S Offline
        SPlatten
        wrote on 10 Dec 2020, 19:44 last edited by
        #9

        @Christian-Ehrlicher oh... my sides... so funny!

        Kind Regards,
        Sy

        1 Reply Last reply
        0
        • K KroMignon
          10 Dec 2020, 15:54

          @SPlatten said in Failing to extract value from JSON why?:

          again I would expect toString to convert the type of the value to a string.

          You are wrong, the JSON you have create defines "position" as an integer { "position" : 1 } and not as a string { "position" : "1" }.
          QJsonObject.value() returns a QJsonValue and, as written in documentation:

          QString QJsonValue::toString() const
          Converts the value to a QString and returns it.
          If type() is not String, a null QString will be returned.

          S Offline
          S Offline
          SPlatten
          wrote on 10 Dec 2020, 19:46 last edited by
          #10

          @KroMignon yes, i realise that but the name of the function is toString, so I expect the result to be whatever is on the left converted to a string.

          Kind Regards,
          Sy

          K J 2 Replies Last reply 10 Dec 2020, 21:01
          0
          • S SPlatten
            10 Dec 2020, 19:46

            @KroMignon yes, i realise that but the name of the function is toString, so I expect the result to be whatever is on the left converted to a string.

            K Offline
            K Offline
            KroMignon
            wrote on 10 Dec 2020, 21:01 last edited by
            #11

            @SPlatten said in Failing to extract value from JSON why?:

            yes, i realise that but the name of the function is toString, so I expect the result to be whatever is on the left converted to a string.

            As always with Qt, there is a very good documentation.
            Your first reflex should be to look there if the function does what you expect. It takes seconds and saves hours.

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            0
            • C Christian Ehrlicher
              10 Dec 2020, 16:04

              @SPlatten said in Failing to extract value from JSON why?:

              isn’t the purpose of toString to convert the value to a string?

              The purpose of a function is to do what's documented... but I know reading documentation just takes too long...

              F Offline
              F Offline
              fcarney
              wrote on 10 Dec 2020, 21:50 last edited by
              #12

              @Christian-Ehrlicher said in Failing to extract value from JSON why?:

              The purpose of a function is to do what's documented... but I know reading documentation just takes too long...

              lol, TLDR RTFM!

              C++ is a perfectly valid school of magic.

              1 Reply Last reply
              0
              • S SPlatten
                10 Dec 2020, 19:46

                @KroMignon yes, i realise that but the name of the function is toString, so I expect the result to be whatever is on the left converted to a string.

                J Offline
                J Offline
                JKSH
                Moderators
                wrote on 11 Dec 2020, 00:57 last edited by JKSH 12 Nov 2020, 06:06
                #13

                @SPlatten said in Failing to extract value from JSON why?:

                the name of the function is toString, so I expect the result to be whatever is on the left converted to a string.

                Following that logic, would you also always expect QJsonValue::toArray() to convert your value to an array, and QJsonValue::toObject() to convert your value to an object, and QJsonValue::toInt() to convert your value to an integer? Even if the value is not an array or an object or a number?

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                S 1 Reply Last reply 11 Dec 2020, 11:14
                4
                • J JKSH
                  11 Dec 2020, 00:57

                  @SPlatten said in Failing to extract value from JSON why?:

                  the name of the function is toString, so I expect the result to be whatever is on the left converted to a string.

                  Following that logic, would you also always expect QJsonValue::toArray() to convert your value to an array, and QJsonValue::toObject() to convert your value to an object, and QJsonValue::toInt() to convert your value to an integer? Even if the value is not an array or an object or a number?

                  S Offline
                  S Offline
                  SPlatten
                  wrote on 11 Dec 2020, 11:14 last edited by
                  #14

                  @JKSH , of course, and if it couldn't then I would expect an error.

                  Kind Regards,
                  Sy

                  J K 2 Replies Last reply 11 Dec 2020, 11:18
                  0
                  • S SPlatten
                    11 Dec 2020, 11:14

                    @JKSH , of course, and if it couldn't then I would expect an error.

                    J Offline
                    J Offline
                    JonB
                    wrote on 11 Dec 2020, 11:18 last edited by JonB 12 Nov 2020, 11:19
                    #15

                    @SPlatten
                    I'm not criticizing you, just making an observation. It did report an error. But it reported an error by the toString() return a null QString(), which is an invalid string, for you to check for, rather than a "message"/"exception". Which is the Qt library way of returning errors.

                    S 1 Reply Last reply 11 Dec 2020, 11:23
                    0
                    • S SPlatten
                      11 Dec 2020, 11:14

                      @JKSH , of course, and if it couldn't then I would expect an error.

                      K Offline
                      K Offline
                      KroMignon
                      wrote on 11 Dec 2020, 11:19 last edited by
                      #16

                      @SPlatten said in Failing to extract value from JSON why?:

                      if it couldn't then I would expect an error.

                      Qt is avoiding exception usage, so what kind of error did you expect?
                      And you should not expect to much but simply some time hit the F1 key on your keyboard to get the help for the function. All is documentation, you only have to read it.

                      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                      S 1 Reply Last reply 11 Dec 2020, 11:24
                      0
                      • J JonB
                        11 Dec 2020, 11:18

                        @SPlatten
                        I'm not criticizing you, just making an observation. It did report an error. But it reported an error by the toString() return a null QString(), which is an invalid string, for you to check for, rather than a "message"/"exception". Which is the Qt library way of returning errors.

                        S Offline
                        S Offline
                        SPlatten
                        wrote on 11 Dec 2020, 11:23 last edited by
                        #17

                        @JonB normally I always check the return of a function, I will revisit this code.

                        Kind Regards,
                        Sy

                        J 1 Reply Last reply 11 Dec 2020, 13:43
                        0
                        • K KroMignon
                          11 Dec 2020, 11:19

                          @SPlatten said in Failing to extract value from JSON why?:

                          if it couldn't then I would expect an error.

                          Qt is avoiding exception usage, so what kind of error did you expect?
                          And you should not expect to much but simply some time hit the F1 key on your keyboard to get the help for the function. All is documentation, you only have to read it.

                          S Offline
                          S Offline
                          SPlatten
                          wrote on 11 Dec 2020, 11:24 last edited by
                          #18

                          @KroMignon , Why is Qt avoid exception usage, if there is an error then there should be a mechanism for finding out what was wrong.

                          Kind Regards,
                          Sy

                          J K 2 Replies Last reply 11 Dec 2020, 11:33
                          0
                          • S SPlatten
                            11 Dec 2020, 11:24

                            @KroMignon , Why is Qt avoid exception usage, if there is an error then there should be a mechanism for finding out what was wrong.

                            J Offline
                            J Offline
                            JonB
                            wrote on 11 Dec 2020, 11:33 last edited by JonB 12 Nov 2020, 11:36
                            #19

                            @SPlatten
                            The answer is to this is historical/per language. C, and then C++, used function return results to indicate errors, not exceptions. (Exceptions are regarded as "expensive" in C++, but that's only one reason.) It's not always true now --- some C++ libraries do use exceptions --- but plenty stick with that approach. Qt is just following that pattern, which is not unreasonable for C++. Some other languages, e.g. C# or Python, tend to raise exceptions, as you prefer. That's just how it is.

                            1 Reply Last reply
                            2
                            • S SPlatten
                              11 Dec 2020, 11:24

                              @KroMignon , Why is Qt avoid exception usage, if there is an error then there should be a mechanism for finding out what was wrong.

                              K Offline
                              K Offline
                              KroMignon
                              wrote on 11 Dec 2020, 11:39 last edited by
                              #20

                              @SPlatten said in Failing to extract value from JSON why?:

                              Why is Qt avoid exception usage

                              Again, this is documented ;)
                              https://wiki.qt.io/Coding_Conventions
                              https://doc.qt.io/qt-5/exceptionsafety.html

                              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                              1 Reply Last reply
                              3
                              • S SPlatten
                                11 Dec 2020, 11:23

                                @JonB normally I always check the return of a function, I will revisit this code.

                                J Offline
                                J Offline
                                JKSH
                                Moderators
                                wrote on 11 Dec 2020, 13:43 last edited by
                                #21

                                @SPlatten said in Failing to extract value from JSON why?:

                                normally I always check the return of a function, I will revisit this code.

                                Good. So check if toString() returns a null string -- that's your error, as @JonB said.

                                the name of the function is toString, so I expect the result to be whatever is on the left converted to a string.

                                Following that logic, would you also always expect QJsonValue::toArray() to convert your value to an array, and QJsonValue::toObject() > to convert your value to an object, and QJsonValue::toInt() to convert your value to an integer? Even if the value is not an array or an object or a number?

                                @JKSH , of course, and if it couldn't then I would expect an error.

                                {
                                    "a": "TruE",
                                    "b": true
                                }
                                

                                Suppose robjRequest contains the above. Tell us what each of these should return:

                                1. robjRequest["a"].toArray()
                                2. robjRequest["a"].toObject()
                                3. robjRequest["a"].toString()
                                4. robjRequest["a"].toInt()
                                5. robjRequest["a"].toBool()
                                6. robjRequest["b"].toArray()
                                7. robjRequest["b"].toObject()
                                8. robjRequest["b"].toString()
                                9. robjRequest["b"].toInt()
                                10. robjRequest["b"].toBool()

                                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                S 1 Reply Last reply 11 Dec 2020, 14:05
                                0
                                • J JKSH
                                  11 Dec 2020, 13:43

                                  @SPlatten said in Failing to extract value from JSON why?:

                                  normally I always check the return of a function, I will revisit this code.

                                  Good. So check if toString() returns a null string -- that's your error, as @JonB said.

                                  the name of the function is toString, so I expect the result to be whatever is on the left converted to a string.

                                  Following that logic, would you also always expect QJsonValue::toArray() to convert your value to an array, and QJsonValue::toObject() > to convert your value to an object, and QJsonValue::toInt() to convert your value to an integer? Even if the value is not an array or an object or a number?

                                  @JKSH , of course, and if it couldn't then I would expect an error.

                                  {
                                      "a": "TruE",
                                      "b": true
                                  }
                                  

                                  Suppose robjRequest contains the above. Tell us what each of these should return:

                                  1. robjRequest["a"].toArray()
                                  2. robjRequest["a"].toObject()
                                  3. robjRequest["a"].toString()
                                  4. robjRequest["a"].toInt()
                                  5. robjRequest["a"].toBool()
                                  6. robjRequest["b"].toArray()
                                  7. robjRequest["b"].toObject()
                                  8. robjRequest["b"].toString()
                                  9. robjRequest["b"].toInt()
                                  10. robjRequest["b"].toBool()
                                  S Offline
                                  S Offline
                                  SPlatten
                                  wrote on 11 Dec 2020, 14:05 last edited by
                                  #22

                                  @JKSH , this is just silly, the name of the function is toSomething, so the name implies that it will convert whatever to the 'something', at least that is exactly how I would code it and expect it to work.

                                  Kind Regards,
                                  Sy

                                  J K 2 Replies Last reply 11 Dec 2020, 14:18
                                  0
                                  • S SPlatten
                                    11 Dec 2020, 14:05

                                    @JKSH , this is just silly, the name of the function is toSomething, so the name implies that it will convert whatever to the 'something', at least that is exactly how I would code it and expect it to work.

                                    J Offline
                                    J Offline
                                    JKSH
                                    Moderators
                                    wrote on 11 Dec 2020, 14:18 last edited by JKSH 12 Nov 2020, 14:19
                                    #23

                                    @SPlatten said in Failing to extract value from JSON why?:

                                    @JKSH , this is just silly, the name of the function is toSomething, so the name implies that it will convert whatever to the 'something', at least that is exactly how I would code it and expect it to work.

                                    I don't understand how you expect toArray() to work on "TruE" or true.

                                    Consistency in an API is important. QJsonValue::toArray() only works on 'something' when that something is an array. Likewise, QJsonValue::toString() only works on 'something' when that something is a string.

                                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                    S 1 Reply Last reply 11 Dec 2020, 14:27
                                    2
                                    • J JKSH
                                      11 Dec 2020, 14:18

                                      @SPlatten said in Failing to extract value from JSON why?:

                                      @JKSH , this is just silly, the name of the function is toSomething, so the name implies that it will convert whatever to the 'something', at least that is exactly how I would code it and expect it to work.

                                      I don't understand how you expect toArray() to work on "TruE" or true.

                                      Consistency in an API is important. QJsonValue::toArray() only works on 'something' when that something is an array. Likewise, QJsonValue::toString() only works on 'something' when that something is a string.

                                      S Offline
                                      S Offline
                                      SPlatten
                                      wrote on 11 Dec 2020, 14:27 last edited by
                                      #24

                                      @JKSH , thanks, I really can't be bothered to debate this any further.

                                      Kind Regards,
                                      Sy

                                      1 Reply Last reply
                                      0
                                      • S SPlatten
                                        11 Dec 2020, 14:05

                                        @JKSH , this is just silly, the name of the function is toSomething, so the name implies that it will convert whatever to the 'something', at least that is exactly how I would code it and expect it to work.

                                        K Offline
                                        K Offline
                                        kshegunov
                                        Moderators
                                        wrote on 11 Dec 2020, 19:04 last edited by
                                        #25

                                        @SPlatten said in Failing to extract value from JSON why?:

                                        @JKSH , this is just silly, the name of the function is toSomething, so the name implies that it will convert whatever to the 'something', at least that is exactly how I would code it and expect it to work.

                                        No, it isn't that silly, actually it's very much on point.

                                        An API, which is what Qt provides, is a contract (the technical idiom is "Design by contract") - you give us this, and we give you that in exchange. You broke the contract, and you didn't bother to read the contract, and that's all fine with me, but to add insult to injury - you're claiming it's not worth reading the contract. Sorry, that ain't gonna fly!

                                        If you're not up to speed on what I'm babbling about, well the documentation is that contract, if a function name was all and enough we wouldn't bother to write documentation at all.

                                        Read and abide by the Qt Code of Conduct

                                        1 Reply Last reply
                                        1

                                        16/25

                                        11 Dec 2020, 11:19

                                        • Login

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