Loading ...
Sorry, an error occurred while loading the content.

Order of members and equality

Expand Messages
  • jonathan wallace
    I received this email today: =============== I am testing out using libjson in our project and am looking for a little guidance. I expected that when comparing
    Message 1 of 5 , Oct 9, 2012
    View Source
    • 0 Attachment
      I received this email today:

      ===============
      I am testing out using libjson in our project and am looking for a little guidance. I expected that when comparing two JSONNode objects via operator==, it would ignore ordering. However, this does not seem to be the case.  For example:

      JSONNode node1 = libjson::parse("{\"uid\":\"mopub_id\", \"uid1\":\"mopub_id1\"}");
      JSONNode node2 = libjson::parse("{\"uid1\":\"mopub_id1\", \"uid\":\"mopub_id\"}");
      if (node1 == node2) { std::cout << "equal" << std::endl;
      } else { std::cout << "not equal" << std::endl;
      }
      I had expected that this would call node1 and node2 equal. However, it does not appear to. Is this the intended behavior? Is there anything in the code to compare JSONNode s w/o respect to order of elements?

      ===============


      Should those two nodes be considered equal to each other?  I am certain that order matters when it comes to arrays, and I am fairly certain that it is for members as well, am I wrong?

      [Non-text portions of this message have been removed]
    • John Cowan
      ... I think you are wrong, and that order does not matter when it comes to objects, though the JSON RFC is characteristically silent on the point. JSON objects
      Message 2 of 5 , Oct 9, 2012
      View Source
      • 0 Attachment
        jonathan wallace scripsit:

        > Should those two nodes be considered equal to each other?  I am
        > certain that order matters when it comes to arrays, and I am fairly
        > certain that it is for members as well, am I wrong?

        I think you are wrong, and that order does not matter when it comes to
        objects, though the JSON RFC is characteristically silent on the point.
        JSON objects are basically JavaScript objects, and it's clear that in
        JavaScript there is no ordering information available, so anyone who
        depended on the order of members wouldn't be able to recover it.

        How are you representing JSON objects internally? If they are just C++
        arrays, then the naive algorithm for order-independent equality is O(N^2),
        which is rather larger than a C++ programmer would expect. Sorting the
        objects at time of comparison drops the cost to O(N log N). (Still,
        typical JSON objects may be small enough that constant factors dominate.)

        --
        Take two turkeys, one goose, four John Cowan
        cabbages, but no duck, and mix them http://www.ccil.org/~cowan
        together. After one taste, you'll duck cowan@...
        soup the rest of your life.
        --Groucho
      • Jonathan Wallace
        Interesting, internally they use a custom container, but has named access time of O(n) so unordered comparison would end up being arithmetic sum of n. I
        Message 3 of 5 , Oct 10, 2012
        View Source
        • 0 Attachment
          Interesting, internally they use a custom container, but has named access time of O(n) so unordered comparison would end up being arithmetic sum of n. I figured in JavaScript it wouldn't matter, but there are lots of people in other languages that use Json in other ways. If the rfc is silent on it, then it would be library specific and not to be relied on. Perhaps I will add a seperate function for this.

          Sent from my iPhone

          On Oct 9, 2012, at 10:28, John Cowan <cowan@...> wrote:

          > jonathan wallace scripsit:
          >
          > > Should those two nodes be considered equal to each other? I am
          > > certain that order matters when it comes to arrays, and I am fairly
          > > certain that it is for members as well, am I wrong?
          >
          > I think you are wrong, and that order does not matter when it comes to
          > objects, though the JSON RFC is characteristically silent on the point.
          > JSON objects are basically JavaScript objects, and it's clear that in
          > JavaScript there is no ordering information available, so anyone who
          > depended on the order of members wouldn't be able to recover it.
          >
          > How are you representing JSON objects internally? If they are just C++
          > arrays, then the naive algorithm for order-independent equality is O(N^2),
          > which is rather larger than a C++ programmer would expect. Sorting the
          > objects at time of comparison drops the cost to O(N log N). (Still,
          > typical JSON objects may be small enough that constant factors dominate.)
          >
          > --
          > Take two turkeys, one goose, four John Cowan
          > cabbages, but no duck, and mix them http://www.ccil.org/~cowan
          > together. After one taste, you'll duck cowan@...
          > soup the rest of your life.
          > --Groucho
          >


          [Non-text portions of this message have been removed]
        • douglascrockford
          ... An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or
          Message 4 of 5 , Oct 12, 2012
          View Source
          • 0 Attachment
            --- In json@yahoogroups.com, John Cowan <cowan@...> wrote:

            > I think you are wrong, and that order does not matter when it comes to
            > objects, though the JSON RFC is characteristically silent on the point.


            An object is an unordered collection of zero or more name/value
            pairs, where a name is a string and a value is a string, number,
            boolean, null, object, or array.


            http://www.ietf.org/rfc/rfc4627.txt?number=4627
          • John Cowan
            ... Touche. And thanks. -- [W]hen I wrote it I was more than a little John Cowan febrile with foodpoisoning from an antique carrot
            Message 5 of 5 , Oct 12, 2012
            View Source
            • 0 Attachment
              douglascrockford scripsit:

              > > I think you are wrong, and that order does not matter when it comes to
              > > objects, though the JSON RFC is characteristically silent on the point.
              >
              > An object is an unordered collection of zero or more name/value
              > pairs, where a name is a string and a value is a string, number,
              > boolean, null, object, or array.

              Touche. And thanks.

              --
              [W]hen I wrote it I was more than a little John Cowan
              febrile with foodpoisoning from an antique carrot cowan@...
              that I foolishly ate out of an illjudged faith http://ccil.org/~cowan
              in the benignancy of vegetables. --And Rosta
            Your message has been successfully submitted and would be delivered to recipients shortly.