Skip to content

fix: Compact JSON serialization for the dbjson dtype #299

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 1 commit into from
Nov 8, 2024
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
4 changes: 3 additions & 1 deletion db_dtypes/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ def _serialize_json(value):
else:
# `sort_keys=True` sorts dictionary keys before serialization, making
# JSON comparisons deterministic.
return json.dumps(value, sort_keys=True)
# `separators=(',', ':')` eliminate whitespace to get the most compact
# JSON representation.
return json.dumps(value, sort_keys=True, separators=(",", ":"))

@staticmethod
def _deserialize_json(value):
Expand Down
12 changes: 8 additions & 4 deletions tests/compliance/json/test_json_compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def test_astype_str(self, data):
# Use `json.dumps(str)` instead of passing `str(obj)` directly to the super method.
result = pd.Series(data[:5]).astype(str)
expected = pd.Series(
[json.dumps(x, sort_keys=True) for x in data[:5]], dtype=str
[json.dumps(x, sort_keys=True, separators=(",", ":")) for x in data[:5]],
dtype=str,
)
tm.assert_series_equal(result, expected)

Expand All @@ -46,7 +47,7 @@ def test_astype_string(self, data, nullable_string_dtype):
# Use `json.dumps(str)` instead of passing `str(obj)` directly to the super method.
result = pd.Series(data[:5]).astype(nullable_string_dtype)
expected = pd.Series(
[json.dumps(x, sort_keys=True) for x in data[:5]],
[json.dumps(x, sort_keys=True, separators=(",", ":")) for x in data[:5]],
dtype=nullable_string_dtype,
)
tm.assert_series_equal(result, expected)
Expand Down Expand Up @@ -119,11 +120,14 @@ class TestJSONArrayInterface(base.BaseInterfaceTests):
def test_array_interface(self, data):
result = np.array(data)
# Use `json.dumps(data[0])` instead of passing `data[0]` directly to the super method.
assert result[0] == json.dumps(data[0])
assert result[0] == json.dumps(data[0], sort_keys=True, separators=(",", ":"))

result = np.array(data, dtype=object)
# Use `json.dumps(x)` instead of passing `x` directly to the super method.
expected = np.array([json.dumps(x) for x in data], dtype=object)
expected = np.array(
[json.dumps(x, sort_keys=True, separators=(",", ":")) for x in data],
dtype=object,
)
# if expected.ndim > 1:
# # nested data, explicitly construct as 1D
# expected = construct_1d_object_array_from_listlike(list(data))
Expand Down