Skip to content

Commit 73440d7

Browse files
committed
feat: make JSON lib configurable
Currently, a mix of goccy/json and encoding/json is used. Both use the same interface; goccy/json is faster, however, it also does questionable things with go:linkname. Create a common package that can be switched (via build tags) from one implementation to another, and use that in all packages.
1 parent ed6737b commit 73440d7

17 files changed

+136
-14
lines changed

api-bucket-notification.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"net/url"
2727
"time"
2828

29-
"github.com/goccy/go-json"
29+
"github.com/minio/minio-go/v7/internal/json"
3030
"github.com/minio/minio-go/v7/pkg/notification"
3131
"github.com/minio/minio-go/v7/pkg/s3utils"
3232
)

api-bucket-replication.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ package minio
2020
import (
2121
"bytes"
2222
"context"
23-
"encoding/json"
2423
"encoding/xml"
2524
"io"
2625
"net/http"
2726
"net/url"
2827
"time"
2928

3029
"github.com/google/uuid"
30+
"github.com/minio/minio-go/v7/internal/json"
3131
"github.com/minio/minio-go/v7/pkg/replication"
3232
"github.com/minio/minio-go/v7/pkg/s3utils"
3333
)

api-prompt-object.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"io"
2424
"net/http"
2525

26-
"github.com/goccy/go-json"
26+
"github.com/minio/minio-go/v7/internal/json"
2727
"github.com/minio/minio-go/v7/pkg/s3utils"
2828
)
2929

api-put-object-fan-out.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package minio
1919

2020
import (
2121
"context"
22-
"encoding/json"
2322
"errors"
2423
"io"
2524
"mime/multipart"
@@ -28,6 +27,7 @@ import (
2827
"strings"
2928
"time"
3029

30+
"github.com/minio/minio-go/v7/internal/json"
3131
"github.com/minio/minio-go/v7/pkg/encrypt"
3232
)
3333

internal/json/json_goccy.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//go:build !stdlibjson
2+
3+
/*
4+
* MinIO Go Library for Amazon S3 Compatible Cloud Storage
5+
* Copyright 2025 MinIO, Inc.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package json
21+
22+
import "github.com/goccy/go-json"
23+
24+
// This file defines the JSON functions used internally and forwards them
25+
// to goccy/go-json. Alternatively, the standard library can be used by setting
26+
// the build tag stdlibjson. This can be useful for testing purposes or if
27+
// goccy/go-json causes issues.
28+
//
29+
// This file does not contain all definitions from goccy/go-json; if needed, more
30+
// can be added, but keep in mind that json_stdlib.go will also need to be
31+
// updated.
32+
33+
var (
34+
// Unmarshal is a wrapper around goccy/go-json Unmarshal function.
35+
Unmarshal = json.Unmarshal
36+
// Marshal is a wrapper around goccy/go-json Marshal function.
37+
Marshal = json.Marshal
38+
// NewEncoder is a wrapper around goccy/go-json NewEncoder function.
39+
NewEncoder = json.NewEncoder
40+
// NewDecoder is a wrapper around goccy/go-json NewDecoder function.
41+
NewDecoder = json.NewDecoder
42+
)
43+
44+
type (
45+
// Encoder is an alias for goccy/go-json Encoder.
46+
Encoder = json.Encoder
47+
// Decoder is an alias for goccy/go-json Decoder.
48+
Decoder = json.Decoder
49+
)

internal/json/json_stdlib.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//go:build stdlibjson
2+
3+
/*
4+
* MinIO Go Library for Amazon S3 Compatible Cloud Storage
5+
* Copyright 2025 MinIO, Inc.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package json
21+
22+
import "encoding/json"
23+
24+
// This file defines the JSON functions used internally and forwards them
25+
// to encoding/json. This is only enabled by setting the build tag stdlibjson,
26+
// otherwise json_goccy.go applies.
27+
// This can be useful for testing purposes or if goccy/go-json (which is used otherwise) causes issues.
28+
//
29+
// This file does not contain all definitions from encoding/json; if needed, more
30+
// can be added, but keep in mind that json_goccy.go will also need to be
31+
// updated.
32+
33+
var (
34+
// Unmarshal is a wrapper around encoding/json Unmarshal function.
35+
Unmarshal = json.Unmarshal
36+
// Marshal is a wrapper around encoding/json Marshal function.
37+
Marshal = json.Marshal
38+
// NewEncoder is a wrapper around encoding/json NewEncoder function.
39+
NewEncoder = json.NewEncoder
40+
// NewDecoder is a wrapper around encoding/json NewDecoder function.
41+
NewDecoder = json.NewDecoder
42+
)
43+
44+
type (
45+
// Encoder is an alias for encoding/json Encoder.
46+
Encoder = json.Encoder
47+
// Decoder is an alias for encoding/json Decoder.
48+
Decoder = json.Decoder
49+
)

pkg/credentials/file_aws_credentials.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package credentials
1919

2020
import (
21-
"encoding/json"
2221
"errors"
2322
"os"
2423
"os/exec"
@@ -27,6 +26,7 @@ import (
2726
"time"
2827

2928
"github.com/go-ini/ini"
29+
"github.com/minio/minio-go/v7/internal/json"
3030
)
3131

3232
// A externalProcessCredentials stores the output of a credential_process

pkg/credentials/file_minio_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"path/filepath"
2323
"runtime"
2424

25-
"github.com/goccy/go-json"
25+
"github.com/minio/minio-go/v7/internal/json"
2626
)
2727

2828
// A FileMinioClient retrieves credentials from the current user's home

pkg/credentials/iam_aws.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
"strings"
3232
"time"
3333

34-
"github.com/goccy/go-json"
34+
"github.com/minio/minio-go/v7/internal/json"
3535
)
3636

3737
// DefaultExpiryWindow - Default expiry window.

pkg/encrypt/server-side.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"errors"
2424
"net/http"
2525

26-
"github.com/goccy/go-json"
26+
"github.com/minio/minio-go/v7/internal/json"
2727
"golang.org/x/crypto/argon2"
2828
)
2929

0 commit comments

Comments
 (0)