File tree 3 files changed +29
-3
lines changed
3 files changed +29
-3
lines changed Original file line number Diff line number Diff line change 17
17
import base64
18
18
import datetime
19
19
import decimal
20
+ import json
20
21
import math
21
22
import re
22
23
import os
@@ -412,6 +413,12 @@ def _time_to_json(value):
412
413
return value
413
414
414
415
416
+ def _json_from_json (value , field ):
417
+ """Coerce 'value' to a pythonic JSON representation, if set or not nullable."""
418
+ if _not_null (value , field ):
419
+ return json .loads (value )
420
+
421
+
415
422
# Converters used for scalar values marshalled as row data.
416
423
_SCALAR_VALUE_TO_JSON_ROW = {
417
424
"INTEGER" : _int_to_json ,
@@ -427,6 +434,7 @@ def _time_to_json(value):
427
434
"DATETIME" : _datetime_to_json ,
428
435
"DATE" : _date_to_json ,
429
436
"TIME" : _time_to_json ,
437
+ "JSON" : _json_from_json ,
430
438
# Make sure DECIMAL and BIGDECIMAL are handled, even though
431
439
# requests for them should be converted to NUMERIC. Better safe
432
440
# than sorry.
Original file line number Diff line number Diff line change @@ -469,7 +469,7 @@ def to_api_repr(self) -> dict:
469
469
value = self .value
470
470
converter = _SCALAR_VALUE_TO_JSON_PARAM .get (self .type_ )
471
471
if converter is not None :
472
- value = converter (value )
472
+ value = converter (value ) # type: ignore
473
473
resource : Dict [str , Any ] = {
474
474
"parameterType" : {"type" : self .type_ },
475
475
"parameterValue" : {"value" : value },
@@ -626,7 +626,7 @@ def to_api_repr(self) -> dict:
626
626
627
627
converter = _SCALAR_VALUE_TO_JSON_PARAM .get (a_type ["type" ])
628
628
if converter is not None :
629
- values = [converter (value ) for value in values ]
629
+ values = [converter (value ) for value in values ] # type: ignore
630
630
a_values = [{"value" : value } for value in values ]
631
631
632
632
resource = {
@@ -775,7 +775,7 @@ def to_api_repr(self) -> dict:
775
775
s_types [name ] = {"name" : name , "type" : {"type" : type_ }}
776
776
converter = _SCALAR_VALUE_TO_JSON_PARAM .get (type_ )
777
777
if converter is not None :
778
- value = converter (value )
778
+ value = converter (value ) # type: ignore
779
779
values [name ] = {"value" : value }
780
780
781
781
resource = {
Original file line number Diff line number Diff line change @@ -58,6 +58,24 @@ def test_w_float_value(self):
58
58
self .assertEqual (coerced , 42 )
59
59
60
60
61
+ class Test_json_from_json (unittest .TestCase ):
62
+ def _call_fut (self , value , field ):
63
+ from google .cloud .bigquery ._helpers import _json_from_json
64
+
65
+ return _json_from_json (value , field )
66
+
67
+ def test_w_none_nullable (self ):
68
+ self .assertIsNone (self ._call_fut (None , _Field ("NULLABLE" )))
69
+
70
+ def test_w_none_required (self ):
71
+ with self .assertRaises (TypeError ):
72
+ self ._call_fut (None , _Field ("REQUIRED" ))
73
+
74
+ def test_w_string_value (self ):
75
+ coerced = self ._call_fut ('{"foo": true}' , object ())
76
+ self .assertEqual (coerced , {"foo" : True })
77
+
78
+
61
79
class Test_float_from_json (unittest .TestCase ):
62
80
def _call_fut (self , value , field ):
63
81
from google .cloud .bigquery ._helpers import _float_from_json
You can’t perform that action at this time.
0 commit comments