@@ -18,6 +18,7 @@ type JSONv2 struct {
1818 marshalOptions jsonv2.Options
1919 marshalKeepOptionalEmptyOptions jsonv2.Options
2020 unmarshalOptions jsonv2.Options
21+ unmarshalCaseInsensitiveOptions jsonv2.Options
2122}
2223
2324var jsonV2 JSONv2
@@ -28,13 +29,15 @@ 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.
36+ // Golang issue: https://github.com/golang/go/issues/75623 encoding/json/v2: unable to make omitempty work with pointer or Optional type with goexperiment.jsonv2
3737 jsonV2 .marshalKeepOptionalEmptyOptions = jsonv2 .JoinOptions (append (commonMarshalOptions , jsonv1 .OmitEmptyWithLegacySemantics (true ))... )
38+
39+ // Some legacy code uses case-insensitive matching (for example: parsing oci.ImageConfig)
40+ jsonV2 .unmarshalCaseInsensitiveOptions = jsonv2 .JoinOptions (jsonv2 .MatchCaseInsensitiveNames (true ))
3841}
3942
4043func getDefaultJSONHandler () Interface {
@@ -45,41 +48,45 @@ func MarshalKeepOptionalEmpty(v any) ([]byte, error) {
4548 return jsonv2 .Marshal (v , jsonV2 .marshalKeepOptionalEmptyOptions )
4649}
4750
48- func (j JSONv2 ) Marshal (v any ) ([]byte , error ) {
51+ func (j * JSONv2 ) Marshal (v any ) ([]byte , error ) {
4952 return jsonv2 .Marshal (v , j .marshalOptions )
5053}
5154
52- func (j JSONv2 ) Unmarshal (data []byte , v any ) error {
55+ func (j * JSONv2 ) Unmarshal (data []byte , v any ) error {
5356 return jsonv2 .Unmarshal (data , v , j .unmarshalOptions )
5457}
5558
56- func (j JSONv2 ) NewEncoder (writer io.Writer ) Encoder {
57- return & encoderV2 {writer : writer , opts : j .marshalOptions }
59+ func (j * JSONv2 ) NewEncoder (writer io.Writer ) Encoder {
60+ return & jsonV2Encoder {writer : writer , opts : j .marshalOptions }
5861}
5962
60- func (j JSONv2 ) NewDecoder (reader io.Reader ) Decoder {
61- return & decoderV2 {reader : reader , opts : j .unmarshalOptions }
63+ func (j * JSONv2 ) NewDecoder (reader io.Reader ) Decoder {
64+ return & jsonV2Decoder {reader : reader , opts : j .unmarshalOptions }
6265}
6366
6467// 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 {
68+ func (* JSONv2 ) Indent (dst * bytes.Buffer , src []byte , prefix , indent string ) error {
6669 return jsonv1 .Indent (dst , src , prefix , indent )
6770}
6871
69- type encoderV2 struct {
72+ type jsonV2Encoder struct {
7073 writer io.Writer
7174 opts jsonv2.Options
7275}
7376
74- func (e * encoderV2 ) Encode (v any ) error {
77+ func (e * jsonV2Encoder ) Encode (v any ) error {
7578 return jsonv2 .MarshalWrite (e .writer , v , e .opts )
7679}
7780
78- type decoderV2 struct {
81+ type jsonV2Decoder struct {
7982 reader io.Reader
8083 opts jsonv2.Options
8184}
8285
83- func (d * decoderV2 ) Decode (v any ) error {
86+ func (d * jsonV2Decoder ) Decode (v any ) error {
8487 return jsonv2 .UnmarshalRead (d .reader , v , d .opts )
8588}
89+
90+ func NewDecoderCaseInsensitive (reader io.Reader ) Decoder {
91+ return & jsonV2Decoder {reader : reader , opts : jsonV2 .unmarshalCaseInsensitiveOptions }
92+ }
0 commit comments