JSON is parsed in Python while the custom code handles the instance

  • 2020-04-02 14:32:57
  • OfStack

In the case of JSON deserialization of file contents or strings, it may be necessary to encode the deserialized contents (such as converting unicode objects to STR) due to raw content encoding issues.

In Python, one way is to deserialize a dict object using json.load or json.loads, and then code the dict object.

But in json.load and json.loads, there is an optional parameter, object_hook. By using this parameter, the dict obtained from deserialization can be processed directly, and the new dict after processing can be used to replace the original dict.

Usage:


d = json.loads(json_str, object_hook=_decode_dict)

Attached are _decode_dict and _decode_list used in Shadowsocks:


def _decode_list(data):
    rv = []
    for item in data:
        if isinstance(item, unicode):
            item = item.encode('utf-8')
        elif isinstance(item, list):
            item = _decode_list(item)
        elif isinstance(item, dict):
            item = _decode_dict(item)
        rv.append(item)
    return rv
 
def _decode_dict(data):
    rv = {}
    for key, value in data.iteritems():
        if isinstance(key, unicode):
            key = key.encode('utf-8')
        if isinstance(value, unicode):
            value = value.encode('utf-8')
        elif isinstance(value, list):
            value = _decode_list(value)
        elif isinstance(value, dict):
            value = _decode_dict(value)
        rv[key] = value
    return rv

Reference:
1. https://docs.python.org/2/library/json.html
2. https://github.com/clowwindy/shadowsocks/blob/master/shadowsocks/utils.py


Related articles: