diff options
author | Sergey Kosukhin <skosukhin@gmail.com> | 2016-12-08 22:42:18 +0100 |
---|---|---|
committer | Todd Gamblin <tgamblin@llnl.gov> | 2016-12-08 13:42:18 -0800 |
commit | 10591fb87fe3889e85c7902a22bebe56d8138913 (patch) | |
tree | 0d5d56bd918a5ad39920bdec096f9b373f807104 | |
parent | 607f4e7b811b648c9c49f5155b7f32e1a8ee54c7 (diff) | |
download | spack-10591fb87fe3889e85c7902a22bebe56d8138913.tar.gz spack-10591fb87fe3889e85c7902a22bebe56d8138913.tar.bz2 spack-10591fb87fe3889e85c7902a22bebe56d8138913.tar.xz spack-10591fb87fe3889e85c7902a22bebe56d8138913.zip |
Json loader now returns str objects instead of unicode. (#2524)
-rw-r--r-- | lib/spack/spack/util/spack_json.py | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/lib/spack/spack/util/spack_json.py b/lib/spack/spack/util/spack_json.py index 240ce86c68..236eef8983 100644 --- a/lib/spack/spack/util/spack_json.py +++ b/lib/spack/spack/util/spack_json.py @@ -37,9 +37,11 @@ _json_dump_args = { def load(stream): """Spack JSON needs to be ordered to support specs.""" if isinstance(stream, basestring): - return json.loads(stream) + return _byteify(json.loads(stream, object_hook=_byteify), + ignore_dicts=True) else: - return json.load(stream) + return _byteify(json.load(stream, object_hook=_byteify), + ignore_dicts=True) def dump(data, stream=None): @@ -50,7 +52,25 @@ def dump(data, stream=None): return json.dump(data, stream, **_json_dump_args) +def _byteify(data, ignore_dicts=False): + # if this is a unicode string, return its string representation + if isinstance(data, unicode): + return data.encode('utf-8') + # if this is a list of values, return list of byteified values + if isinstance(data, list): + return [_byteify(item, ignore_dicts=True) for item in data] + # if this is a dictionary, return dictionary of byteified keys and values + # but only if we haven't already byteified it + if isinstance(data, dict) and not ignore_dicts: + return dict((_byteify(key, ignore_dicts=True), + _byteify(value, ignore_dicts=True)) for key, value in + data.iteritems()) + # if it's anything else, return it in its original form + return data + + class SpackJSONError(spack.error.SpackError): """Raised when there are issues with JSON parsing.""" + def __init__(self, msg, yaml_error): super(SpackJSONError, self).__init__(msg, str(yaml_error)) |