Statistics
| Branch: | Tag: | Revision:

root / tools / fmtjson @ ea2bcb82

History | View | Annotate | Download (1.2 kB)

1 d62cbd3a Michael Hanselmann
#!/usr/bin/python
2 d62cbd3a Michael Hanselmann
#
3 d62cbd3a Michael Hanselmann
4 d62cbd3a Michael Hanselmann
# Copyright (C) 2011 Google Inc.
5 d62cbd3a Michael Hanselmann
#
6 d62cbd3a Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 d62cbd3a Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 d62cbd3a Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 d62cbd3a Michael Hanselmann
# (at your option) any later version.
10 d62cbd3a Michael Hanselmann
#
11 d62cbd3a Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 d62cbd3a Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 d62cbd3a Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 d62cbd3a Michael Hanselmann
# General Public License for more details.
15 d62cbd3a Michael Hanselmann
#
16 d62cbd3a Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 d62cbd3a Michael Hanselmann
# along with this program; if not, write to the Free Software
18 d62cbd3a Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 d62cbd3a Michael Hanselmann
# 02110-1301, USA.
20 d62cbd3a Michael Hanselmann
21 d62cbd3a Michael Hanselmann
"""Tool to format JSON data.
22 d62cbd3a Michael Hanselmann
23 d62cbd3a Michael Hanselmann
"""
24 d62cbd3a Michael Hanselmann
25 d62cbd3a Michael Hanselmann
import sys
26 d62cbd3a Michael Hanselmann
import simplejson
27 d62cbd3a Michael Hanselmann
28 d62cbd3a Michael Hanselmann
29 d62cbd3a Michael Hanselmann
def main():
30 d62cbd3a Michael Hanselmann
  """Main routine.
31 d62cbd3a Michael Hanselmann
32 d62cbd3a Michael Hanselmann
  """
33 d62cbd3a Michael Hanselmann
  if len(sys.argv) > 1:
34 d62cbd3a Michael Hanselmann
    sys.stderr.write("Read JSON data from standard input and write a"
35 d62cbd3a Michael Hanselmann
                     " formatted version on standard output. There are"
36 d62cbd3a Michael Hanselmann
                     " no options or arguments.\n")
37 d62cbd3a Michael Hanselmann
    sys.exit(1)
38 d62cbd3a Michael Hanselmann
39 d62cbd3a Michael Hanselmann
  data = simplejson.load(sys.stdin)
40 d62cbd3a Michael Hanselmann
  simplejson.dump(data, sys.stdout, indent=2, sort_keys=True)
41 d62cbd3a Michael Hanselmann
  sys.stdout.write("\n")
42 d62cbd3a Michael Hanselmann
43 d62cbd3a Michael Hanselmann
44 d62cbd3a Michael Hanselmann
if __name__ == "__main__":
45 d62cbd3a Michael Hanselmann
  main()