-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
use experimental go json v2 library #35392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
c5f251b
use experimental go json v2 library
cb16b33
Merge branch 'main' into jsonv2
techknowlogick abc734c
Update go.mod
techknowlogick f9c8805
Update jsonv2.go
techknowlogick 9d83440
Update jsonv2.go
techknowlogick 31492c1
Update jsonv2.go
techknowlogick 7b0832c
Update jsonv2.go
techknowlogick fe51938
Update jsonv2_fallback.go
techknowlogick 29d1770
Update jsonv2_fallback.go
techknowlogick 3c3f77f
Update jsonv2_fallback.go
techknowlogick ce4107d
Merge branch 'main' into jsonv2
techknowlogick b8fb94d
Update jsonv2_fallback.go
techknowlogick c748ca8
Update jsonv2_fallback.go
techknowlogick f9d91f2
Merge branch 'main' into jsonv2
techknowlogick 7fec838
Update Go version from 1.24.6 to 1.25.0
techknowlogick 3c6d690
bump go.mod
techknowlogick b3969be
use fixed go-swagger version
techknowlogick ba43047
Merge branch 'main' into jsonv2
techknowlogick 94d537c
Update GOEXPERIMENT to use jsonv2 by default
techknowlogick 82b2d97
fix lint
techknowlogick 502b4fb
clean up modernizer fixes
techknowlogick d4b1e10
try to fix lint
techknowlogick 8f3b218
try to use go1.25 waitgroup logic
techknowlogick 34af20c
fixup test fails
techknowlogick a65bff4
Merge remote-tracking branch 'upstream/main' into jsonv2
cdcb9bd
adjust jsonv2 output to become similar to v1
ecc304a
resolve vagrant test failure
748b590
the security check has a panic when using the experimental go library
60b26b0
resolve panic in assetfs parsing of embeded data
28beca0
Merge branch 'main' into jsonv2
wxiaoguang 07afc37
fix
wxiaoguang cb47618
fix
wxiaoguang f302ca8
fix
wxiaoguang ad93826
fine tune
wxiaoguang 7f23bb9
Merge branch 'main' into jsonv2
wxiaoguang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2025 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
//go:build !goexperiment.jsonv2 | ||
|
||
package json | ||
|
||
import ( | ||
"io" | ||
|
||
jsoniter "github.com/json-iterator/go" | ||
) | ||
|
||
func getDefaultJSONHandler() Interface { | ||
return JSONiter{jsoniter.ConfigCompatibleWithStandardLibrary} | ||
} | ||
|
||
func MarshalKeepOptionalEmpty(v any) ([]byte, error) { | ||
return DefaultJSONHandler.Marshal(v) | ||
} | ||
|
||
func NewDecoderCaseInsensitive(reader io.Reader) Decoder { | ||
return DefaultJSONHandler.NewDecoder(reader) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2025 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
techknowlogick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//go:build goexperiment.jsonv2 | ||
|
||
package json | ||
techknowlogick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"bytes" | ||
jsonv1 "encoding/json" //nolint:depguard // this package wraps it | ||
jsonv2 "encoding/json/v2" //nolint:depguard // this package wraps it | ||
"io" | ||
) | ||
|
||
// JSONv2 implements Interface via encoding/json/v2 | ||
// Requires GOEXPERIMENT=jsonv2 to be set at build time | ||
type JSONv2 struct { | ||
marshalOptions jsonv2.Options | ||
marshalKeepOptionalEmptyOptions jsonv2.Options | ||
unmarshalOptions jsonv2.Options | ||
unmarshalCaseInsensitiveOptions jsonv2.Options | ||
} | ||
|
||
var jsonV2 JSONv2 | ||
|
||
func init() { | ||
commonMarshalOptions := []jsonv2.Options{ | ||
jsonv2.FormatNilSliceAsNull(true), | ||
jsonv2.FormatNilMapAsNull(true), | ||
} | ||
jsonV2.marshalOptions = jsonv2.JoinOptions(commonMarshalOptions...) | ||
jsonV2.unmarshalOptions = jsonv2.DefaultOptionsV2() | ||
|
||
// By default, "json/v2" omitempty removes all `""` empty strings, no matter where it comes from. | ||
// v1 has a different behavior: if the `""` is from a null pointer, or a Marshal function, it is kept. | ||
// 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 | ||
jsonV2.marshalKeepOptionalEmptyOptions = jsonv2.JoinOptions(append(commonMarshalOptions, jsonv1.OmitEmptyWithLegacySemantics(true))...) | ||
|
||
// Some legacy code uses case-insensitive matching (for example: parsing oci.ImageConfig) | ||
jsonV2.unmarshalCaseInsensitiveOptions = jsonv2.JoinOptions(jsonv2.MatchCaseInsensitiveNames(true)) | ||
} | ||
|
||
func getDefaultJSONHandler() Interface { | ||
return &jsonV2 | ||
} | ||
|
||
func MarshalKeepOptionalEmpty(v any) ([]byte, error) { | ||
return jsonv2.Marshal(v, jsonV2.marshalKeepOptionalEmptyOptions) | ||
} | ||
|
||
func (j *JSONv2) Marshal(v any) ([]byte, error) { | ||
return jsonv2.Marshal(v, j.marshalOptions) | ||
} | ||
|
||
func (j *JSONv2) Unmarshal(data []byte, v any) error { | ||
return jsonv2.Unmarshal(data, v, j.unmarshalOptions) | ||
} | ||
|
||
func (j *JSONv2) NewEncoder(writer io.Writer) Encoder { | ||
return &jsonV2Encoder{writer: writer, opts: j.marshalOptions} | ||
} | ||
|
||
func (j *JSONv2) NewDecoder(reader io.Reader) Decoder { | ||
return &jsonV2Decoder{reader: reader, opts: j.unmarshalOptions} | ||
} | ||
|
||
// Indent implements Interface using standard library (JSON v2 doesn't have Indent yet) | ||
func (*JSONv2) Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { | ||
return jsonv1.Indent(dst, src, prefix, indent) | ||
} | ||
|
||
type jsonV2Encoder struct { | ||
writer io.Writer | ||
opts jsonv2.Options | ||
} | ||
|
||
func (e *jsonV2Encoder) Encode(v any) error { | ||
return jsonv2.MarshalWrite(e.writer, v, e.opts) | ||
} | ||
|
||
type jsonV2Decoder struct { | ||
reader io.Reader | ||
opts jsonv2.Options | ||
} | ||
|
||
func (d *jsonV2Decoder) Decode(v any) error { | ||
return jsonv2.UnmarshalRead(d.reader, v, d.opts) | ||
} | ||
|
||
func NewDecoderCaseInsensitive(reader io.Reader) Decoder { | ||
return &jsonV2Decoder{reader: reader, opts: jsonV2.unmarshalCaseInsensitiveOptions} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fyi we do not use submake, so this
export
is useless.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why it is "useless"? Will
go ...
inherit non-exported variables?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, all commands will see the variables like
FOO ?= bar
,export
in a Makefile is only for sub-make:https://www.gnu.org/software/make/manual/html_node/Variables_002fRecursion.html#Communicating-Variables-to-a-Sub_002dmake
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure or have you tested? I don't think the the GNU toolchain can have such a counterintuitive, global-polluting and dirty design.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you are right. My test was incorrect. I have always used the explicit form
VAR=$(VAR) cmd
so far, which explicitly passes variables. I think it's the most clean way, especially because the Makefile handles a lot of other commands that don't acceptGOEXPERIMENT
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that "export" is not the best approach, while it just works, so I just kept the original change from techknowlogick's 94d537c