Commit 1df8de0d authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[dataformat] Use error_on_duplicate_key_hook when loading data

parent 26083434
......@@ -47,7 +47,7 @@ import copy
import six
import numpy
import simplejson
import simplejson as json
from . import utils
from .baseformat import baseformat
......@@ -186,7 +186,16 @@ class DataFormat(object):
return
with open(json_path, "rb") as f:
self.data = simplejson.loads(f.read().decode("utf-8"))
try:
self.data = json.loads(
f.read().decode("utf-8"),
object_pairs_hook=utils.error_on_duplicate_key_hook,
)
except RuntimeError as error:
self.errors.append(
"Dataformat declaration file invalid: %s" % error
)
return
dataformat_cache[self._name] = self # registers itself into the cache
......@@ -457,7 +466,7 @@ class DataFormat(object):
"""
return simplejson.dumps(self.data, indent=indent, cls=utils.NumpyJSONEncoder)
return json.dumps(self.data, indent=indent, cls=utils.NumpyJSONEncoder)
def __str__(self):
return self.json_dumps()
......
......@@ -346,3 +346,12 @@ def test_string():
copy.isclose(obj),
"%r is not close enough to %r" % (copy.as_dict(), obj.as_dict()),
)
# ----------------------------------------------------------
def test_duplicate_key_error():
df = DataFormat(prefix, "errors/duplicate_key/1")
nose.tools.assert_false(df.valid)
nose.tools.assert_true("Dataformat declaration file invalid" in df.errors[0])
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment