bradjasper/django-jsonfield — pull request #31 by dmr (2013-02-10 08:46:45+00:00)
Title: Ordering of OrderedDict lost with default settings

If I try to save and get an OrderedDict from a jsonfield, the ordering is lost when using default settings.

Took me a while to figure out how this works so it might help others.

I expected ordering to work like this:

from collections import OrderedDict
ordered_dict = OrderedDict([
('number', [1, 2, 3, 4]),
('notes', True),
])
expected_key_ordering = ['number', 'notes']

# Works as expected
assert ordered_dict.keys() == expected_key_ordering)
assert ordered_dict.keys() != dict(ordered_dict).keys()

from jsonfield.tests import JsonModel
mod = JsonModel.objects.create(json=ordered_dict)

# Works as expected
assert mod.schema.keys() == expected_key_ordering

mod_from_db = JsonModel.objects.get(id=mod.id)

# This will fail
self.assertEqual(mod_from_db.schema.keys(), expected_key_ordering)

To overcome this limitation the JSONField definition has to be adjusted:

class MyModel(models.Model):
json = jsonfield.JSONField(load_kwargs={'object_pairs_hook': collections.OrderedDict})

I'd like to contribute a test for this.
How do you run the tests or where did I miss the documentation?