A JSON encoder that allows for more common Python data types.
In addition to the defaults handled by json, this also supports:
- datetime.datetime
- datetime.date
- datetime.time
- decimal.Decimal
Given a lookup string, attempts to descend through nested data looking for the value.
Can work with either dictionary-alikes or objects (or any combination of those).
Lookups should be a string. If it is a dotted path, it will be split on . & it will traverse through to find the final value. If not, it will simply attempt to find either a key or attribute of that name & return it.
Example:
>>> data = {
... 'type': 'message',
... 'greeting': {
... 'en': 'hello',
... 'fr': 'bonjour',
... 'es': 'hola',
... },
... 'person': Person(
... name='daniel'
... )
... }
>>> lookup_data('type', data)
'message'
>>> lookup_data('greeting.en', data)
'hello'
>>> lookup_data('person.name', data)
'daniel'