Source

simpliFiRE.IDAscope / idascope / core / helpers / JsonHelper.py

#!/usr/bin/python
# JSON decoding hooks by Mike Brennan / Stack Overflow
# http://stackoverflow.com/questions/956867/how-to-get-string-objects-instead-unicode-ones-from-json-in-python

"""
This module allows using the Python json module for parsing JSON data and
decoding strings in UTF-8 instead of Unicode, resulting in ability to use str functions (e.g. for sorting)
"""


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