1

I am digitally signing json in javascript to be verified by c++. I am using json spirit to parse the json which uses maps for json Objects, so the Objects need to be sorted in the same way as the c++ maps.

An Object can easily be sorted by javascript

function sortObject(o) {
    var sorted = {},
    key, a = [];

    for (key in o) {
        if (o.hasOwnProperty(key)) {
            a.push(key);
        }
    }

    a.sort();

    for (key = 0; key < a.length; key++) {
        sorted[a[key]] = o[a[key]];
    }
    return sorted;
}

however, I am unsure of the exact sorting method that c++ uses.

Is the above code sufficient for javascript to sort Object keys in the same manner that map keys are sorted? If not, how can this be done?

Community
  • 1
  • 1
  • 1
    An object's order is not guaranteed.... – epascarello Apr 11 '14 at 16:09
  • @epascarello It looks like most browsers order by insertion except for Chrome which did/does order by integers when available. –  Apr 11 '14 at 18:55
  • Why don't you sign (and verify) the binary representation after encoding (and before parsing respectively)? That should avoid such sorting problems. – Perseids Apr 12 '14 at 14:27
  • @Perseids I was thinking about that, but json-spirit doesn't seem to be able to treat objects as strings, and the signature data (message, key, signature) is a child object. I've tested with Chrome, and I can confirm it does maintain order by insertion. Can't say the same for other browsers :/ –  Apr 12 '14 at 15:20
  • How about some nested serialization? Serialize the payload object as json and create a new object that contains only the signature and the serialized payload and serialize that one again for transfer. On the receiving end you deserialize the outer json, check the signature, and deserialize the payload. – Perseids Apr 12 '14 at 18:03
  • @Perseids Yep, I was definitely thinking about that until I saw all of the escape characters, lol. I just hope they keep maintaining order based upon insertion. :/ –  Apr 12 '14 at 18:05
  • I would argue that the more simple code and stability against future browser changes is probably more valuable than the transmission overhead from the double deserialization, but that is for you to decide ;) – Perseids Apr 12 '14 at 18:09

1 Answers1

1

You are basically asking this question. As the top answer states:

Yes, that's [the iteration order is] guaranteed. Moreover, *begin() gives you the smallest and *rbegin() the largest element, as determined by the comparison operator, and two key values a and b for which the expression !compare(a,b) && !compare(b,a) is true are considered equal. The default comparison function is std::less.

Community
  • 1
  • 1
niklasfi
  • 15,245
  • 7
  • 40
  • 54
  • Thank you niklasfi! Is it equivalent to the default `Array.sort` function in js? Thank you so much in advance! –  Apr 12 '14 at 14:16
  • 1
    @Gracchus the order is the one specified in the map template argument http://www.cplusplus.com/reference/map/map/. By default for a string that would be a lexicographical sort. – niklasfi Apr 12 '14 at 14:21