@@ -16,8 +16,9 @@ import (
1616// Requires GOEXPERIMENT=jsonv2 to be set at build time
1717type JSONv2 struct {
1818 marshalOptions jsonv2.Options
19- marshalKeepOptionalEmptyOptions jsonv2.Options
19+ marshalOptionalEmptyOptions jsonv2.Options
2020 unmarshalOptions jsonv2.Options
21+ unmarshalCaseInsensitiveOptions jsonv2.Options
2122}
2223
2324var jsonV2 JSONv2
@@ -28,58 +29,63 @@ func init() {
2829 jsonv2 .FormatNilMapAsNull (true ),
2930 }
3031 jsonV2 .marshalOptions = jsonv2 .JoinOptions (commonMarshalOptions ... )
32+ jsonV2 .unmarshalOptions = jsonv2 .DefaultOptionsV2 ()
3133
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.
3635 // 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 ))
3840}
3941
4042func getDefaultJSONHandler () Interface {
4143 return & jsonV2
4244}
4345
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 ) {
4947 return jsonv2 .Marshal (v , j .marshalOptions )
5048}
5149
52- func (j JSONv2 ) Unmarshal (data []byte , v any ) error {
50+ func (j * JSONv2 ) Unmarshal (data []byte , v any ) error {
5351 return jsonv2 .Unmarshal (data , v , j .unmarshalOptions )
5452}
5553
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 }
5856}
5957
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 }
6260}
6361
6462// 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 {
6664 return jsonv1 .Indent (dst , src , prefix , indent )
6765}
6866
69- type encoderV2 struct {
67+ type jsonV2Encoder struct {
7068 writer io.Writer
7169 opts jsonv2.Options
7270}
7371
74- func (e * encoderV2 ) Encode (v any ) error {
72+ func (e * jsonV2Encoder ) Encode (v any ) error {
7573 return jsonv2 .MarshalWrite (e .writer , v , e .opts )
7674}
7775
78- type decoderV2 struct {
76+ type jsonV2Decoder struct {
7977 reader io.Reader
8078 opts jsonv2.Options
8179}
8280
83- func (d * decoderV2 ) Decode (v any ) error {
81+ func (d * jsonV2Decoder ) Decode (v any ) error {
8482 return jsonv2 .UnmarshalRead (d .reader , v , d .opts )
8583}
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