Skip to content

Commit 9f31f3a

Browse files
authored
Add an abstract json layout to make it's easier to change json library (#16528)
* Add an abstract json layout to make it's easier to change json library * Fix import * Fix import sequence * Fix blank lines * Fix blank lines
1 parent e0f9635 commit 9f31f3a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+272
-264
lines changed

build/generate-emoji.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"strings"
2121
"unicode/utf8"
2222

23-
jsoniter "github.com/json-iterator/go"
23+
"code.gitea.io/gitea/modules/json"
2424
)
2525

2626
const (
@@ -51,7 +51,6 @@ func (e Emoji) MarshalJSON() ([]byte, error) {
5151
x.UnicodeVersion = ""
5252
x.Description = ""
5353
x.SkinTones = false
54-
json := jsoniter.ConfigCompatibleWithStandardLibrary
5554
return json.Marshal(x)
5655
}
5756

@@ -103,7 +102,6 @@ func generate() ([]byte, error) {
103102

104103
// unmarshal
105104
var data Gemoji
106-
json := jsoniter.ConfigCompatibleWithStandardLibrary
107105
err = json.Unmarshal(body, &data)
108106
if err != nil {
109107
return nil, err

cmd/dump.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ import (
1515
"time"
1616

1717
"code.gitea.io/gitea/models"
18+
"code.gitea.io/gitea/modules/json"
1819
"code.gitea.io/gitea/modules/log"
1920
"code.gitea.io/gitea/modules/setting"
2021
"code.gitea.io/gitea/modules/storage"
2122
"code.gitea.io/gitea/modules/util"
2223

2324
"gitea.com/go-chi/session"
24-
jsoniter "github.com/json-iterator/go"
2525
archiver "github.com/mholt/archiver/v3"
2626
"github.com/urfave/cli"
2727
)
@@ -306,7 +306,6 @@ func runDump(ctx *cli.Context) error {
306306
var excludes []string
307307
if setting.Cfg.Section("session").Key("PROVIDER").Value() == "file" {
308308
var opts session.Options
309-
json := jsoniter.ConfigCompatibleWithStandardLibrary
310309
if err = json.Unmarshal([]byte(setting.SessionConfig.ProviderConfig), &opts); err != nil {
311310
return err
312311
}

cmd/serv.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import (
1717
"time"
1818

1919
"code.gitea.io/gitea/models"
20+
"code.gitea.io/gitea/modules/json"
2021
"code.gitea.io/gitea/modules/log"
2122
"code.gitea.io/gitea/modules/pprof"
2223
"code.gitea.io/gitea/modules/private"
2324
"code.gitea.io/gitea/modules/setting"
2425
"code.gitea.io/gitea/services/lfs"
2526

2627
"github.com/golang-jwt/jwt"
27-
jsoniter "github.com/json-iterator/go"
2828
"github.com/kballard/go-shellquote"
2929
"github.com/urfave/cli"
3030
)
@@ -265,7 +265,6 @@ func runServ(c *cli.Context) error {
265265
}
266266
tokenAuthentication.Header["Authorization"] = fmt.Sprintf("Bearer %s", tokenString)
267267

268-
json := jsoniter.ConfigCompatibleWithStandardLibrary
269268
enc := json.NewEncoder(os.Stdout)
270269
err = enc.Encode(tokenAuthentication)
271270
if err != nil {

integrations/api_admin_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"testing"
1111

1212
"code.gitea.io/gitea/models"
13+
"code.gitea.io/gitea/modules/json"
1314
api "code.gitea.io/gitea/modules/structs"
1415

15-
jsoniter "github.com/json-iterator/go"
1616
"github.com/stretchr/testify/assert"
1717
)
1818

@@ -190,7 +190,6 @@ func TestAPIEditUser(t *testing.T) {
190190
resp := session.MakeRequest(t, req, http.StatusUnprocessableEntity)
191191

192192
errMap := make(map[string]interface{})
193-
json := jsoniter.ConfigCompatibleWithStandardLibrary
194193
json.Unmarshal(resp.Body.Bytes(), &errMap)
195194
assert.EqualValues(t, "email is not allowed to be empty string", errMap["message"].(string))
196195

integrations/api_helper_for_declarative_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import (
1414
"time"
1515

1616
"code.gitea.io/gitea/models"
17+
"code.gitea.io/gitea/modules/json"
1718
"code.gitea.io/gitea/modules/queue"
1819
api "code.gitea.io/gitea/modules/structs"
1920
"code.gitea.io/gitea/services/forms"
2021

21-
jsoniter "github.com/json-iterator/go"
2222
"github.com/stretchr/testify/assert"
2323
)
2424

@@ -231,7 +231,6 @@ func doAPICreatePullRequest(ctx APITestContext, owner, repo, baseBranch, headBra
231231
}
232232
resp := ctx.Session.MakeRequest(t, req, expected)
233233

234-
json := jsoniter.ConfigCompatibleWithStandardLibrary
235234
decoder := json.NewDecoder(resp.Body)
236235
pr := api.PullRequest{}
237236
err := decoder.Decode(&pr)
@@ -251,7 +250,6 @@ func doAPIGetPullRequest(ctx APITestContext, owner, repo string, index int64) fu
251250
}
252251
resp := ctx.Session.MakeRequest(t, req, expected)
253252

254-
json := jsoniter.ConfigCompatibleWithStandardLibrary
255253
decoder := json.NewDecoder(resp.Body)
256254
pr := api.PullRequest{}
257255
err := decoder.Decode(&pr)

integrations/api_pull_review_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import (
1010
"testing"
1111

1212
"code.gitea.io/gitea/models"
13+
"code.gitea.io/gitea/modules/json"
1314
api "code.gitea.io/gitea/modules/structs"
14-
jsoniter "github.com/json-iterator/go"
15+
1516
"github.com/stretchr/testify/assert"
1617
)
1718

@@ -189,7 +190,6 @@ func TestAPIPullReview(t *testing.T) {
189190
})
190191
resp = session.MakeRequest(t, req, http.StatusUnprocessableEntity)
191192
errMap := make(map[string]interface{})
192-
json := jsoniter.ConfigCompatibleWithStandardLibrary
193193
json.Unmarshal(resp.Body.Bytes(), &errMap)
194194
assert.EqualValues(t, "review event COMMENT requires a body or a comment", errMap["message"].(string))
195195

integrations/api_repo_lfs_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import (
1313
"testing"
1414

1515
"code.gitea.io/gitea/models"
16+
"code.gitea.io/gitea/modules/json"
1617
"code.gitea.io/gitea/modules/lfs"
1718
"code.gitea.io/gitea/modules/setting"
1819

19-
jsoniter "github.com/json-iterator/go"
2020
"github.com/stretchr/testify/assert"
2121
)
2222

@@ -86,7 +86,6 @@ func TestAPILFSBatch(t *testing.T) {
8686
decodeResponse := func(t *testing.T, b *bytes.Buffer) *lfs.BatchResponse {
8787
var br lfs.BatchResponse
8888

89-
json := jsoniter.ConfigCompatibleWithStandardLibrary
9089
assert.NoError(t, json.Unmarshal(b.Bytes(), &br))
9190
return &br
9291
}

integrations/create_no_session_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import (
1212
"path/filepath"
1313
"testing"
1414

15+
"code.gitea.io/gitea/modules/json"
1516
"code.gitea.io/gitea/modules/setting"
1617
"code.gitea.io/gitea/modules/util"
1718
"code.gitea.io/gitea/routers"
1819

1920
"gitea.com/go-chi/session"
20-
jsoniter "github.com/json-iterator/go"
2121
"github.com/stretchr/testify/assert"
2222
)
2323

@@ -63,7 +63,6 @@ func TestSessionFileCreation(t *testing.T) {
6363

6464
var config session.Options
6565

66-
json := jsoniter.ConfigCompatibleWithStandardLibrary
6766
err := json.Unmarshal([]byte(oldSessionConfig), &config)
6867
assert.NoError(t, err)
6968

integrations/integration_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"code.gitea.io/gitea/modules/base"
2929
"code.gitea.io/gitea/modules/git"
3030
"code.gitea.io/gitea/modules/graceful"
31+
"code.gitea.io/gitea/modules/json"
3132
"code.gitea.io/gitea/modules/log"
3233
"code.gitea.io/gitea/modules/queue"
3334
"code.gitea.io/gitea/modules/setting"
@@ -37,7 +38,6 @@ import (
3738
"code.gitea.io/gitea/routers"
3839

3940
"github.com/PuerkitoBio/goquery"
40-
jsoniter "github.com/json-iterator/go"
4141
"github.com/stretchr/testify/assert"
4242
)
4343

@@ -416,7 +416,6 @@ func NewRequestWithValues(t testing.TB, method, urlStr string, values map[string
416416
func NewRequestWithJSON(t testing.TB, method, urlStr string, v interface{}) *http.Request {
417417
t.Helper()
418418

419-
json := jsoniter.ConfigCompatibleWithStandardLibrary
420419
jsonBytes, err := json.Marshal(v)
421420
assert.NoError(t, err)
422421
req := NewRequestWithBody(t, method, urlStr, bytes.NewBuffer(jsonBytes))
@@ -508,7 +507,6 @@ func logUnexpectedResponse(t testing.TB, recorder *httptest.ResponseRecorder) {
508507
func DecodeJSON(t testing.TB, resp *httptest.ResponseRecorder, v interface{}) {
509508
t.Helper()
510509

511-
json := jsoniter.ConfigCompatibleWithStandardLibrary
512510
decoder := json.NewDecoder(resp.Body)
513511
assert.NoError(t, decoder.Decode(v))
514512
}

integrations/lfs_getobject_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import (
1414

1515
"code.gitea.io/gitea/models"
1616
"code.gitea.io/gitea/modules/git"
17+
"code.gitea.io/gitea/modules/json"
1718
"code.gitea.io/gitea/modules/lfs"
1819
"code.gitea.io/gitea/modules/setting"
1920
"code.gitea.io/gitea/routers/web"
2021

21-
jsoniter "github.com/json-iterator/go"
2222
gzipp "github.com/klauspost/compress/gzip"
2323
"github.com/stretchr/testify/assert"
2424
)
@@ -206,7 +206,7 @@ func TestGetLFSRange(t *testing.T) {
206206
assert.Equal(t, tt.out, resp.Body.String())
207207
} else {
208208
var er lfs.ErrorResponse
209-
err := jsoniter.Unmarshal(resp.Body.Bytes(), &er)
209+
err := json.Unmarshal(resp.Body.Bytes(), &er)
210210
assert.NoError(t, err)
211211
assert.Equal(t, tt.out, er.Message)
212212
}

integrations/oauth_test.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
"io/ioutil"
1010
"testing"
1111

12+
"code.gitea.io/gitea/modules/json"
1213
"code.gitea.io/gitea/modules/setting"
1314

14-
jsoniter "github.com/json-iterator/go"
1515
"github.com/stretchr/testify/assert"
1616
)
1717

@@ -71,7 +71,6 @@ func TestAccessTokenExchange(t *testing.T) {
7171
}
7272
parsed := new(response)
7373

74-
json := jsoniter.ConfigCompatibleWithStandardLibrary
7574
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
7675
assert.True(t, len(parsed.AccessToken) > 10)
7776
assert.True(t, len(parsed.RefreshToken) > 10)
@@ -96,7 +95,6 @@ func TestAccessTokenExchangeWithoutPKCE(t *testing.T) {
9695
}
9796
parsed := new(response)
9897

99-
json := jsoniter.ConfigCompatibleWithStandardLibrary
10098
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
10199
assert.True(t, len(parsed.AccessToken) > 10)
102100
assert.True(t, len(parsed.RefreshToken) > 10)
@@ -186,7 +184,6 @@ func TestAccessTokenExchangeWithBasicAuth(t *testing.T) {
186184
}
187185
parsed := new(response)
188186

189-
json := jsoniter.ConfigCompatibleWithStandardLibrary
190187
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
191188
assert.True(t, len(parsed.AccessToken) > 10)
192189
assert.True(t, len(parsed.RefreshToken) > 10)
@@ -230,7 +227,6 @@ func TestRefreshTokenInvalidation(t *testing.T) {
230227
}
231228
parsed := new(response)
232229

233-
json := jsoniter.ConfigCompatibleWithStandardLibrary
234230
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
235231

236232
// test without invalidation

integrations/repo_commits_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
"path"
1111
"testing"
1212

13+
"code.gitea.io/gitea/modules/json"
1314
"code.gitea.io/gitea/modules/setting"
1415
api "code.gitea.io/gitea/modules/structs"
1516

16-
jsoniter "github.com/json-iterator/go"
1717
"github.com/stretchr/testify/assert"
1818
)
1919

@@ -86,7 +86,6 @@ func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) {
8686
}
8787

8888
func testRepoCommitsWithStatus(t *testing.T, resp, respOne *httptest.ResponseRecorder, state string) {
89-
json := jsoniter.ConfigCompatibleWithStandardLibrary
9089
var statuses []*api.CommitStatus
9190
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), &statuses))
9291
var status api.CombinedStatus

integrations/testlogger.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414
"testing"
1515
"time"
1616

17+
"code.gitea.io/gitea/modules/json"
1718
"code.gitea.io/gitea/modules/log"
1819
"code.gitea.io/gitea/modules/queue"
19-
jsoniter "github.com/json-iterator/go"
2020
)
2121

2222
var (
@@ -158,7 +158,6 @@ func NewTestLogger() log.LoggerProvider {
158158
// Init inits connection writer with json config.
159159
// json config only need key "level".
160160
func (log *TestLogger) Init(config string) error {
161-
json := jsoniter.ConfigCompatibleWithStandardLibrary
162161
err := json.Unmarshal([]byte(config), log)
163162
if err != nil {
164163
return err

models/helper.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package models
77
import (
88
"encoding/binary"
99

10-
jsoniter "github.com/json-iterator/go"
10+
"code.gitea.io/gitea/modules/json"
1111
)
1212

1313
func keysInt64(m map[int64]struct{}) []int64 {
@@ -37,7 +37,6 @@ func valuesUser(m map[int64]*User) []*User {
3737
// JSONUnmarshalHandleDoubleEncode - due to a bug in xorm (see https://gitea.com/xorm/xorm/pulls/1957) - it's
3838
// possible that a Blob may be double encoded or gain an unwanted prefix of 0xff 0xfe.
3939
func JSONUnmarshalHandleDoubleEncode(bs []byte, v interface{}) error {
40-
json := jsoniter.ConfigCompatibleWithStandardLibrary
4140
err := json.Unmarshal(bs, v)
4241
if err != nil {
4342
ok := true

models/issue_comment.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ import (
1515
"unicode/utf8"
1616

1717
"code.gitea.io/gitea/modules/git"
18+
"code.gitea.io/gitea/modules/json"
1819
"code.gitea.io/gitea/modules/log"
1920
"code.gitea.io/gitea/modules/markup"
2021
"code.gitea.io/gitea/modules/markup/markdown"
2122
"code.gitea.io/gitea/modules/references"
2223
"code.gitea.io/gitea/modules/structs"
2324
"code.gitea.io/gitea/modules/timeutil"
24-
jsoniter "github.com/json-iterator/go"
2525

2626
"xorm.io/builder"
2727
"xorm.io/xorm"
@@ -654,7 +654,6 @@ func (c *Comment) LoadPushCommits() (err error) {
654654

655655
var data PushActionContent
656656

657-
json := jsoniter.ConfigCompatibleWithStandardLibrary
658657
err = json.Unmarshal([]byte(c.Content), &data)
659658
if err != nil {
660659
return
@@ -1249,7 +1248,6 @@ func CreatePushPullComment(pusher *User, pr *PullRequest, oldCommitID, newCommit
12491248

12501249
ops.Issue = pr.Issue
12511250

1252-
json := jsoniter.ConfigCompatibleWithStandardLibrary
12531251
dataJSON, err := json.Marshal(data)
12541252
if err != nil {
12551253
return nil, err

models/migrations/testlogger_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414
"testing"
1515
"time"
1616

17+
"code.gitea.io/gitea/modules/json"
1718
"code.gitea.io/gitea/modules/log"
1819
"code.gitea.io/gitea/modules/queue"
19-
jsoniter "github.com/json-iterator/go"
2020
)
2121

2222
var (
@@ -158,7 +158,6 @@ func NewTestLogger() log.LoggerProvider {
158158
// Init inits connection writer with json config.
159159
// json config only need key "level".
160160
func (log *TestLogger) Init(config string) error {
161-
json := jsoniter.ConfigCompatibleWithStandardLibrary
162161
err := json.Unmarshal([]byte(config), log)
163162
if err != nil {
164163
return err

0 commit comments

Comments
 (0)