Package IDAscope :: Package idascope :: Package core :: Module JsonHelper
[hide private]
[frames] | no frames]

Source Code for Module IDAscope.idascope.core.JsonHelper

 1  #!/usr/bin/python 
 2  # JSON decoding hooks by Mike Brennan / Stack Overflow 
 3  # http://stackoverflow.com/questions/956867/how-to-get-string-objects-instead-unicode-ones-from-json-in-python 
 4   
 5  """ 
 6  This module allows using the Python json module for parsing JSON data and 
 7  decoding strings in UTF-8 instead of Unicode, resulting in ability to use str functions (e.g. for sorting) 
 8  """ 
 9   
10   
11 -def decode_list(data):
12 rv = [] 13 for item in data: 14 if isinstance(item, unicode): 15 item = item.encode('utf-8') 16 elif isinstance(item, list): 17 item = decode_list(item) 18 elif isinstance(item, dict): 19 item = decode_dict(item) 20 rv.append(item) 21 return rv
22 23
24 -def decode_dict(data):
25 rv = {} 26 for key, value in data.iteritems(): 27 if isinstance(key, unicode): 28 key = key.encode('utf-8') 29 if isinstance(value, unicode): 30 value = value.encode('utf-8') 31 elif isinstance(value, list): 32 value = decode_list(value) 33 elif isinstance(value, dict): 34 value = decode_dict(value) 35 rv[key] = value 36 return rv
37