Comments (2)
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.
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)
- Lint returning 0 for erroring geojson HOT 4
- wheels for demjson HOT 4
- trailing whitespace
- high cpu load
- enum encode error:
- python -m demjson
- jsonlint changes foat values HOT 8
- Version on CentOS EPEL is out of date and exits 0 on errors HOT 2
- --allow=non-bmp HOT 4
- Code bug? HOT 2
- Failed to import under Python 2.7.13
- Documentation page is not opening HOT 2
- deron.meranda.us is Down, Causing Pip Installs to Fail HOT 1
- Importing ABC directly from collections module was removed in Python 3.9
- “Gibberish” false positive when parsing Unicode identifiers in objects HOT 4
- demjson.JSONDecodeError: ('Bad number', '.')
- syntax error
- To support numpy?
- Setuptools 58.0.0 has removed support for 2to3 during builds, breaks demjson for Python 3.x HOT 8
- demjson pip install fails with python3.7 and 3.7 but not 3.6. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from demjson.