Skip to content
Snippets Groups Projects
Commit f2dc73bd authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[schema] Import simplejson as json to make it easy to swap

parent 7392340e
No related branches found
No related tags found
1 merge request!64Factorize simplejson
...@@ -41,7 +41,7 @@ import collections ...@@ -41,7 +41,7 @@ import collections
import pkg_resources import pkg_resources
import six import six
import simplejson import simplejson as json
import jsonschema import jsonschema
...@@ -55,7 +55,7 @@ def maybe_load_json(s): ...@@ -55,7 +55,7 @@ def maybe_load_json(s):
with open(s, "rt") as f: with open(s, "rt") as f:
return maybe_load_json(f) return maybe_load_json(f)
else: else:
return simplejson.loads(s, object_pairs_hook=collections.OrderedDict) return json.loads(s, object_pairs_hook=collections.OrderedDict)
# if it is a 'file-like' object # if it is a 'file-like' object
if hasattr(s, "read"): if hasattr(s, "read"):
...@@ -94,7 +94,7 @@ def load_schema(schema_name, version=1): ...@@ -94,7 +94,7 @@ def load_schema(schema_name, version=1):
with open(fname, "rb") as f: with open(fname, "rb") as f:
data = f.read().decode() data = f.read().decode()
schema = simplejson.loads(data) schema = json.loads(data)
basedir = os.path.realpath(os.path.dirname(fname)) basedir = os.path.realpath(os.path.dirname(fname))
resolver = jsonschema.RefResolver("file://" + basedir + "/", schema) resolver = jsonschema.RefResolver("file://" + basedir + "/", schema)
...@@ -118,7 +118,7 @@ def validate(schema_name, data): ...@@ -118,7 +118,7 @@ def validate(schema_name, data):
try: try:
cleaned_data, error_list = validate('toolchain', '/to/my/file.json') cleaned_data, error_list = validate('toolchain', '/to/my/file.json')
except simplejson.JSONDecodeError as e: except json.JSONDecodeError as e:
print(e) print(e)
...@@ -133,12 +133,12 @@ def validate(schema_name, data): ...@@ -133,12 +133,12 @@ def validate(schema_name, data):
If ``data`` is a string and represents a valid filesystem path, the If ``data`` is a string and represents a valid filesystem path, the
relevant file will be opened and read as with relevant file will be opened and read as with
:py:func:`simplejson.load``. Otherwise, it will be considered to be :py:func:`json.load``. Otherwise, it will be considered to be
string containing a valid JSON structure that will be loaded as with string containing a valid JSON structure that will be loaded as with
:py:func:`simplejson.loads`. :py:func:`json.loads`.
Note that if the file is opened and read internally using Note that if the file is opened and read internally using
:py:func:`simplejson.load`, exceptions may be thrown by that subsystem, :py:func:`json.load`, exceptions may be thrown by that subsystem,
concerning the file structure. Consult the manual page for concerning the file structure. Consult the manual page for
:py:mod:`simplejson` for details. :py:mod:`simplejson` for details.
...@@ -157,7 +157,7 @@ def validate(schema_name, data): ...@@ -157,7 +157,7 @@ def validate(schema_name, data):
try: try:
data = maybe_load_json(data) data = maybe_load_json(data)
except simplejson.JSONDecodeError as e: except json.JSONDecodeError as e:
return data, ["invalid JSON code: %s" % str(e)] return data, ["invalid JSON code: %s" % str(e)]
# handles the schema version # handles the schema version
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment