blob: 401594d85cd4f7b6e8534888c2dac78039409b93 [file] [log] [blame]
#!/usr/bin/env python
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import argparse
import json
import os
import sys
def GetFormattedJSONString(file_path):
with open(file_path, 'r') as f:
json_obj = json.load(f)
file_content =
return json.dumps(
json_obj, indent=2, sort_keys=True, separators=(',', ': '))
def ValidateJSONFormat(file_path):
with open(file_path, 'r') as f:
file_content =
if file_content != GetFormattedJSONString(file_path):
raise Exception(
'Reformat your JSON file by running: %s --format %s' %
(__file__, file_path))
print >> sys.stdout, ('%s passes the JSON format validation' % file_path)
def Format(file_path):
formatted_JSON_string = GetFormattedJSONString(file_path)
with open(file_path, 'w') as f:
def Main(args):
description = """A JSON formatting tool.
This is a tool that validate and reformats JSON file so that it complies with
a certain style. The JSON style imposed by this tool is:
* JSON array elements and object members are indented with 2 spaces.
* Dictionaries objects are sorted by key.
* Items are sperated by ', ' and ': '.
parser = argparse.ArgumentParser(
description=description, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('file_path', type=str, help='The path to JSON file.')
parser.add_argument('--format', action='store_true', default=False,
help='Format the JSON file.')
options = parser.parse_args(args)
if options.format:
return 0
return 0
if __name__ == '__main__':