|
| 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +//go:build goexperiment.jsonv2 |
| 5 | + |
| 6 | +package json |
| 7 | + |
| 8 | +import ( |
| 9 | + "bytes" |
| 10 | + jsonv1 "encoding/json" //nolint:depguard // this package wraps it |
| 11 | + jsonv2 "encoding/json/v2" //nolint:depguard // this package wraps it |
| 12 | + "io" |
| 13 | +) |
| 14 | + |
| 15 | +// JSONv2 implements Interface via encoding/json/v2 |
| 16 | +// Requires GOEXPERIMENT=jsonv2 to be set at build time |
| 17 | +type JSONv2 struct { |
| 18 | + marshalOptions jsonv2.Options |
| 19 | + marshalKeepOptionalEmptyOptions jsonv2.Options |
| 20 | + unmarshalOptions jsonv2.Options |
| 21 | + unmarshalCaseInsensitiveOptions jsonv2.Options |
| 22 | +} |
| 23 | + |
| 24 | +var jsonV2 JSONv2 |
| 25 | + |
| 26 | +func init() { |
| 27 | + commonMarshalOptions := []jsonv2.Options{ |
| 28 | + jsonv2.FormatNilSliceAsNull(true), |
| 29 | + jsonv2.FormatNilMapAsNull(true), |
| 30 | + } |
| 31 | + jsonV2.marshalOptions = jsonv2.JoinOptions(commonMarshalOptions...) |
| 32 | + jsonV2.unmarshalOptions = jsonv2.DefaultOptionsV2() |
| 33 | + |
| 34 | + // By default, "json/v2" omitempty removes all `""` empty strings, no matter where it comes from. |
| 35 | + // 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 |
| 37 | + 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)) |
| 41 | +} |
| 42 | + |
| 43 | +func getDefaultJSONHandler() Interface { |
| 44 | + return &jsonV2 |
| 45 | +} |
| 46 | + |
| 47 | +func MarshalKeepOptionalEmpty(v any) ([]byte, error) { |
| 48 | + return jsonv2.Marshal(v, jsonV2.marshalKeepOptionalEmptyOptions) |
| 49 | +} |
| 50 | + |
| 51 | +func (j *JSONv2) Marshal(v any) ([]byte, error) { |
| 52 | + return jsonv2.Marshal(v, j.marshalOptions) |
| 53 | +} |
| 54 | + |
| 55 | +func (j *JSONv2) Unmarshal(data []byte, v any) error { |
| 56 | + return jsonv2.Unmarshal(data, v, j.unmarshalOptions) |
| 57 | +} |
| 58 | + |
| 59 | +func (j *JSONv2) NewEncoder(writer io.Writer) Encoder { |
| 60 | + return &jsonV2Encoder{writer: writer, opts: j.marshalOptions} |
| 61 | +} |
| 62 | + |
| 63 | +func (j *JSONv2) NewDecoder(reader io.Reader) Decoder { |
| 64 | + return &jsonV2Decoder{reader: reader, opts: j.unmarshalOptions} |
| 65 | +} |
| 66 | + |
| 67 | +// Indent implements Interface using standard library (JSON v2 doesn't have Indent yet) |
| 68 | +func (*JSONv2) Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { |
| 69 | + return jsonv1.Indent(dst, src, prefix, indent) |
| 70 | +} |
| 71 | + |
| 72 | +type jsonV2Encoder struct { |
| 73 | + writer io.Writer |
| 74 | + opts jsonv2.Options |
| 75 | +} |
| 76 | + |
| 77 | +func (e *jsonV2Encoder) Encode(v any) error { |
| 78 | + return jsonv2.MarshalWrite(e.writer, v, e.opts) |
| 79 | +} |
| 80 | + |
| 81 | +type jsonV2Decoder struct { |
| 82 | + reader io.Reader |
| 83 | + opts jsonv2.Options |
| 84 | +} |
| 85 | + |
| 86 | +func (d *jsonV2Decoder) Decode(v any) error { |
| 87 | + return jsonv2.UnmarshalRead(d.reader, v, d.opts) |
| 88 | +} |
| 89 | + |
| 90 | +func NewDecoderCaseInsensitive(reader io.Reader) Decoder { |
| 91 | + return &jsonV2Decoder{reader: reader, opts: jsonV2.unmarshalCaseInsensitiveOptions} |
| 92 | +} |
0 commit comments