Skip to content

Commit c4af6f0

Browse files
fix: JsonObject init when called on JsonObject of list (googleapis#1166)
Co-authored-by: Sri Harsha CH <[email protected]>
1 parent f886ebd commit c4af6f0

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

google/cloud/spanner_v1/data_types.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ def __init__(self, *args, **kwargs):
3838
self._array_value = args[0]
3939
return
4040

41+
if len(args) and isinstance(args[0], JsonObject):
42+
self._is_array = args[0]._is_array
43+
if self._is_array:
44+
self._array_value = args[0]._array_value
45+
4146
if not self._is_null:
4247
super(JsonObject, self).__init__(*args, **kwargs)
4348

tests/unit/test_datatypes.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2024 Google LLC All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import unittest
17+
18+
import json
19+
from google.cloud.spanner_v1.data_types import JsonObject
20+
21+
22+
class Test_JsonObject_serde(unittest.TestCase):
23+
def test_w_dict(self):
24+
data = {"foo": "bar"}
25+
expected = json.dumps(data, sort_keys=True, separators=(",", ":"))
26+
data_jsonobject = JsonObject(data)
27+
self.assertEqual(data_jsonobject.serialize(), expected)
28+
29+
def test_w_list_of_dict(self):
30+
data = [{"foo1": "bar1"}, {"foo2": "bar2"}]
31+
expected = json.dumps(data, sort_keys=True, separators=(",", ":"))
32+
data_jsonobject = JsonObject(data)
33+
self.assertEqual(data_jsonobject.serialize(), expected)
34+
35+
def test_w_JsonObject_of_dict(self):
36+
data = {"foo": "bar"}
37+
expected = json.dumps(data, sort_keys=True, separators=(",", ":"))
38+
data_jsonobject = JsonObject(JsonObject(data))
39+
self.assertEqual(data_jsonobject.serialize(), expected)
40+
41+
def test_w_JsonObject_of_list_of_dict(self):
42+
data = [{"foo1": "bar1"}, {"foo2": "bar2"}]
43+
expected = json.dumps(data, sort_keys=True, separators=(",", ":"))
44+
data_jsonobject = JsonObject(JsonObject(data))
45+
self.assertEqual(data_jsonobject.serialize(), expected)

0 commit comments

Comments
 (0)