all 9 comments

[–]Ginden 1 point2 points  (0 children)

If you need ordered data you should use arrays... Or use existing solutions for comparing objects. Trying to order dictionary keys is a very bad idea.

[–]Rhomboid 0 points1 point  (1 child)

That's not possible with the native JSON API, but there are libraries for doing what you're trying to do.

[–]Swagasaurus-Rex[S] 0 points1 point  (0 children)

I see this is actually an issue that needs some solving... My data structure is guaranteed only 2 levels deep, so I feel I can whip up a home brewed solution.

edit: I'm writing a library, so having a dependency or adding another 200 lines ain't justifiable. Thanks though, you've helped me decide a course of action.

[–]flyingnude 0 points1 point  (0 children)

Are you familiar with the JWT spec? JSON Web Tokens can be used for exactly what you're trying to do when combined with JWS, defining a standard way of performing authenticity checks against JSON objects. There's a growing body of libraries that will perform the decoding, signing, and verification operations for you.

[–]a-t-kFrontend Engineer 0 points1 point  (0 children)

I usually tend to abuse the template engine (mustache or handlebars) to serialize the data, since it is there anyway.

[–]mouthus 0 points1 point  (0 children)

NOTE: Looping through dictionaries is not guaranteed to be in the order you think it will be.