Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.31 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. try:
  4.     basestring
  5. except NameError:
  6.     basestring = str
  7.  
  8.  
  9. def pretty_print_json(jdata):
  10.     buf = []
  11.     _pp_dict(jdata, 0, buf)
  12.     return "".join(buf)
  13.  
  14. def _pp_dict(dct, depth, buf):
  15.     space = "  " * depth
  16.     buf.append('{\n')
  17.     for k, v in dct.items():
  18.         buf.append('%s  "%s": ' % (space, k))  # TODO: escape k
  19.         if isinstance(v, list):
  20.             _pp_list(v, depth+1, buf)
  21.         elif isinstance(v, dict):
  22.             _pp_dict(v, depth+1, buf)
  23.         else:
  24.             _pp_scalar(v, depth+1, buf)
  25.         buf.append(',\n')
  26.     if buf[-1] == ',\n':
  27.         buf.pop()
  28.         buf.append('\n')
  29.     buf.append(space)
  30.     buf.append('}')
  31.  
  32. def _pp_list(lst, depth, buf):
  33.     space = "  " * depth
  34.     buf.append('[\n')
  35.     for x in lst:
  36.         buf.append(space)
  37.         if isinstance(x, list):
  38.             _pp_list(x, depth+1, buf)
  39.         elif isinstance(x, dict):
  40.             buf.append('  ')
  41.             _pp_dict(x, depth+1, buf)
  42.         else:
  43.             _pp_scalar(x, depth+1, buf)
  44.         buf.append(',\n')
  45.     if buf[-1] == ',\n':
  46.         buf.pop()
  47.         buf.append('\n')
  48.     buf.append(space)
  49.     buf.append(']')
  50.  
  51. def _pp_scalar(scalar, depth, buf):
  52.     buf.append(_repr_scalar(scalar))
  53.  
  54. def _repr_scalar(scalar):
  55.     if scalar is None:
  56.         return "null"
  57.     elif scalar is True:
  58.         return "true"
  59.     if scalar is False:
  60.         return "false"
  61.     if isinstance(scalar, basestring):
  62.         return '"%s"' % scalar.replace('"', r'\"')
  63.     if isinstance(scalar, (int, float)):
  64.         return str(scalar)
  65.     raise TypeError("%r: unknown data type" % (scalar,))
  66.  
  67.  
  68. if __name__ == '__main__':
  69.     jdata = {
  70.       "books": [
  71.         {
  72.            "id": 101,
  73.            "title": u"すごい本",
  74.            "authors": [
  75.              {
  76.                "id": 2001,
  77.                "name": u"著者1"
  78.              },
  79.              {
  80.                "id": 2001,
  81.                "name": u"著者2"
  82.              }
  83.            ]
  84.         },
  85.         {
  86.           "id": 102,
  87.           "title": u"もっとすごい本",
  88.           "authors": [
  89.             {
  90.               "id": 2103,
  91.               "name": u"著者3"
  92.             }
  93.           ]
  94.         }
  95.       ]
  96.     }
  97.     s = pretty_print_json(jdata)
  98.     print(s.encode('utf-8'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement