diff --git a/google/cloud/spanner_v1/data_types.py b/google/cloud/spanner_v1/data_types.py index 130603afa9..63897b293c 100644 --- a/google/cloud/spanner_v1/data_types.py +++ b/google/cloud/spanner_v1/data_types.py @@ -38,6 +38,11 @@ def __init__(self, *args, **kwargs): self._array_value = args[0] return + if len(args) and isinstance(args[0], JsonObject): + self._is_array = args[0]._is_array + if self._is_array: + self._array_value = args[0]._array_value + if not self._is_null: super(JsonObject, self).__init__(*args, **kwargs) diff --git a/tests/unit/test_datatypes.py b/tests/unit/test_datatypes.py new file mode 100644 index 0000000000..60630f73d3 --- /dev/null +++ b/tests/unit/test_datatypes.py @@ -0,0 +1,45 @@ +# Copyright 2024 Google LLC All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import unittest + +import json +from google.cloud.spanner_v1.data_types import JsonObject + + +class Test_JsonObject_serde(unittest.TestCase): + def test_w_dict(self): + data = {"foo": "bar"} + expected = json.dumps(data, sort_keys=True, separators=(",", ":")) + data_jsonobject = JsonObject(data) + self.assertEqual(data_jsonobject.serialize(), expected) + + def test_w_list_of_dict(self): + data = [{"foo1": "bar1"}, {"foo2": "bar2"}] + expected = json.dumps(data, sort_keys=True, separators=(",", ":")) + data_jsonobject = JsonObject(data) + self.assertEqual(data_jsonobject.serialize(), expected) + + def test_w_JsonObject_of_dict(self): + data = {"foo": "bar"} + expected = json.dumps(data, sort_keys=True, separators=(",", ":")) + data_jsonobject = JsonObject(JsonObject(data)) + self.assertEqual(data_jsonobject.serialize(), expected) + + def test_w_JsonObject_of_list_of_dict(self): + data = [{"foo1": "bar1"}, {"foo2": "bar2"}] + expected = json.dumps(data, sort_keys=True, separators=(",", ":")) + data_jsonobject = JsonObject(JsonObject(data)) + self.assertEqual(data_jsonobject.serialize(), expected)