diff --git a/docs/release.rst b/docs/release.rst index 39defca9..1bf12b74 100644 --- a/docs/release.rst +++ b/docs/release.rst @@ -15,6 +15,9 @@ Release notes * Use (new) buffer protocol in ``MsgPack`` codec `decode()` method. By :user:`John Kirkham `, :issue:`148`. +* Use (new) buffer protocol in ``JSON`` codec `decode()` method. + By :user:`John Kirkham `, :issue:`151`. + * Avoid copying into data in ``GZip``'s `decode()` method on Python 2. By :user:`John Kirkham `, :issue:`152`. diff --git a/numcodecs/json.py b/numcodecs/json.py index b502cb26..e00add91 100644 --- a/numcodecs/json.py +++ b/numcodecs/json.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import, print_function, division +import codecs import json as _json import textwrap @@ -8,7 +9,7 @@ from .abc import Codec -from .compat import ensure_bytes +from .compat import ensure_contiguous_ndarray class JSON(Codec): @@ -63,8 +64,8 @@ def encode(self, buf): return self._encoder.encode(items).encode(self._text_encoding) def decode(self, buf, out=None): - buf = ensure_bytes(buf) - items = self._decoder.decode(buf.decode(self._text_encoding)) + buf = ensure_contiguous_ndarray(buf) + items = self._decoder.decode(codecs.decode(buf, self._text_encoding)) dec = np.empty(items[-1], dtype=items[-2]) dec[:] = items[:-2] if out is not None: @@ -125,8 +126,8 @@ def encode(self, buf): return self._encoder.encode(items).encode(self._text_encoding) def decode(self, buf, out=None): - buf = ensure_bytes(buf) - items = self._decoder.decode(buf.decode(self._text_encoding)) + buf = ensure_contiguous_ndarray(buf) + items = self._decoder.decode(codecs.decode(buf, self._text_encoding)) dec = np.array(items[:-1], dtype=items[-1]) if out is not None: np.copyto(out, dec)