python - Different json encoders for different "depths" -


i write json encoder knows when expand object, or leave in abbreviated form.

the object have, has many properties, of collections of other objects (which in turn have own properties may collections, ad infinitum). json.dumps method return large strings if recursed through objects.

is possible set "depth" objects found after depth not expanded further?

first, should figure out whether you're getting hard disk savings here. don't think in percent difference, dollar cost of diskspace people running program.

second, figured out hacky way of doing this:

copy folder /lib/json/ folder outside of python install. name folder like, say, jsondepth

go encoder.py , make 3 changes (line numbers python 2.7):

line 411, change

def _iterencode(o, _current_indent_level):     if isinstance(o, basestring):         yield _encoder(o) 

to

def _iterencode(o, _current_indent_level):     if(_current_indent_level > skip_indent_level):         yield 'null'         return     if isinstance(o, basestring):         yield _encoder(o) 

line 335, change

def _iterencode_dict(dct, _current_indent_level):     if not dct:         yield '{}' 

to

def _iterencode_dict(dct, _current_indent_level):     if(_current_indent_level > skip_indent_level):         yield 'null'         return     if not dct:         yield '{}' 

line 282, change

def _iterencode_list(lst, _current_indent_level):     if not lst:         yield '[]' 

to

def _iterencode_list(lst, _current_indent_level):     if(_current_indent_level > skip_indent_level):         yield 'null'         return     if not lst:         yield '[]' 

line 5, change

try:     _json import encode_basestring_ascii c_encode_basestring_ascii except importerror:     c_encode_basestring_ascii = none try:     _json import make_encoder c_make_encoder except importerror:     c_make_encoder = none 

to

c_encode_basestring_ascii = none c_make_encoder = none 

somewhere @ top of file, (say line 13)

skip_indent_level = 4 # or whatever recursion depth want. 

note indentation must enabled, or falls apart. note didn't test this. no warranty expressed or implied :)


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -