Travis integration TBD
jsontree is a simple module for quickly building manipulating and modifying rich json data in python.
Datetime objects are serialized out ti the ISO format which is easilly used in javascript. ISO formatted datetime strings will be deserialized into datetime objects.
import jsontree
import datetime
data = jsontree.jsontree()
data.username = 'doug'
data.meta.date = datetime.datetime.now()
data.somethingelse = [1,2,3]
data['username'] == 'doug'
ser = jsontree.dumps(data)
backagain = jsontree.loads(ser)
cloned = jsontree.clone(data)
JSON Tree Library
Bases: json.decoder.JSONDecoder
JSON decoder class for deserializing to a jsontree object structure and building datetime objects from strings with the ISO datetime format.
Return the Python representation of s (a str or unicode instance containing a JSON document)
Decode a JSON document from s (a str or unicode beginning with a JSON document) and return a 2-tuple of the Python representation and the index in s where the document ended.
This can be used to decode a JSON document from a string that may have extraneous data at the end.
Bases: json.encoder.JSONEncoder
JSON encoder class that serializes out jsontree object structures and datetime objects into ISO strings.
Return a JSON string representation of a Python data structure.
>>> JSONEncoder().encode({"foo": ["bar", "baz"]})
'{"foo": ["bar", "baz"]}'
Encode the given object and yield each string representation as available.
For example:
for chunk in JSONEncoder().iterencode(bigobject):
mysocket.write(chunk)
Clone an object by first searializing out and then loading it back in.
JSON serialize to file function that defaults the encoding class to be JSONTreeEncoder
JSON serialize to string function that defaults the encoding class to be JSONTreeEncoder
Bases: collections.defaultdict
Default dictionary where keys can be accessed as attributes and new entries recursively default to be this class. This means the following code is valid:
>>> mytree = jsontree()
>>> mytree.something.there = 3
>>> mytree['something']['there'] == 3
True
Factory for default value called by __missing__().
v defaults to None.
If key is not found, d is returned if given, otherwise KeyError is raised
2-tuple; but raise KeyError if D is empty.
If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]
JSON load from file function that defaults the loading class to be JSONTreeDecoder
JSON load from string function that defaults the loading class to be JSONTreeDecoder
Helper function that calls mapped_jsontree_class, and passing the rest of the arguments to the constructor of the new class.
>>> number = mapped_jsontree(dict(one='1', two='2', three='3', four='4'),
... {'1': 'something', '2': 'hello'})
>>> number.two
'hello'
>>> number.items()
[('1', 'something'), ('2', 'hello')]
Return a class which is a jsontree, but with a supplied attribute name mapping. The mapping argument can be a mapping object (dict, jsontree, etc.) or it can be a callable which takes a single argument (the attribute name), and returns a new name.
This is useful in situations where you have a jsontree with keys that are not valid python attribute names, to simplify communication with a client library, or allow for configurable names.
For example:
>>> numjt = mapped_jsontree_class(dict(one='1', two='2', three='3'))
>>> number = numjt()
>>> number.one = 'something'
>>> number
defaultdict(<class 'jsontree.mapped_jsontree'>, {'1': 'something'})
This is very useful for abstracting field names that may change between a development sandbox and production environment. Both FogBugz and Jira bug trackers have custom fields with dynamically generated values. These field names can be abstracted out into a configruation mapping, and the jsontree code can be standardized.
This can also be iseful for JavaScript API’s (PHPCake) which insist on having spaces in some key names. A function can be supplied which maps all ‘_’s in the attribute name to spaces:
>>> spacify = lambda name: name.replace('_', ' ')
>>> spacemapped = mapped_jsontree_class(spacify)
>>> sm = spacemapped()
>>> sm.hello_there = 5
>>> sm.hello_there
5
>>> sm.keys()
['hello there']
This will also work with non-string keys for translating from libraries that use object keys in python over to string versions of the keys in JSON
>>> numjt = mapped_jsontree_class(dict(one=1, two=2))
>>> number = numjt()
>>> number.one = 'something'
>>> number
defaultdict(<class 'jsontree.mapped_jsontree'>, {1: 'something'})
>>> numjt_as_text = mapped_jsontree_class(dict(one='1', two='2'))
>>> dumped_number = dumps(number)
>>> loaded_number = loads(dumped_number, jsontreecls=numjt_as_text)
>>> loaded_number.one
'something'
>>> loaded_number
defaultdict(<class 'jsontree.mapped_jsontree'>, {'1': 'something'})