Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. def recursive_jsonify(item):
  2.     return "'" + make_json(item) + "'"
  3.     def make_json(item):
  4.         if not parseable(item):
  5.             return ""
  6.         if is_map(item):
  7.             return parse_map(item)
  8.         elif is_array(item):
  9.             return parse_array(item)
  10.         # otherwise, it's a primitive type or not parsable
  11.         # deal with this yourself!
  12.        
  13.     def parsable(item):
  14.         return type(item) in {bool, float, int, string, dict, list, NoneType} #dict <-> map, list <-> array
  15.  
  16.  
  17.     def parse_map(map):
  18.         inner_string = ""
  19.         for key in map:
  20.             value = map[key]
  21.             if type(key) in {bool, float, int} and parsable(value):
  22.                 inner_string += ','+make_json(key+)':'+make_json(value)
  23.         return '{' + inner_string + '}'
  24.    
  25.     def parse_array(array):
  26.         inner_string = ','.join(make_json(x) for x in item if parsable(x) else make_json(None)) # None <-> null
  27.         return '[' + inner_string + ']'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement