@@ -21,6 +21,7 @@ import (
21
21
"database/sql"
22
22
"database/sql/driver"
23
23
"encoding/base64"
24
+ "encoding/json"
24
25
"fmt"
25
26
"math"
26
27
"math/big"
@@ -32,7 +33,6 @@ import (
32
33
"cloud.google.com/go/civil"
33
34
"cloud.google.com/go/internal/fields"
34
35
sppb "cloud.google.com/go/spanner/apiv1/spannerpb"
35
- jsoniter "github.com/json-iterator/go"
36
36
"google.golang.org/grpc/codes"
37
37
"google.golang.org/protobuf/proto"
38
38
proto3 "google.golang.org/protobuf/types/known/structpb"
@@ -114,7 +114,7 @@ var (
114
114
115
115
jsonNullBytes = []byte ("null" )
116
116
117
- jsonProvider = jsoniter . ConfigCompatibleWithStandardLibrary
117
+ jsonUseNumber bool
118
118
)
119
119
120
120
// UseNumberWithJSONDecoderEncoder specifies whether Cloud Spanner JSON numbers are decoded
@@ -124,11 +124,15 @@ var (
124
124
// NOTE 1: Calling this method affects the behavior of all clients created by this library, both existing and future instances.
125
125
// NOTE 2: This method sets a global variable that is used by the client to encode/decode JSON numbers. Access to the global variable is not synchronized. You should only call this method when there are no goroutines encoding/decoding Cloud Spanner JSON values. It is recommended to only call this method during the initialization of your application, and preferably before you create any Cloud Spanner clients, and/or in tests when there are no queries being executed.
126
126
func UseNumberWithJSONDecoderEncoder (useNumber bool ) {
127
- jsonProvider = jsoniter.Config {
128
- EscapeHTML : true ,
129
- SortMapKeys : true , // Sort map keys to ensure deterministic output, to be consistent with encoding.
130
- UseNumber : useNumber ,
131
- }.Froze ()
127
+ jsonUseNumber = useNumber
128
+ }
129
+
130
+ func jsonUnmarshal (data []byte , v any ) error {
131
+ dec := json .NewDecoder (bytes .NewReader (data ))
132
+ if jsonUseNumber {
133
+ dec .UseNumber ()
134
+ }
135
+ return dec .Decode (v )
132
136
}
133
137
134
138
// Encoder is the interface implemented by a custom type that can be encoded to
@@ -297,7 +301,7 @@ func (n *NullString) UnmarshalJSON(payload []byte) error {
297
301
return nil
298
302
}
299
303
var s * string
300
- if err := jsonProvider . Unmarshal (payload , & s ); err != nil {
304
+ if err := jsonUnmarshal (payload , & s ); err != nil {
301
305
return err
302
306
}
303
307
if s != nil {
@@ -866,7 +870,7 @@ func (n NullJSON) String() string {
866
870
if ! n .Valid {
867
871
return nullString
868
872
}
869
- b , err := jsonProvider .Marshal (n .Value )
873
+ b , err := json .Marshal (n .Value )
870
874
if err != nil {
871
875
return fmt .Sprintf ("error: %v" , err )
872
876
}
@@ -888,7 +892,7 @@ func (n *NullJSON) UnmarshalJSON(payload []byte) error {
888
892
return nil
889
893
}
890
894
var v interface {}
891
- err := jsonProvider . Unmarshal (payload , & v )
895
+ err := jsonUnmarshal (payload , & v )
892
896
if err != nil {
893
897
return fmt .Errorf ("payload cannot be converted to a struct: got %v, err: %w" , string (payload ), err )
894
898
}
@@ -972,7 +976,7 @@ func (n PGJsonB) String() string {
972
976
if ! n .Valid {
973
977
return nullString
974
978
}
975
- b , err := jsonProvider .Marshal (n .Value )
979
+ b , err := json .Marshal (n .Value )
976
980
if err != nil {
977
981
return fmt .Sprintf ("error: %v" , err )
978
982
}
@@ -994,7 +998,7 @@ func (n *PGJsonB) UnmarshalJSON(payload []byte) error {
994
998
return nil
995
999
}
996
1000
var v interface {}
997
- err := jsonProvider . Unmarshal (payload , & v )
1001
+ err := jsonUnmarshal (payload , & v )
998
1002
if err != nil {
999
1003
return fmt .Errorf ("payload cannot be converted to a struct: got %v, err: %w" , string (payload ), err )
1000
1004
}
@@ -1007,7 +1011,7 @@ func nulljson(valid bool, v interface{}) ([]byte, error) {
1007
1011
if ! valid {
1008
1012
return jsonNullBytes , nil
1009
1013
}
1010
- return jsonProvider .Marshal (v )
1014
+ return json .Marshal (v )
1011
1015
}
1012
1016
1013
1017
// GenericColumnValue represents the generic encoded value and type of the
@@ -1714,7 +1718,7 @@ func decodeValue(v *proto3.Value, t *sppb.Type, ptr interface{}, opts ...DecodeO
1714
1718
}
1715
1719
x := v .GetStringValue ()
1716
1720
var y interface {}
1717
- err := jsonProvider . Unmarshal ([]byte (x ), & y )
1721
+ err := jsonUnmarshal ([]byte (x ), & y )
1718
1722
if err != nil {
1719
1723
return err
1720
1724
}
@@ -1873,7 +1877,7 @@ func decodeValue(v *proto3.Value, t *sppb.Type, ptr interface{}, opts ...DecodeO
1873
1877
}
1874
1878
x := v .GetStringValue ()
1875
1879
var y interface {}
1876
- err := jsonProvider . Unmarshal ([]byte (x ), & y )
1880
+ err := jsonUnmarshal ([]byte (x ), & y )
1877
1881
if err != nil {
1878
1882
return err
1879
1883
}
@@ -2566,7 +2570,7 @@ func (dsc decodableSpannerType) decodeValueToCustomType(v *proto3.Value, t *sppb
2566
2570
}
2567
2571
x := v .GetStringValue ()
2568
2572
var y interface {}
2569
- err := jsonProvider . Unmarshal ([]byte (x ), & y )
2573
+ err := jsonUnmarshal ([]byte (x ), & y )
2570
2574
if err != nil {
2571
2575
return err
2572
2576
}
@@ -2581,7 +2585,7 @@ func (dsc decodableSpannerType) decodeValueToCustomType(v *proto3.Value, t *sppb
2581
2585
}
2582
2586
x := v .GetStringValue ()
2583
2587
var y interface {}
2584
- err := jsonProvider . Unmarshal ([]byte (x ), & y )
2588
+ err := jsonUnmarshal ([]byte (x ), & y )
2585
2589
if err != nil {
2586
2590
return err
2587
2591
}
@@ -3272,7 +3276,7 @@ func decodeNullJSONArrayToNullJSON(pb *proto3.ListValue) (*NullJSON, error) {
3272
3276
}
3273
3277
s := fmt .Sprintf ("[%s]" , strings .Join (strs , "," ))
3274
3278
var y interface {}
3275
- err := jsonProvider . Unmarshal ([]byte (s ), & y )
3279
+ err := jsonUnmarshal ([]byte (s ), & y )
3276
3280
if err != nil {
3277
3281
return nil , err
3278
3282
}
@@ -3929,7 +3933,7 @@ func encodeValue(v interface{}) (*proto3.Value, *sppb.Type, error) {
3929
3933
pt = listType (pgNumericType ())
3930
3934
case NullJSON :
3931
3935
if v .Valid {
3932
- b , err := jsonProvider .Marshal (v .Value )
3936
+ b , err := json .Marshal (v .Value )
3933
3937
if err != nil {
3934
3938
return nil , nil , err
3935
3939
}
@@ -3946,7 +3950,7 @@ func encodeValue(v interface{}) (*proto3.Value, *sppb.Type, error) {
3946
3950
pt = listType (jsonType ())
3947
3951
case PGJsonB :
3948
3952
if v .Valid {
3949
- b , err := jsonProvider .Marshal (v .Value )
3953
+ b , err := json .Marshal (v .Value )
3950
3954
if err != nil {
3951
3955
return nil , nil , err
3952
3956
}
0 commit comments