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. QJsonObject isUndefined is never undefined when applied to a copy

QJsonObject isUndefined is never undefined when applied to a copy

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 390 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.
  • L Offline
    L Offline
    lurchi
    wrote on last edited by
    #1

    I use Json files for initialisation of my program. Some variables are predefined by default, so if a certain tag is not in the init file I want to use these.
    Now, if I use QJsonObject init["sometag"].isUndefined() to check for existence of an initialisation value, this works if init is the original object.
    But after QJsonObject copy = init;
    copy["sometag"],isUndefined() returns always false, and "sometag": Null is inserted into copy.
    Same happens with
    QJsonObject subpart = init["subpart"]

    Is this a bug or a feature, and how can I work around this?

    Uli

    JonBJ 1 Reply Last reply
    0
    • L lurchi

      I use Json files for initialisation of my program. Some variables are predefined by default, so if a certain tag is not in the init file I want to use these.
      Now, if I use QJsonObject init["sometag"].isUndefined() to check for existence of an initialisation value, this works if init is the original object.
      But after QJsonObject copy = init;
      copy["sometag"],isUndefined() returns always false, and "sometag": Null is inserted into copy.
      Same happens with
      QJsonObject subpart = init["subpart"]

      Is this a bug or a feature, and how can I work around this?

      Uli

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @lurchi
      isUndefined() does something different from what you are thinking it does.

      You cannot index a QJsonObject by ["some arbitrary string"], so it won't get as far as the following isUndefined(). use bool QJsonObject::contains(const QString &key) const to check whether a key exists before trying to index by it.

      EDIT
      I realise this contradicts https://doc.qt.io/qt-5/qjsonobject.html#operator-5b-5d

      The returned QJsonValue is QJsonValue::Undefined if the key does not exist.

      and you are asking a question about behaviour after copying via QJsonObject copy = init; being different, is that right? I am rethinking.....

      EDIT2
      OK, if I understand right, your issue is going to be connected to this method: QJsonValueRef QJsonObject::operator[](const QString &key)

      Returns a reference to the value for key. If there is no value with key key in the object, one is created with a QJsonValue::Null value and then returned.

      Now, I think you will find that you calling

      init["sometag"].isUndefined()
      

      activates this. init["sometag"] had no such key initially, but executing this creates one with QJsonValue::Null value in the init object. (Verify that for yourself before versus after the call by using contains("sometag").) And then that gets copied to copy....

      Think about using what I said above about QJsonObject::contains(const QString &key) to avoid creating the non-existent key-value.

      P.S.
      If I am not mistaken: you have an initial QJsonObject, init. If you never wish to modify it and only make changes in copy try to declare init (or something referring to it) as const. That way should prevent you (compiler) calling init["sometag"] and having it create that key in init as an unwanted side-effect --- only the various const overrides of QJsonObject::operator[] etc. will be allowed?

      1 Reply Last reply
      3
      • L Offline
        L Offline
        lurchi
        wrote on last edited by
        #3

        I think you understood me right. I will try wtih the contains() method. This seems to not create a key, no matter whether the object is copied (referenced) or not. Thanks a lot.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lurchi
          wrote on last edited by
          #4

          Both proposed solutions work. Plus, I understood why it went wrong. Thank you!

          1 Reply Last reply
          0

          • Login

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