Fix Mojo and Mandoline builds.
[chromium-blink-merge.git] / tools / json_schema_compiler / json_parse.py
blob21a5a8f6c593de38d69c8e6e1a521c52fafcecc8
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 import json
6 import os
7 import sys
9 _FILE_PATH = os.path.dirname(os.path.realpath(__file__))
10 _SYS_PATH = sys.path[:]
11 try:
12 _COMMENT_EATER_PATH = os.path.join(
13 _FILE_PATH, os.pardir, 'json_comment_eater')
14 sys.path.insert(0, _COMMENT_EATER_PATH)
15 import json_comment_eater
16 finally:
17 sys.path = _SYS_PATH
19 try:
20 from collections import OrderedDict
22 # Successfully imported, so we're running Python >= 2.7, and json.loads
23 # supports object_pairs_hook.
24 def Parse(json_str):
25 return json.loads(json_comment_eater.Nom(json_str),
26 object_pairs_hook=OrderedDict)
28 except ImportError:
29 # Failed to import, so we're running Python < 2.7, and json.loads doesn't
30 # support object_pairs_hook. simplejson however does, but it's slow.
32 # TODO(cduvall/kalman): Refuse to start the docs server in this case, but
33 # let json-schema-compiler do its thing.
34 #logging.warning('Using simplejson to parse, this might be slow! Upgrade to '
35 # 'Python 2.7.')
37 _SYS_PATH = sys.path[:]
38 try:
39 _SIMPLE_JSON_PATH = os.path.join(_FILE_PATH,
40 os.pardir,
41 os.pardir,
42 'third_party')
43 sys.path.insert(0, _SIMPLE_JSON_PATH)
44 # Add this path in case this is being used in the docs server.
45 sys.path.insert(0, os.path.join(_FILE_PATH,
46 os.pardir,
47 os.pardir,
48 'third_party',
49 'json_schema_compiler'))
50 import simplejson
51 from simplejson import OrderedDict
52 finally:
53 sys.path = _SYS_PATH
55 def Parse(json_str):
56 return simplejson.loads(json_comment_eater.Nom(json_str),
57 object_pairs_hook=OrderedDict)
60 def IsDict(item):
61 return isinstance(item, (dict, OrderedDict))