Giter Club home page Giter Club logo

Comments (2)

dmeranda avatar dmeranda commented on July 1, 2024 1

Yes you can. There is an option, sort_keys, which you may provide to either the decode() or encode() function, or wrappers like encode_to_file().

Use sort_keys=demjson.SORT_PRESERVE to keep the same order. On decode()-ing it will create OrderedDict instances, on encoding it will preserve the order in an OrderedDicts. You can see an example of this in the included self test code (file "test/test_demjson.py"):

def testEncodeDictPreserveSorting(self):
        import collections
        d = collections.OrderedDict()
        d['X'] = 42
        d['A'] = 99
        d['Z'] = 50
        self.assertEqual(demjson.encode( d, sort_keys=demjson.SORT_PRESERVE ),
                         '{"X":42,"A":99,"Z":50}')

The various choices are:

  • SORT_ALPHA — Alphabetic (case-sensitive)
  • SORT_ALPHA_CI — Alphabetic (case-insensitive)
  • SORT_NONE — No sorting (random by Python hash ids)
  • SORT_PRESERVE — Keep order is possible (OrderedDict), or fall back to SORT_SMART
  • SORT_SMART — Case-insensitive, but catch numbers, etc.: "cat2" < "cat10"

There are many many different options that you can provide, not all of which are documented directly in the encode() function's help/docstring. Instead look at the documentation for the "json_options" class — See to 'sort_keys' descriptor (not method):
http://deron.meranda.us/python/demjson/demjson-2.2.3/docs/demjson.html#json_options

Did this solve your problem?

from demjson.

Kounavi avatar Kounavi commented on July 1, 2024

Yay! Thanks! Indeed, that did the trick :)
Thanks a lot for the immediate response & all the available options for that matter.
Much appreciated! :]

from demjson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.