Advertisement
Guest User

Untitled

a guest
Feb 1st, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. class ContentHandler(YajlContentHandler):
  2.     def __init__(self):
  3.         self.out = sys.stdout
  4.         self.json_list = []
  5.         self.json_object = {}
  6.         self.key, self.val = '', ''
  7.         pass
  8.  
  9.     def yajl_null(self, ctx):
  10.         pass
  11.  
  12.     def yajl_boolean(self, ctx, boolVal):
  13.         pass
  14.  
  15.     def yajl_integer(self, ctx, integerVal):
  16.         self.out.write(integerVal)
  17.         pass
  18.  
  19.     def yajl_double(self, ctx, doubleVal):
  20.         self.out.write(doubleVal)
  21.         pass
  22.  
  23.     def yajl_number(self, ctx, stringNum):
  24.         stringNum = stringNum.decode('utf-8')
  25.         num = float(stringNum) if '.' in stringNum else int(stringNum)
  26.         self.json_object[self.key.decode('utf-8')] = num
  27.         pass
  28.  
  29.     def yajl_string(self, ctx, stringVal):
  30.         self.json_object[self.key.decode('utf-8')] = stringVal.decode('utf-8')
  31.         pass
  32.  
  33.     def yajl_start_map(self, ctx):
  34.         pass
  35.  
  36.     def yajl_map_key(self, ctx, stringVal):
  37.         self.key = stringVal
  38.         pass
  39.  
  40.     def yajl_end_map(self, ctx):
  41.         self.json_list.append(self.json_object)
  42.         self.json_object = {}
  43.         pass
  44.  
  45.     def yajl_start_array(self, ctx):
  46.         self.json_list = []
  47.         pass
  48.  
  49.     def yajl_end_array(self, ctx):
  50.         pass
  51.  
  52. obj = ContentHandler() # the class with callback methods to store json into object
  53. parser = YajlParser(obj)
  54. f = gzip.open(json_file, 'rb')
  55. parser.parse(f)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement