Skip to content

feature: Add JSON lines deserializer #1767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/sagemaker/deserializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,28 @@ def deserialize(self, stream, content_type):
return pandas.read_json(stream)

raise ValueError("%s cannot read content type %s." % (__class__.__name__, content_type))


class JSONLinesDeserializer(BaseDeserializer):
"""Deserialize JSON lines data from an inference endpoint."""

ACCEPT = "application/jsonlines"

def deserialize(self, stream, content_type):
"""Deserialize JSON lines data from an inference endpoint.

See https://docs.python.org/3/library/json.html#py-to-json-table to
understand how JSON values are converted to Python objects.

Args:
stream (botocore.response.StreamingBody): Data to be deserialized.
content_type (str): The MIME type of the data.

Returns:
list: A list of JSON serializable objects.
"""
try:
lines = stream.read().rstrip().split("\n")
return [json.loads(line) for line in lines]
finally:
stream.close()
24 changes: 24 additions & 0 deletions tests/unit/sagemaker/test_deserializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
NumpyDeserializer,
JSONDeserializer,
PandasDeserializer,
JSONLinesDeserializer,
)


Expand Down Expand Up @@ -208,3 +209,26 @@ def test_pandas_deserializer_csv(pandas_deserializer):
result = pandas_deserializer.deserialize(stream, "text/csv")
expected = pd.DataFrame([["a", "b"], ["c", "d"]], columns=["col 1", "col 2"])
assert result.equals(expected)


@pytest.fixture
def json_lines_deserializer():
return JSONLinesDeserializer()


@pytest.mark.parametrize(
"source, expected",
[
('["Name", "Score"]\n["Gilbert", 24]', [["Name", "Score"], ["Gilbert", 24]]),
('["Name", "Score"]\n["Gilbert", 24]\n', [["Name", "Score"], ["Gilbert", 24]]),
(
'{"Name": "Gilbert", "Score": 24}\n{"Name": "Alexa", "Score": 29}',
[{"Name": "Gilbert", "Score": 24}, {"Name": "Alexa", "Score": 29}],
),
],
)
def test_json_lines_deserializer(json_lines_deserializer, source, expected):
stream = io.StringIO(source)
content_type = "application/jsonlines"
actual = json_lines_deserializer.deserialize(stream, content_type)
assert actual == expected