@@ -16,8 +16,9 @@ import (
16
16
// Requires GOEXPERIMENT=jsonv2 to be set at build time
17
17
type JSONv2 struct {
18
18
marshalOptions jsonv2.Options
19
- marshalKeepOptionalEmptyOptions jsonv2.Options
19
+ marshalOptionalEmptyOptions jsonv2.Options
20
20
unmarshalOptions jsonv2.Options
21
+ unmarshalCaseInsensitiveOptions jsonv2.Options
21
22
}
22
23
23
24
var jsonV2 JSONv2
@@ -28,58 +29,63 @@ func init() {
28
29
jsonv2 .FormatNilMapAsNull (true ),
29
30
}
30
31
jsonV2 .marshalOptions = jsonv2 .JoinOptions (commonMarshalOptions ... )
32
+ jsonV2 .unmarshalOptions = jsonv2 .DefaultOptionsV2 ()
31
33
32
- // Some JSON structs like oci.ImageConfig uses case-insensitive matching
33
- jsonV2 .unmarshalOptions = jsonv2 .JoinOptions (jsonv2 .MatchCaseInsensitiveNames (true ))
34
-
35
- // by default, "json/v2" omitempty removes all `""` empty strings, no matter where it comes from.
34
+ // By default, "json/v2" omitempty removes all `""` empty strings, no matter where it comes from.
36
35
// v1 has a different behavior: if the `""` is from a null pointer, or a Marshal function, it is kept.
37
- jsonV2 .marshalKeepOptionalEmptyOptions = jsonv2 .JoinOptions (append (commonMarshalOptions , jsonv1 .OmitEmptyWithLegacySemantics (true ))... )
36
+ jsonV2 .marshalOptionalEmptyOptions = jsonv2 .JoinOptions (append (commonMarshalOptions , jsonv1 .OmitEmptyWithLegacySemantics (true ))... )
37
+
38
+ // Some legacy code uses case-insensitive matching (for example: parsing oci.ImageConfig)
39
+ jsonV2 .unmarshalCaseInsensitiveOptions = jsonv2 .JoinOptions (jsonv2 .MatchCaseInsensitiveNames (true ))
38
40
}
39
41
40
42
func getDefaultJSONHandler () Interface {
41
43
return & jsonV2
42
44
}
43
45
44
- func MarshalKeepOptionalEmpty (v any ) ([]byte , error ) {
45
- return jsonv2 .Marshal (v , jsonV2 .marshalKeepOptionalEmptyOptions )
46
- }
47
-
48
- func (j JSONv2 ) Marshal (v any ) ([]byte , error ) {
46
+ func (j * JSONv2 ) Marshal (v any ) ([]byte , error ) {
49
47
return jsonv2 .Marshal (v , j .marshalOptions )
50
48
}
51
49
52
- func (j JSONv2 ) Unmarshal (data []byte , v any ) error {
50
+ func (j * JSONv2 ) Unmarshal (data []byte , v any ) error {
53
51
return jsonv2 .Unmarshal (data , v , j .unmarshalOptions )
54
52
}
55
53
56
- func (j JSONv2 ) NewEncoder (writer io.Writer ) Encoder {
57
- return & encoderV2 {writer : writer , opts : j .marshalOptions }
54
+ func (j * JSONv2 ) NewEncoder (writer io.Writer ) Encoder {
55
+ return & jsonV2Encoder {writer : writer , opts : j .marshalOptions }
58
56
}
59
57
60
- func (j JSONv2 ) NewDecoder (reader io.Reader ) Decoder {
61
- return & decoderV2 {reader : reader , opts : j .unmarshalOptions }
58
+ func (j * JSONv2 ) NewDecoder (reader io.Reader ) Decoder {
59
+ return & jsonV2Decoder {reader : reader , opts : j .unmarshalOptions }
62
60
}
63
61
64
62
// Indent implements Interface using standard library (JSON v2 doesn't have Indent yet)
65
- func (JSONv2 ) Indent (dst * bytes.Buffer , src []byte , prefix , indent string ) error {
63
+ func (* JSONv2 ) Indent (dst * bytes.Buffer , src []byte , prefix , indent string ) error {
66
64
return jsonv1 .Indent (dst , src , prefix , indent )
67
65
}
68
66
69
- type encoderV2 struct {
67
+ type jsonV2Encoder struct {
70
68
writer io.Writer
71
69
opts jsonv2.Options
72
70
}
73
71
74
- func (e * encoderV2 ) Encode (v any ) error {
72
+ func (e * jsonV2Encoder ) Encode (v any ) error {
75
73
return jsonv2 .MarshalWrite (e .writer , v , e .opts )
76
74
}
77
75
78
- type decoderV2 struct {
76
+ type jsonV2Decoder struct {
79
77
reader io.Reader
80
78
opts jsonv2.Options
81
79
}
82
80
83
- func (d * decoderV2 ) Decode (v any ) error {
81
+ func (d * jsonV2Decoder ) Decode (v any ) error {
84
82
return jsonv2 .UnmarshalRead (d .reader , v , d .opts )
85
83
}
84
+
85
+ func NewEncoderOptionalEmpty (writer io.Writer ) Encoder {
86
+ return & jsonV2Encoder {writer : writer , opts : jsonV2 .marshalOptionalEmptyOptions }
87
+ }
88
+
89
+ func NewDecoderCaseInsensitive (reader io.Reader ) Decoder {
90
+ return & jsonV2Decoder {reader : reader , opts : jsonV2 .unmarshalCaseInsensitiveOptions }
91
+ }
0 commit comments