From 9f31f3aa8ac4f6bfc8390c9ae63b2a387b414c88 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 25 Jul 2021 00:03:58 +0800 Subject: [PATCH] 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 --- build/generate-emoji.go | 4 +- cmd/dump.go | 3 +- cmd/serv.go | 3 +- integrations/api_admin_test.go | 3 +- .../api_helper_for_declarative_test.go | 4 +- integrations/api_pull_review_test.go | 4 +- integrations/api_repo_lfs_test.go | 3 +- integrations/create_no_session_test.go | 3 +- integrations/integration_test.go | 4 +- integrations/lfs_getobject_test.go | 4 +- integrations/oauth_test.go | 6 +- integrations/repo_commits_test.go | 3 +- integrations/testlogger.go | 3 +- models/helper.go | 3 +- models/issue_comment.go | 4 +- models/migrations/testlogger_test.go | 3 +- models/migrations/v130.go | 3 +- models/migrations/v180.go | 3 +- models/migrations/v189.go | 6 +- models/migrations/v189_test.go | 7 +- models/repo_unit.go | 7 +- models/task.go | 5 +- models/user_heatmap_test.go | 4 +- models/webhook.go | 7 +- models/webhook_test.go | 3 +- modules/cache/cache_twoqueue.go | 4 +- modules/context/context.go | 3 +- modules/eventsource/event.go | 4 +- modules/httplib/httplib.go | 3 +- modules/indexer/code/elastic_search.go | 3 +- modules/json/json.go | 142 ++++++++++++++++++ modules/lfs/http_client.go | 7 +- modules/lfs/http_client_test.go | 9 +- modules/lfs/transferadapter.go | 7 +- modules/lfs/transferadapter_test.go | 7 +- modules/log/conn.go | 3 +- modules/log/console.go | 3 +- modules/log/file.go | 3 +- modules/log/level.go | 3 +- modules/log/level_test.go | 4 +- modules/log/smtp.go | 3 +- modules/notification/action/action.go | 4 +- modules/private/hook.go | 5 +- modules/private/internal.go | 3 +- modules/private/mail.go | 3 +- modules/private/manager.go | 4 +- modules/private/restore_repo.go | 3 +- modules/private/serv.go | 5 +- modules/queue/helper.go | 7 +- modules/queue/manager.go | 3 +- modules/queue/queue_bytefifo.go | 4 +- modules/queue/queue_test.go | 5 +- modules/queue/setting.go | 4 +- modules/recaptcha/recaptcha.go | 4 +- modules/session/virtual.go | 4 +- modules/setting/log.go | 5 +- modules/setting/session.go | 4 +- modules/setting/setting.go | 3 +- modules/setting/setting_test.go | 4 +- modules/storage/helper.go | 5 +- modules/structs/hook.go | 13 +- modules/structs/user.go | 3 +- modules/task/migrate.go | 3 +- modules/task/task.go | 4 +- modules/templates/helper.go | 7 +- routers/api/v1/utils/hook.go | 4 +- routers/private/mail.go | 4 +- routers/private/manager.go | 3 +- routers/private/restore_repo.go | 3 +- routers/web/admin/admin.go | 3 +- routers/web/events/events.go | 3 +- routers/web/repo/editor.go | 3 +- routers/web/repo/webhook.go | 12 +- routers/web/user/home.go | 3 +- routers/web/user/oauth.go | 4 +- routers/web/user/task.go | 3 +- services/auth/source/ldap/source.go | 4 +- services/auth/source/oauth2/source.go | 4 +- services/auth/source/pam/source.go | 4 +- services/auth/source/smtp/source.go | 4 +- services/auth/source/sspi/source.go | 3 +- services/gitdiff/gitdiff_test.go | 5 +- services/lfs/locks.go | 6 +- services/lfs/server.go | 8 +- services/pull/pull.go | 3 +- services/webhook/dingtalk.go | 3 +- services/webhook/discord.go | 5 +- services/webhook/feishu.go | 3 +- services/webhook/matrix.go | 6 +- services/webhook/msteams.go | 3 +- services/webhook/slack.go | 5 +- services/webhook/telegram.go | 4 +- services/webhook/wechatwork.go | 2 +- 93 files changed, 272 insertions(+), 264 deletions(-) create mode 100644 modules/json/json.go diff --git a/build/generate-emoji.go b/build/generate-emoji.go index 53c5285b23..c97d865bdc 100644 --- a/build/generate-emoji.go +++ b/build/generate-emoji.go @@ -20,7 +20,7 @@ import ( "strings" "unicode/utf8" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) const ( @@ -51,7 +51,6 @@ func (e Emoji) MarshalJSON() ([]byte, error) { x.UnicodeVersion = "" x.Description = "" x.SkinTones = false - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.Marshal(x) } @@ -103,7 +102,6 @@ func generate() ([]byte, error) { // unmarshal var data Gemoji - json := jsoniter.ConfigCompatibleWithStandardLibrary err = json.Unmarshal(body, &data) if err != nil { return nil, err diff --git a/cmd/dump.go b/cmd/dump.go index 629993b6c8..9ce7c2636a 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -15,13 +15,13 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/storage" "code.gitea.io/gitea/modules/util" "gitea.com/go-chi/session" - jsoniter "github.com/json-iterator/go" archiver "github.com/mholt/archiver/v3" "github.com/urfave/cli" ) @@ -306,7 +306,6 @@ func runDump(ctx *cli.Context) error { var excludes []string if setting.Cfg.Section("session").Key("PROVIDER").Value() == "file" { var opts session.Options - json := jsoniter.ConfigCompatibleWithStandardLibrary if err = json.Unmarshal([]byte(setting.SessionConfig.ProviderConfig), &opts); err != nil { return err } diff --git a/cmd/serv.go b/cmd/serv.go index 3b180fc626..4479902ea1 100644 --- a/cmd/serv.go +++ b/cmd/serv.go @@ -17,6 +17,7 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/pprof" "code.gitea.io/gitea/modules/private" @@ -24,7 +25,6 @@ import ( "code.gitea.io/gitea/services/lfs" "github.com/golang-jwt/jwt" - jsoniter "github.com/json-iterator/go" "github.com/kballard/go-shellquote" "github.com/urfave/cli" ) @@ -265,7 +265,6 @@ func runServ(c *cli.Context) error { } tokenAuthentication.Header["Authorization"] = fmt.Sprintf("Bearer %s", tokenString) - json := jsoniter.ConfigCompatibleWithStandardLibrary enc := json.NewEncoder(os.Stdout) err = enc.Encode(tokenAuthentication) if err != nil { diff --git a/integrations/api_admin_test.go b/integrations/api_admin_test.go index b93179d21d..da6a89c974 100644 --- a/integrations/api_admin_test.go +++ b/integrations/api_admin_test.go @@ -10,9 +10,9 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -190,7 +190,6 @@ func TestAPIEditUser(t *testing.T) { resp := session.MakeRequest(t, req, http.StatusUnprocessableEntity) errMap := make(map[string]interface{}) - json := jsoniter.ConfigCompatibleWithStandardLibrary json.Unmarshal(resp.Body.Bytes(), &errMap) assert.EqualValues(t, "email is not allowed to be empty string", errMap["message"].(string)) diff --git a/integrations/api_helper_for_declarative_test.go b/integrations/api_helper_for_declarative_test.go index d56ae5d27c..5d44cde5bf 100644 --- a/integrations/api_helper_for_declarative_test.go +++ b/integrations/api_helper_for_declarative_test.go @@ -14,11 +14,11 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/queue" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/services/forms" - jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -231,7 +231,6 @@ func doAPICreatePullRequest(ctx APITestContext, owner, repo, baseBranch, headBra } resp := ctx.Session.MakeRequest(t, req, expected) - json := jsoniter.ConfigCompatibleWithStandardLibrary decoder := json.NewDecoder(resp.Body) pr := api.PullRequest{} err := decoder.Decode(&pr) @@ -251,7 +250,6 @@ func doAPIGetPullRequest(ctx APITestContext, owner, repo string, index int64) fu } resp := ctx.Session.MakeRequest(t, req, expected) - json := jsoniter.ConfigCompatibleWithStandardLibrary decoder := json.NewDecoder(resp.Body) pr := api.PullRequest{} err := decoder.Decode(&pr) diff --git a/integrations/api_pull_review_test.go b/integrations/api_pull_review_test.go index bcc0cbffcb..b0e380376b 100644 --- a/integrations/api_pull_review_test.go +++ b/integrations/api_pull_review_test.go @@ -10,8 +10,9 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" + "github.com/stretchr/testify/assert" ) @@ -189,7 +190,6 @@ func TestAPIPullReview(t *testing.T) { }) resp = session.MakeRequest(t, req, http.StatusUnprocessableEntity) errMap := make(map[string]interface{}) - json := jsoniter.ConfigCompatibleWithStandardLibrary json.Unmarshal(resp.Body.Bytes(), &errMap) assert.EqualValues(t, "review event COMMENT requires a body or a comment", errMap["message"].(string)) diff --git a/integrations/api_repo_lfs_test.go b/integrations/api_repo_lfs_test.go index 9e1e2b0418..7aa172064e 100644 --- a/integrations/api_repo_lfs_test.go +++ b/integrations/api_repo_lfs_test.go @@ -13,10 +13,10 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -86,7 +86,6 @@ func TestAPILFSBatch(t *testing.T) { decodeResponse := func(t *testing.T, b *bytes.Buffer) *lfs.BatchResponse { var br lfs.BatchResponse - json := jsoniter.ConfigCompatibleWithStandardLibrary assert.NoError(t, json.Unmarshal(b.Bytes(), &br)) return &br } diff --git a/integrations/create_no_session_test.go b/integrations/create_no_session_test.go index 46f111b6f7..d6b3634268 100644 --- a/integrations/create_no_session_test.go +++ b/integrations/create_no_session_test.go @@ -12,12 +12,12 @@ import ( "path/filepath" "testing" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/routers" "gitea.com/go-chi/session" - jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -63,7 +63,6 @@ func TestSessionFileCreation(t *testing.T) { var config session.Options - json := jsoniter.ConfigCompatibleWithStandardLibrary err := json.Unmarshal([]byte(oldSessionConfig), &config) assert.NoError(t, err) diff --git a/integrations/integration_test.go b/integrations/integration_test.go index 8a008ac621..7ddcf3e1cd 100644 --- a/integrations/integration_test.go +++ b/integrations/integration_test.go @@ -28,6 +28,7 @@ import ( "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/graceful" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/queue" "code.gitea.io/gitea/modules/setting" @@ -37,7 +38,6 @@ import ( "code.gitea.io/gitea/routers" "github.com/PuerkitoBio/goquery" - jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -416,7 +416,6 @@ func NewRequestWithValues(t testing.TB, method, urlStr string, values map[string func NewRequestWithJSON(t testing.TB, method, urlStr string, v interface{}) *http.Request { t.Helper() - json := jsoniter.ConfigCompatibleWithStandardLibrary jsonBytes, err := json.Marshal(v) assert.NoError(t, err) req := NewRequestWithBody(t, method, urlStr, bytes.NewBuffer(jsonBytes)) @@ -508,7 +507,6 @@ func logUnexpectedResponse(t testing.TB, recorder *httptest.ResponseRecorder) { func DecodeJSON(t testing.TB, resp *httptest.ResponseRecorder, v interface{}) { t.Helper() - json := jsoniter.ConfigCompatibleWithStandardLibrary decoder := json.NewDecoder(resp.Body) assert.NoError(t, decoder.Decode(v)) } diff --git a/integrations/lfs_getobject_test.go b/integrations/lfs_getobject_test.go index c99500f469..063f72b25c 100644 --- a/integrations/lfs_getobject_test.go +++ b/integrations/lfs_getobject_test.go @@ -14,11 +14,11 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/routers/web" - jsoniter "github.com/json-iterator/go" gzipp "github.com/klauspost/compress/gzip" "github.com/stretchr/testify/assert" ) @@ -206,7 +206,7 @@ func TestGetLFSRange(t *testing.T) { assert.Equal(t, tt.out, resp.Body.String()) } else { var er lfs.ErrorResponse - err := jsoniter.Unmarshal(resp.Body.Bytes(), &er) + err := json.Unmarshal(resp.Body.Bytes(), &er) assert.NoError(t, err) assert.Equal(t, tt.out, er.Message) } diff --git a/integrations/oauth_test.go b/integrations/oauth_test.go index 2da182d0c8..c97db777be 100644 --- a/integrations/oauth_test.go +++ b/integrations/oauth_test.go @@ -9,9 +9,9 @@ import ( "io/ioutil" "testing" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -71,7 +71,6 @@ func TestAccessTokenExchange(t *testing.T) { } parsed := new(response) - json := jsoniter.ConfigCompatibleWithStandardLibrary assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed)) assert.True(t, len(parsed.AccessToken) > 10) assert.True(t, len(parsed.RefreshToken) > 10) @@ -96,7 +95,6 @@ func TestAccessTokenExchangeWithoutPKCE(t *testing.T) { } parsed := new(response) - json := jsoniter.ConfigCompatibleWithStandardLibrary assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed)) assert.True(t, len(parsed.AccessToken) > 10) assert.True(t, len(parsed.RefreshToken) > 10) @@ -186,7 +184,6 @@ func TestAccessTokenExchangeWithBasicAuth(t *testing.T) { } parsed := new(response) - json := jsoniter.ConfigCompatibleWithStandardLibrary assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed)) assert.True(t, len(parsed.AccessToken) > 10) assert.True(t, len(parsed.RefreshToken) > 10) @@ -230,7 +227,6 @@ func TestRefreshTokenInvalidation(t *testing.T) { } parsed := new(response) - json := jsoniter.ConfigCompatibleWithStandardLibrary assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed)) // test without invalidation diff --git a/integrations/repo_commits_test.go b/integrations/repo_commits_test.go index 8dcbcd3683..4be1e4ec8e 100644 --- a/integrations/repo_commits_test.go +++ b/integrations/repo_commits_test.go @@ -10,10 +10,10 @@ import ( "path" "testing" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -86,7 +86,6 @@ func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) { } func testRepoCommitsWithStatus(t *testing.T, resp, respOne *httptest.ResponseRecorder, state string) { - json := jsoniter.ConfigCompatibleWithStandardLibrary var statuses []*api.CommitStatus assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), &statuses)) var status api.CombinedStatus diff --git a/integrations/testlogger.go b/integrations/testlogger.go index 9ea357ba27..9498ad655b 100644 --- a/integrations/testlogger.go +++ b/integrations/testlogger.go @@ -14,9 +14,9 @@ import ( "testing" "time" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/queue" - jsoniter "github.com/json-iterator/go" ) var ( @@ -158,7 +158,6 @@ func NewTestLogger() log.LoggerProvider { // Init inits connection writer with json config. // json config only need key "level". func (log *TestLogger) Init(config string) error { - json := jsoniter.ConfigCompatibleWithStandardLibrary err := json.Unmarshal([]byte(config), log) if err != nil { return err diff --git a/models/helper.go b/models/helper.go index 798fa3fef0..c499b5512d 100644 --- a/models/helper.go +++ b/models/helper.go @@ -7,7 +7,7 @@ package models import ( "encoding/binary" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) func keysInt64(m map[int64]struct{}) []int64 { @@ -37,7 +37,6 @@ func valuesUser(m map[int64]*User) []*User { // JSONUnmarshalHandleDoubleEncode - due to a bug in xorm (see https://gitea.com/xorm/xorm/pulls/1957) - it's // possible that a Blob may be double encoded or gain an unwanted prefix of 0xff 0xfe. func JSONUnmarshalHandleDoubleEncode(bs []byte, v interface{}) error { - json := jsoniter.ConfigCompatibleWithStandardLibrary err := json.Unmarshal(bs, v) if err != nil { ok := true diff --git a/models/issue_comment.go b/models/issue_comment.go index 4c5b77ff8e..ae2dc0811f 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -15,13 +15,13 @@ import ( "unicode/utf8" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/references" "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" - jsoniter "github.com/json-iterator/go" "xorm.io/builder" "xorm.io/xorm" @@ -654,7 +654,6 @@ func (c *Comment) LoadPushCommits() (err error) { var data PushActionContent - json := jsoniter.ConfigCompatibleWithStandardLibrary err = json.Unmarshal([]byte(c.Content), &data) if err != nil { return @@ -1249,7 +1248,6 @@ func CreatePushPullComment(pusher *User, pr *PullRequest, oldCommitID, newCommit ops.Issue = pr.Issue - json := jsoniter.ConfigCompatibleWithStandardLibrary dataJSON, err := json.Marshal(data) if err != nil { return nil, err diff --git a/models/migrations/testlogger_test.go b/models/migrations/testlogger_test.go index 8d6e61ae64..5e3d880b54 100644 --- a/models/migrations/testlogger_test.go +++ b/models/migrations/testlogger_test.go @@ -14,9 +14,9 @@ import ( "testing" "time" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/queue" - jsoniter "github.com/json-iterator/go" ) var ( @@ -158,7 +158,6 @@ func NewTestLogger() log.LoggerProvider { // Init inits connection writer with json config. // json config only need key "level". func (log *TestLogger) Init(config string) error { - json := jsoniter.ConfigCompatibleWithStandardLibrary err := json.Unmarshal([]byte(config), log) if err != nil { return err diff --git a/models/migrations/v130.go b/models/migrations/v130.go index 08a8d5fca1..7ffed3fd17 100644 --- a/models/migrations/v130.go +++ b/models/migrations/v130.go @@ -5,8 +5,8 @@ package migrations import ( + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" "xorm.io/xorm" ) @@ -70,7 +70,6 @@ func expandWebhooks(x *xorm.Engine) error { for _, res := range results { var events HookEvent - json := jsoniter.ConfigCompatibleWithStandardLibrary if err = json.Unmarshal([]byte(res.Events), &events); err != nil { return err } diff --git a/models/migrations/v180.go b/models/migrations/v180.go index 8a7637deba..1b735c2035 100644 --- a/models/migrations/v180.go +++ b/models/migrations/v180.go @@ -5,9 +5,9 @@ package migrations import ( + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/util" - jsoniter "github.com/json-iterator/go" "xorm.io/builder" "xorm.io/xorm" ) @@ -105,7 +105,6 @@ func removeCredentials(payload string) (string, error) { } var opts MigrateOptions - json := jsoniter.ConfigCompatibleWithStandardLibrary err := json.Unmarshal([]byte(payload), &opts) if err != nil { return "", err diff --git a/models/migrations/v189.go b/models/migrations/v189.go index 42b996353b..f136a89b4e 100644 --- a/models/migrations/v189.go +++ b/models/migrations/v189.go @@ -8,13 +8,13 @@ import ( "encoding/binary" "fmt" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" + "xorm.io/xorm" ) func unwrapLDAPSourceCfg(x *xorm.Engine) error { jsonUnmarshalHandleDoubleEncode := func(bs []byte, v interface{}) error { - json := jsoniter.ConfigCompatibleWithStandardLibrary err := json.Unmarshal(bs, v) if err != nil { ok := true @@ -84,7 +84,7 @@ func unwrapLDAPSourceCfg(x *xorm.Engine) error { return fmt.Errorf("failed to unmarshal %s: %w", string(source.Cfg), err) } if wrapped.Source != nil && len(wrapped.Source) > 0 { - bs, err := jsoniter.Marshal(wrapped.Source) + bs, err := json.Marshal(wrapped.Source) if err != nil { return err } diff --git a/models/migrations/v189_test.go b/models/migrations/v189_test.go index f4fe6dec3f..fcae5dcc73 100644 --- a/models/migrations/v189_test.go +++ b/models/migrations/v189_test.go @@ -7,7 +7,8 @@ package migrations import ( "testing" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" + "github.com/stretchr/testify/assert" ) @@ -65,12 +66,12 @@ func Test_unwrapLDAPSourceCfg(t *testing.T) { converted := map[string]interface{}{} expected := map[string]interface{}{} - if err := jsoniter.Unmarshal([]byte(source.Cfg), &converted); err != nil { + if err := json.Unmarshal([]byte(source.Cfg), &converted); err != nil { assert.NoError(t, err) return } - if err := jsoniter.Unmarshal([]byte(source.Expected), &expected); err != nil { + if err := json.Unmarshal([]byte(source.Expected), &expected); err != nil { assert.NoError(t, err) return } diff --git a/models/repo_unit.go b/models/repo_unit.go index c5eac2656f..2a20205fe4 100644 --- a/models/repo_unit.go +++ b/models/repo_unit.go @@ -7,9 +7,9 @@ package models import ( "fmt" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/timeutil" - jsoniter "github.com/json-iterator/go" "xorm.io/xorm" "xorm.io/xorm/convert" ) @@ -33,7 +33,6 @@ func (cfg *UnitConfig) FromDB(bs []byte) error { // ToDB exports a UnitConfig to a serialized format. func (cfg *UnitConfig) ToDB() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.Marshal(cfg) } @@ -49,7 +48,6 @@ func (cfg *ExternalWikiConfig) FromDB(bs []byte) error { // ToDB exports a ExternalWikiConfig to a serialized format. func (cfg *ExternalWikiConfig) ToDB() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.Marshal(cfg) } @@ -67,7 +65,6 @@ func (cfg *ExternalTrackerConfig) FromDB(bs []byte) error { // ToDB exports a ExternalTrackerConfig to a serialized format. func (cfg *ExternalTrackerConfig) ToDB() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.Marshal(cfg) } @@ -85,7 +82,6 @@ func (cfg *IssuesConfig) FromDB(bs []byte) error { // ToDB exports a IssuesConfig to a serialized format. func (cfg *IssuesConfig) ToDB() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.Marshal(cfg) } @@ -109,7 +105,6 @@ func (cfg *PullRequestsConfig) FromDB(bs []byte) error { // ToDB exports a PullRequestsConfig to a serialized format. func (cfg *PullRequestsConfig) ToDB() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.Marshal(cfg) } diff --git a/models/task.go b/models/task.go index 5f9ccc6bfa..7dfcaea2e3 100644 --- a/models/task.go +++ b/models/task.go @@ -7,13 +7,13 @@ package models import ( "fmt" + "code.gitea.io/gitea/modules/json" migration "code.gitea.io/gitea/modules/migrations/base" "code.gitea.io/gitea/modules/secret" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" - jsoniter "github.com/json-iterator/go" "xorm.io/builder" ) @@ -114,7 +114,6 @@ func (task *Task) UpdateCols(cols ...string) error { func (task *Task) MigrateConfig() (*migration.MigrateOptions, error) { if task.Type == structs.TaskTypeMigrateRepo { var opts migration.MigrateOptions - json := jsoniter.ConfigCompatibleWithStandardLibrary err := json.Unmarshal([]byte(task.PayloadContent), &opts) if err != nil { return nil, err @@ -190,7 +189,6 @@ func GetMigratingTaskByID(id, doerID int64) (*Task, *migration.MigrateOptions, e } var opts migration.MigrateOptions - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal([]byte(task.PayloadContent), &opts); err != nil { return nil, nil, err } @@ -244,7 +242,6 @@ func FinishMigrateTask(task *Task) error { conf.AuthPasswordEncrypted = "" conf.AuthTokenEncrypted = "" conf.CloneAddrEncrypted = "" - json := jsoniter.ConfigCompatibleWithStandardLibrary confBytes, err := json.Marshal(conf) if err != nil { return err diff --git a/models/user_heatmap_test.go b/models/user_heatmap_test.go index b2aaea6499..bdf7766652 100644 --- a/models/user_heatmap_test.go +++ b/models/user_heatmap_test.go @@ -8,7 +8,8 @@ import ( "fmt" "testing" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" + "github.com/stretchr/testify/assert" ) @@ -68,7 +69,6 @@ func TestGetUserHeatmapDataByUser(t *testing.T) { assert.Equal(t, tc.CountResult, contributions, fmt.Sprintf("testcase %d", i)) // Test JSON rendering - json := jsoniter.ConfigCompatibleWithStandardLibrary jsonData, err := json.Marshal(heatmap) assert.NoError(t, err) assert.Equal(t, tc.JSONResult, string(jsonData)) diff --git a/models/webhook.go b/models/webhook.go index 9c8c0a568b..138ba26bde 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -11,13 +11,13 @@ import ( "strings" "time" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" gouuid "github.com/google/uuid" - jsoniter "github.com/json-iterator/go" ) // HookContentType is the content type of a web hook @@ -160,8 +160,6 @@ type Webhook struct { // AfterLoad updates the webhook object upon setting a column func (w *Webhook) AfterLoad() { w.HookEvent = &HookEvent{} - - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil { log.Error("Unmarshal[%d]: %v", w.ID, err) } @@ -174,7 +172,6 @@ func (w *Webhook) History(page int) ([]*HookTask, error) { // UpdateEvent handles conversion from HookEvent to Events. func (w *Webhook) UpdateEvent() error { - json := jsoniter.ConfigCompatibleWithStandardLibrary data, err := json.Marshal(w.HookEvent) w.Events = string(data) return err @@ -687,7 +684,6 @@ func (t *HookTask) AfterLoad() { } t.RequestInfo = &HookRequest{} - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal([]byte(t.RequestContent), t.RequestInfo); err != nil { log.Error("Unmarshal RequestContent[%d]: %v", t.ID, err) } @@ -701,7 +697,6 @@ func (t *HookTask) AfterLoad() { } func (t *HookTask) simpleMarshalJSON(v interface{}) string { - json := jsoniter.ConfigCompatibleWithStandardLibrary p, err := json.Marshal(v) if err != nil { log.Error("Marshal [%d]: %v", t.ID, err) diff --git a/models/webhook_test.go b/models/webhook_test.go index cab44a120e..84520666c0 100644 --- a/models/webhook_test.go +++ b/models/webhook_test.go @@ -9,9 +9,9 @@ import ( "testing" "time" + "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -58,7 +58,6 @@ func TestWebhook_UpdateEvent(t *testing.T) { assert.NoError(t, webhook.UpdateEvent()) assert.NotEmpty(t, webhook.Events) actualHookEvent := &HookEvent{} - json := jsoniter.ConfigCompatibleWithStandardLibrary assert.NoError(t, json.Unmarshal([]byte(webhook.Events), actualHookEvent)) assert.Equal(t, *hookEvent, *actualHookEvent) } diff --git a/modules/cache/cache_twoqueue.go b/modules/cache/cache_twoqueue.go index 7d8fa7c934..275b73f068 100644 --- a/modules/cache/cache_twoqueue.go +++ b/modules/cache/cache_twoqueue.go @@ -9,9 +9,10 @@ import ( "sync" "time" + "code.gitea.io/gitea/modules/json" + mc "gitea.com/go-chi/cache" lru "github.com/hashicorp/golang-lru" - jsoniter "github.com/json-iterator/go" ) // TwoQueueCache represents a LRU 2Q cache adapter implementation @@ -177,7 +178,6 @@ func (c *TwoQueueCache) StartAndGC(opts mc.Options) error { size, err = strconv.Atoi(opts.AdapterConfig) } if err != nil { - json := jsoniter.ConfigCompatibleWithStandardLibrary if !json.Valid([]byte(opts.AdapterConfig)) { return err } diff --git a/modules/context/context.go b/modules/context/context.go index 8949dd7149..6973f0dddc 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -23,6 +23,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" mc "code.gitea.io/gitea/modules/cache" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/templates" @@ -34,7 +35,6 @@ import ( "gitea.com/go-chi/cache" "gitea.com/go-chi/session" "github.com/go-chi/chi" - jsoniter "github.com/json-iterator/go" "github.com/unknwon/com" "github.com/unknwon/i18n" "github.com/unrolled/render" @@ -408,7 +408,6 @@ func (ctx *Context) Error(status int, contents ...string) { func (ctx *Context) JSON(status int, content interface{}) { ctx.Resp.Header().Set("Content-Type", "application/json;charset=utf-8") ctx.Resp.WriteHeader(status) - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.NewEncoder(ctx.Resp).Encode(content); err != nil { ctx.ServerError("Render JSON failed", err) } diff --git a/modules/eventsource/event.go b/modules/eventsource/event.go index a3407a94d2..8fc3221c18 100644 --- a/modules/eventsource/event.go +++ b/modules/eventsource/event.go @@ -11,7 +11,7 @@ import ( "strings" "time" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) func wrapNewlines(w io.Writer, prefix []byte, value []byte) (sum int64, err error) { @@ -80,7 +80,6 @@ func (e *Event) WriteTo(w io.Writer) (int64, error) { data = []byte(v) default: var err error - json := jsoniter.ConfigCompatibleWithStandardLibrary data, err = json.Marshal(e.Data) if err != nil { return sum, err @@ -91,7 +90,6 @@ func (e *Event) WriteTo(w io.Writer) (int64, error) { if err != nil { return sum, err } - } n, err = wrapNewlines(w, []byte("id: "), []byte(e.ID)) diff --git a/modules/httplib/httplib.go b/modules/httplib/httplib.go index 5c8eac8b42..aecdd4346c 100644 --- a/modules/httplib/httplib.go +++ b/modules/httplib/httplib.go @@ -24,7 +24,7 @@ import ( "sync" "time" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) var defaultSetting = Settings{false, "GiteaServer", 60 * time.Second, 60 * time.Second, nil, nil, nil, false} @@ -443,7 +443,6 @@ func (r *Request) ToJSON(v interface{}) error { if err != nil { return err } - json := jsoniter.ConfigCompatibleWithStandardLibrary err = json.Unmarshal(data, v) return err } diff --git a/modules/indexer/code/elastic_search.go b/modules/indexer/code/elastic_search.go index 569917f151..a7a243e24e 100644 --- a/modules/indexer/code/elastic_search.go +++ b/modules/indexer/code/elastic_search.go @@ -18,13 +18,13 @@ import ( "code.gitea.io/gitea/modules/analyze" "code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/typesniffer" "github.com/go-enry/go-enry/v2" - jsoniter "github.com/json-iterator/go" "github.com/olivere/elastic/v7" ) @@ -321,7 +321,6 @@ func convertResult(searchResult *elastic.SearchResult, kw string, pageSize int) repoID, fileName := parseIndexerID(hit.Id) var res = make(map[string]interface{}) - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal(hit.Source, &res); err != nil { return 0, nil, nil, err } diff --git a/modules/json/json.go b/modules/json/json.go new file mode 100644 index 0000000000..be42b6ae6c --- /dev/null +++ b/modules/json/json.go @@ -0,0 +1,142 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package json + +import ( + "bytes" + "encoding/json" + "io" + + jsoniter "github.com/json-iterator/go" +) + +// Encoder represents an encoder for json +type Encoder interface { + Encode(v interface{}) error +} + +// Decoder represents a decoder for json +type Decoder interface { + Decode(v interface{}) error +} + +// Interface represents an interface to handle json data +type Interface interface { + Marshal(v interface{}) ([]byte, error) + Unmarshal(data []byte, v interface{}) error + NewEncoder(writer io.Writer) Encoder + NewDecoder(reader io.Reader) Decoder + Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error +} + +var ( + // DefaultJSONHandler default json handler + DefaultJSONHandler Interface = JSONiter{jsoniter.ConfigCompatibleWithStandardLibrary} + + _ Interface = StdJSON{} + _ Interface = JSONiter{} +) + +// StdJSON implements Interface via encoding/json +type StdJSON struct{} + +// Marshal implements Interface +func (StdJSON) Marshal(v interface{}) ([]byte, error) { + return json.Marshal(v) +} + +// Unmarshal implements Interface +func (StdJSON) Unmarshal(data []byte, v interface{}) error { + return json.Unmarshal(data, v) +} + +// NewEncoder implements Interface +func (StdJSON) NewEncoder(writer io.Writer) Encoder { + return json.NewEncoder(writer) +} + +// NewDecoder implements Interface +func (StdJSON) NewDecoder(reader io.Reader) Decoder { + return json.NewDecoder(reader) +} + +// Indent implements Interface +func (StdJSON) Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { + return json.Indent(dst, src, prefix, indent) +} + +// JSONiter implements Interface via jsoniter +type JSONiter struct { + jsoniter.API +} + +// Marshal implements Interface +func (j JSONiter) Marshal(v interface{}) ([]byte, error) { + return j.API.Marshal(v) +} + +// Unmarshal implements Interface +func (j JSONiter) Unmarshal(data []byte, v interface{}) error { + return j.API.Unmarshal(data, v) +} + +// NewEncoder implements Interface +func (j JSONiter) NewEncoder(writer io.Writer) Encoder { + return j.API.NewEncoder(writer) +} + +// NewDecoder implements Interface +func (j JSONiter) NewDecoder(reader io.Reader) Decoder { + return j.API.NewDecoder(reader) +} + +// Indent implements Interface, since jsoniter don't support Indent, just use encoding/json's +func (j JSONiter) Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { + return json.Indent(dst, src, prefix, indent) +} + +// Marshal converts object as bytes +func Marshal(v interface{}) ([]byte, error) { + return DefaultJSONHandler.Marshal(v) +} + +// Unmarshal decodes object from bytes +func Unmarshal(data []byte, v interface{}) error { + return DefaultJSONHandler.Unmarshal(data, v) +} + +// NewEncoder creates an encoder to write objects to writer +func NewEncoder(writer io.Writer) Encoder { + return DefaultJSONHandler.NewEncoder(writer) +} + +// NewDecoder creates a decoder to read objects from reader +func NewDecoder(reader io.Reader) Decoder { + return DefaultJSONHandler.NewDecoder(reader) +} + +// Indent appends to dst an indented form of the JSON-encoded src. +func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { + return DefaultJSONHandler.Indent(dst, src, prefix, indent) +} + +// MarshalIndent copied from encoding/json +func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) { + b, err := Marshal(v) + if err != nil { + return nil, err + } + var buf bytes.Buffer + err = Indent(&buf, b, prefix, indent) + if err != nil { + return nil, err + } + return buf.Bytes(), nil +} + +// Valid proxy to json.Valid +func Valid(data []byte) bool { + return json.Valid(data) +} diff --git a/modules/lfs/http_client.go b/modules/lfs/http_client.go index e799b80831..31c67903a8 100644 --- a/modules/lfs/http_client.go +++ b/modules/lfs/http_client.go @@ -13,9 +13,8 @@ import ( "net/url" "strings" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" - - jsoniter "github.com/json-iterator/go" ) const batchSize = 20 @@ -69,7 +68,7 @@ func (c *HTTPClient) batch(ctx context.Context, operation string, objects []Poin request := &BatchRequest{operation, c.transferNames(), nil, objects} payload := new(bytes.Buffer) - err := jsoniter.NewEncoder(payload).Encode(request) + err := json.NewEncoder(payload).Encode(request) if err != nil { log.Error("Error encoding json: %v", err) return nil, err @@ -102,7 +101,7 @@ func (c *HTTPClient) batch(ctx context.Context, operation string, objects []Poin } var response BatchResponse - err = jsoniter.NewDecoder(res.Body).Decode(&response) + err = json.NewDecoder(res.Body).Decode(&response) if err != nil { log.Error("Error decoding json: %v", err) return nil, err diff --git a/modules/lfs/http_client_test.go b/modules/lfs/http_client_test.go index 0f633ede54..589773e45b 100644 --- a/modules/lfs/http_client_test.go +++ b/modules/lfs/http_client_test.go @@ -13,7 +13,8 @@ import ( "strings" "testing" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" + "github.com/stretchr/testify/assert" ) @@ -146,7 +147,7 @@ func lfsTestRoundtripHandler(req *http.Request) *http.Response { } payload := new(bytes.Buffer) - jsoniter.NewEncoder(payload).Encode(batchResponse) + json.NewEncoder(payload).Encode(batchResponse) return &http.Response{StatusCode: http.StatusOK, Body: ioutil.NopCloser(payload)} } @@ -160,7 +161,7 @@ func TestHTTPClientDownload(t *testing.T) { assert.Equal(t, MediaType, req.Header.Get("Accept")) var batchRequest BatchRequest - err := jsoniter.NewDecoder(req.Body).Decode(&batchRequest) + err := json.NewDecoder(req.Body).Decode(&batchRequest) assert.NoError(t, err) assert.Equal(t, "download", batchRequest.Operation) @@ -267,7 +268,7 @@ func TestHTTPClientUpload(t *testing.T) { assert.Equal(t, MediaType, req.Header.Get("Accept")) var batchRequest BatchRequest - err := jsoniter.NewDecoder(req.Body).Decode(&batchRequest) + err := json.NewDecoder(req.Body).Decode(&batchRequest) assert.NoError(t, err) assert.Equal(t, "upload", batchRequest.Operation) diff --git a/modules/lfs/transferadapter.go b/modules/lfs/transferadapter.go index 8c40ab8c04..03f326f26c 100644 --- a/modules/lfs/transferadapter.go +++ b/modules/lfs/transferadapter.go @@ -12,9 +12,8 @@ import ( "io" "net/http" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" - - jsoniter "github.com/json-iterator/go" ) // TransferAdapter represents an adapter for downloading/uploading LFS objects @@ -65,7 +64,7 @@ func (a *BasicTransferAdapter) Upload(ctx context.Context, l *Link, p Pointer, r // Verify calls the verify handler on the LFS server func (a *BasicTransferAdapter) Verify(ctx context.Context, l *Link, p Pointer) error { - b, err := jsoniter.Marshal(p) + b, err := json.Marshal(p) if err != nil { log.Error("Error encoding json: %v", err) return err @@ -128,7 +127,7 @@ func handleErrorResponse(resp *http.Response) error { func decodeReponseError(r io.Reader) (ErrorResponse, error) { var er ErrorResponse - err := jsoniter.NewDecoder(r).Decode(&er) + err := json.NewDecoder(r).Decode(&er) if err != nil { log.Error("Error decoding json: %v", err) } diff --git a/modules/lfs/transferadapter_test.go b/modules/lfs/transferadapter_test.go index 7dfdad417e..9192b486ed 100644 --- a/modules/lfs/transferadapter_test.go +++ b/modules/lfs/transferadapter_test.go @@ -13,7 +13,8 @@ import ( "strings" "testing" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" + "github.com/stretchr/testify/assert" ) @@ -49,7 +50,7 @@ func TestBasicTransferAdapter(t *testing.T) { assert.Equal(t, MediaType, req.Header.Get("Content-Type")) var vp Pointer - err := jsoniter.NewDecoder(req.Body).Decode(&vp) + err := json.NewDecoder(req.Body).Decode(&vp) assert.NoError(t, err) assert.Equal(t, p.Oid, vp.Oid) assert.Equal(t, p.Size, vp.Size) @@ -60,7 +61,7 @@ func TestBasicTransferAdapter(t *testing.T) { Message: "Object not found", } payload := new(bytes.Buffer) - jsoniter.NewEncoder(payload).Encode(er) + json.NewEncoder(payload).Encode(er) return &http.Response{StatusCode: http.StatusNotFound, Body: ioutil.NopCloser(payload)} } else { diff --git a/modules/log/conn.go b/modules/log/conn.go index ad3d7f74e0..3e32c81d7f 100644 --- a/modules/log/conn.go +++ b/modules/log/conn.go @@ -10,7 +10,7 @@ import ( "io" "net" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) type connWriter struct { @@ -106,7 +106,6 @@ func NewConn() LoggerProvider { // Init inits connection writer with json config. // json config only need key "level". func (log *ConnLogger) Init(jsonconfig string) error { - json := jsoniter.ConfigCompatibleWithStandardLibrary err := json.Unmarshal([]byte(jsonconfig), log) if err != nil { return fmt.Errorf("Unable to parse JSON: %v", err) diff --git a/modules/log/console.go b/modules/log/console.go index 339b9ef3a4..7ecdd5c3b8 100644 --- a/modules/log/console.go +++ b/modules/log/console.go @@ -10,7 +10,7 @@ import ( "io" "os" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) // CanColorStdout reports if we can color the Stdout @@ -52,7 +52,6 @@ func NewConsoleLogger() LoggerProvider { // Init inits connection writer with json config. // json config only need key "level". func (log *ConsoleLogger) Init(config string) error { - json := jsoniter.ConfigCompatibleWithStandardLibrary err := json.Unmarshal([]byte(config), log) if err != nil { return fmt.Errorf("Unable to parse JSON: %v", err) diff --git a/modules/log/file.go b/modules/log/file.go index 79cbe740fd..bc9d741724 100644 --- a/modules/log/file.go +++ b/modules/log/file.go @@ -15,8 +15,8 @@ import ( "sync" "time" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/util" - jsoniter "github.com/json-iterator/go" ) // FileLogger implements LoggerProvider. @@ -101,7 +101,6 @@ func NewFileLogger() LoggerProvider { // "rotate":true // } func (log *FileLogger) Init(config string) error { - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal([]byte(config), log); err != nil { return fmt.Errorf("Unable to parse JSON: %v", err) } diff --git a/modules/log/level.go b/modules/log/level.go index 4b2d4ced41..5964ed8f8b 100644 --- a/modules/log/level.go +++ b/modules/log/level.go @@ -10,7 +10,7 @@ import ( "os" "strings" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) // Level is the level of the logger @@ -104,7 +104,6 @@ func FromString(level string) Level { // UnmarshalJSON takes text and turns it into a Level func (l *Level) UnmarshalJSON(b []byte) error { var tmp interface{} - json := jsoniter.ConfigCompatibleWithStandardLibrary err := json.Unmarshal(b, &tmp) if err != nil { fmt.Fprintf(os.Stderr, "Err: %v", err) diff --git a/modules/log/level_test.go b/modules/log/level_test.go index 2f37e407a1..47f77738c5 100644 --- a/modules/log/level_test.go +++ b/modules/log/level_test.go @@ -8,7 +8,8 @@ import ( "fmt" "testing" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" + "github.com/stretchr/testify/assert" ) @@ -17,7 +18,6 @@ type testLevel struct { } func TestLevelMarshalUnmarshalJSON(t *testing.T) { - json := jsoniter.ConfigCompatibleWithStandardLibrary levelBytes, err := json.Marshal(testLevel{ Level: INFO, }) diff --git a/modules/log/smtp.go b/modules/log/smtp.go index 3f27b2c658..60dced3706 100644 --- a/modules/log/smtp.go +++ b/modules/log/smtp.go @@ -10,7 +10,7 @@ import ( "net/smtp" "strings" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) type smtpWriter struct { @@ -57,7 +57,6 @@ func NewSMTPLogger() LoggerProvider { // "level":LevelError // } func (log *SMTPLogger) Init(jsonconfig string) error { - json := jsoniter.ConfigCompatibleWithStandardLibrary err := json.Unmarshal([]byte(jsonconfig), log) if err != nil { return fmt.Errorf("Unable to parse JSON: %v", err) diff --git a/modules/notification/action/action.go b/modules/notification/action/action.go index 776bee6a0c..772e7be137 100644 --- a/modules/notification/action/action.go +++ b/modules/notification/action/action.go @@ -10,10 +10,10 @@ import ( "strings" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification/base" "code.gitea.io/gitea/modules/repository" - jsoniter "github.com/json-iterator/go" ) type actionNotifier struct { @@ -296,7 +296,6 @@ func (*actionNotifier) NotifyPullRevieweDismiss(doer *models.User, review *model } func (a *actionNotifier) NotifyPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { - json := jsoniter.ConfigCompatibleWithStandardLibrary data, err := json.Marshal(commits) if err != nil { log.Error("Marshal: %v", err) @@ -368,7 +367,6 @@ func (a *actionNotifier) NotifyDeleteRef(doer *models.User, repo *models.Reposit } func (a *actionNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { - json := jsoniter.ConfigCompatibleWithStandardLibrary data, err := json.Marshal(commits) if err != nil { log.Error("json.Marshal: %v", err) diff --git a/modules/private/hook.go b/modules/private/hook.go index 79fae052dd..9596f5f4da 100644 --- a/modules/private/hook.go +++ b/modules/private/hook.go @@ -6,15 +6,14 @@ package private import ( "context" - "encoding/json" "fmt" "net/http" "net/url" "strconv" "time" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" ) // Git environment variables @@ -88,7 +87,6 @@ func HookPreReceive(ctx context.Context, ownerName, repoName string, opts HookOp ) req := newInternalRequest(ctx, reqURL, "POST") req = req.Header("Content-Type", "application/json") - json := jsoniter.ConfigCompatibleWithStandardLibrary jsonBytes, _ := json.Marshal(opts) req.Body(jsonBytes) req.SetTimeout(60*time.Second, time.Duration(60+len(opts.OldCommitIDs))*time.Second) @@ -115,7 +113,6 @@ func HookPostReceive(ctx context.Context, ownerName, repoName string, opts HookO req := newInternalRequest(ctx, reqURL, "POST") req = req.Header("Content-Type", "application/json") req.SetTimeout(60*time.Second, time.Duration(60+len(opts.OldCommitIDs))*time.Second) - json := jsoniter.ConfigCompatibleWithStandardLibrary jsonBytes, _ := json.Marshal(opts) req.Body(jsonBytes) resp, err := req.Response() diff --git a/modules/private/internal.go b/modules/private/internal.go index 672ac74970..f5b5db0ca1 100644 --- a/modules/private/internal.go +++ b/modules/private/internal.go @@ -12,8 +12,8 @@ import ( "net/http" "code.gitea.io/gitea/modules/httplib" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" ) func newRequest(ctx context.Context, url, method string) *httplib.Request { @@ -30,7 +30,6 @@ type Response struct { func decodeJSONError(resp *http.Response) *Response { var res Response - json := jsoniter.ConfigCompatibleWithStandardLibrary err := json.NewDecoder(resp.Body).Decode(&res) if err != nil { res.Err = err.Error() diff --git a/modules/private/mail.go b/modules/private/mail.go index 4a5a3eedd7..7e229396e3 100644 --- a/modules/private/mail.go +++ b/modules/private/mail.go @@ -10,8 +10,8 @@ import ( "io/ioutil" "net/http" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" ) // Email structure holds a data for sending general emails @@ -33,7 +33,6 @@ func SendEmail(ctx context.Context, subject, message string, to []string) (int, req := newInternalRequest(ctx, reqURL, "POST") req = req.Header("Content-Type", "application/json") - json := jsoniter.ConfigCompatibleWithStandardLibrary jsonBytes, _ := json.Marshal(Email{ Subject: subject, Message: message, diff --git a/modules/private/manager.go b/modules/private/manager.go index 0bcc3f8112..337b0dac64 100644 --- a/modules/private/manager.go +++ b/modules/private/manager.go @@ -11,8 +11,8 @@ import ( "net/url" "time" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" ) // Shutdown calls the internal shutdown function @@ -66,7 +66,6 @@ func FlushQueues(ctx context.Context, timeout time.Duration, nonBlocking bool) ( req.SetTimeout(timeout+10*time.Second, timeout+10*time.Second) } req = req.Header("Content-Type", "application/json") - json := jsoniter.ConfigCompatibleWithStandardLibrary jsonBytes, _ := json.Marshal(FlushOptions{ Timeout: timeout, NonBlocking: nonBlocking, @@ -153,7 +152,6 @@ func AddLogger(ctx context.Context, group, name, mode string, config map[string] req := newInternalRequest(ctx, reqURL, "POST") req = req.Header("Content-Type", "application/json") - json := jsoniter.ConfigCompatibleWithStandardLibrary jsonBytes, _ := json.Marshal(LoggerOptions{ Group: group, Name: name, diff --git a/modules/private/restore_repo.go b/modules/private/restore_repo.go index 66b60d8d12..feb2e1d141 100644 --- a/modules/private/restore_repo.go +++ b/modules/private/restore_repo.go @@ -11,8 +11,8 @@ import ( "net/http" "time" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" ) // RestoreParams structure holds a data for restore repository @@ -30,7 +30,6 @@ func RestoreRepo(ctx context.Context, repoDir, ownerName, repoName string, units req := newInternalRequest(ctx, reqURL, "POST") req.SetTimeout(3*time.Second, 0) // since the request will spend much time, don't timeout req = req.Header("Content-Type", "application/json") - json := jsoniter.ConfigCompatibleWithStandardLibrary jsonBytes, _ := json.Marshal(RestoreParams{ RepoDir: repoDir, OwnerName: ownerName, diff --git a/modules/private/serv.go b/modules/private/serv.go index 9643dad679..c378dc6dc7 100644 --- a/modules/private/serv.go +++ b/modules/private/serv.go @@ -11,8 +11,8 @@ import ( "net/url" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" ) // KeyAndOwner is the response from ServNoCommand @@ -35,7 +35,6 @@ func ServNoCommand(ctx context.Context, keyID int64) (*models.PublicKey, *models } var keyAndOwner KeyAndOwner - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.NewDecoder(resp.Body).Decode(&keyAndOwner); err != nil { return nil, nil, err } @@ -91,7 +90,7 @@ func ServCommand(ctx context.Context, keyID int64, ownerName, repoName string, m return nil, err } defer resp.Body.Close() - json := jsoniter.ConfigCompatibleWithStandardLibrary + if resp.StatusCode != http.StatusOK { var errServCommand ErrServCommand if err := json.NewDecoder(resp.Body).Decode(&errServCommand); err != nil { diff --git a/modules/queue/helper.go b/modules/queue/helper.go index e0368bce3a..f1aba411a8 100644 --- a/modules/queue/helper.go +++ b/modules/queue/helper.go @@ -7,7 +7,7 @@ package queue import ( "reflect" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) // Mappable represents an interface that can MapTo another interface @@ -20,8 +20,6 @@ type Mappable interface { // It will tolerate the cfg being passed as a []byte or string of a json representation of the // exemplar or the correct type of the exemplar itself func toConfig(exemplar, cfg interface{}) (interface{}, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary - // First of all check if we've got the same type as the exemplar - if so it's all fine. if reflect.TypeOf(cfg).AssignableTo(reflect.TypeOf(exemplar)) { return cfg, nil @@ -48,7 +46,6 @@ func toConfig(exemplar, cfg interface{}) (interface{}, error) { if !ok { // hmm ... can we marshal it to json? var err error - configBytes, err = json.Marshal(cfg) ok = err == nil } @@ -68,7 +65,6 @@ func toConfig(exemplar, cfg interface{}) (interface{}, error) { // unmarshalAs will attempt to unmarshal provided bytes as the provided exemplar func unmarshalAs(bs []byte, exemplar interface{}) (data Data, err error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary if exemplar != nil { t := reflect.TypeOf(exemplar) n := reflect.New(t) @@ -78,7 +74,6 @@ func unmarshalAs(bs []byte, exemplar interface{}) (data Data, err error) { } else { err = json.Unmarshal(bs, &data) } - return } diff --git a/modules/queue/manager.go b/modules/queue/manager.go index a6d48575ab..a88933191a 100644 --- a/modules/queue/manager.go +++ b/modules/queue/manager.go @@ -12,8 +12,8 @@ import ( "sync" "time" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" - jsoniter "github.com/json-iterator/go" ) var manager *Manager @@ -110,7 +110,6 @@ func (m *Manager) Add(managed interface{}, configuration, exemplar interface{}) int64 { - json := jsoniter.ConfigCompatibleWithStandardLibrary cfg, _ := json.Marshal(configuration) mq := &ManagedQueue{ Type: t, diff --git a/modules/queue/queue_bytefifo.go b/modules/queue/queue_bytefifo.go index 3ea61aad0e..edde47a62d 100644 --- a/modules/queue/queue_bytefifo.go +++ b/modules/queue/queue_bytefifo.go @@ -10,8 +10,8 @@ import ( "sync" "time" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" - jsoniter "github.com/json-iterator/go" ) // ByteFIFOQueueConfiguration is the configuration for a ByteFIFOQueue @@ -83,7 +83,6 @@ func (q *ByteFIFOQueue) PushFunc(data Data, fn func() error) error { if !assignableTo(data, q.exemplar) { return fmt.Errorf("Unable to assign data: %v to same type as exemplar: %v in %s", data, q.exemplar, q.name) } - json := jsoniter.ConfigCompatibleWithStandardLibrary bs, err := json.Marshal(data) if err != nil { return err @@ -309,7 +308,6 @@ func (q *ByteFIFOUniqueQueue) Has(data Data) (bool, error) { if !assignableTo(data, q.exemplar) { return false, fmt.Errorf("Unable to assign data: %v to same type as exemplar: %v in %s", data, q.exemplar, q.name) } - json := jsoniter.ConfigCompatibleWithStandardLibrary bs, err := json.Marshal(data) if err != nil { return false, err diff --git a/modules/queue/queue_test.go b/modules/queue/queue_test.go index 89ce23ac4c..65fb816537 100644 --- a/modules/queue/queue_test.go +++ b/modules/queue/queue_test.go @@ -7,7 +7,8 @@ package queue import ( "testing" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" + "github.com/stretchr/testify/assert" ) @@ -29,8 +30,6 @@ func TestToConfig(t *testing.T) { assert.True(t, ok) assert.NotEqual(t, cfg2, exemplar) assert.Equal(t, &cfg, &cfg2) - - json := jsoniter.ConfigCompatibleWithStandardLibrary cfgString, err := json.Marshal(cfg) assert.NoError(t, err) diff --git a/modules/queue/setting.go b/modules/queue/setting.go index 9b2c31b783..0e6ff3a0a8 100644 --- a/modules/queue/setting.go +++ b/modules/queue/setting.go @@ -8,9 +8,9 @@ import ( "fmt" "strings" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" ) func validType(t string) (Type, error) { @@ -27,8 +27,6 @@ func validType(t string) (Type, error) { func getQueueSettings(name string) (setting.QueueSettings, []byte) { q := setting.GetQueueSettings(name) - - json := jsoniter.ConfigCompatibleWithStandardLibrary cfg, err := json.Marshal(q) if err != nil { log.Error("Unable to marshall generic options: %v Error: %v", q, err) diff --git a/modules/recaptcha/recaptcha.go b/modules/recaptcha/recaptcha.go index f5f4c7ef9c..e0f5ee2e07 100644 --- a/modules/recaptcha/recaptcha.go +++ b/modules/recaptcha/recaptcha.go @@ -12,9 +12,9 @@ import ( "net/url" "strings" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" - jsoniter "github.com/json-iterator/go" ) // Response is the structure of JSON returned from API @@ -50,8 +50,8 @@ func Verify(ctx context.Context, response string) (bool, error) { if err != nil { return false, fmt.Errorf("Failed to read CAPTCHA response: %s", err) } + var jsonResponse Response - json := jsoniter.ConfigCompatibleWithStandardLibrary err = json.Unmarshal(body, &jsonResponse) if err != nil { return false, fmt.Errorf("Failed to parse CAPTCHA response: %s", err) diff --git a/modules/session/virtual.go b/modules/session/virtual.go index 8f5c4a7e89..928dd49740 100644 --- a/modules/session/virtual.go +++ b/modules/session/virtual.go @@ -8,12 +8,13 @@ import ( "fmt" "sync" + "code.gitea.io/gitea/modules/json" + "gitea.com/go-chi/session" couchbase "gitea.com/go-chi/session/couchbase" memcache "gitea.com/go-chi/session/memcache" mysql "gitea.com/go-chi/session/mysql" postgres "gitea.com/go-chi/session/postgres" - jsoniter "github.com/json-iterator/go" ) // VirtualSessionProvider represents a shadowed session provider implementation. @@ -25,7 +26,6 @@ type VirtualSessionProvider struct { // Init initializes the cookie session provider with given root path. func (o *VirtualSessionProvider) Init(gclifetime int64, config string) error { var opts session.Options - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal([]byte(config), &opts); err != nil { return err } diff --git a/modules/setting/log.go b/modules/setting/log.go index 0fb108c93d..ecf0711484 100644 --- a/modules/setting/log.go +++ b/modules/setting/log.go @@ -13,14 +13,13 @@ import ( "strings" "sync" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" - jsoniter "github.com/json-iterator/go" ini "gopkg.in/ini.v1" ) var filenameSuffix = "" - var descriptionLock = sync.RWMutex{} var logDescriptions = make(map[string]*LogDescription) @@ -203,8 +202,6 @@ func generateLogConfig(sec *ini.Section, name string, defaults defaultLogOptions } logConfig["colorize"] = sec.Key("COLORIZE").MustBool(false) - - json := jsoniter.ConfigCompatibleWithStandardLibrary byteConfig, err := json.Marshal(logConfig) if err != nil { log.Error("Failed to marshal log configuration: %v %v", logConfig, err) diff --git a/modules/setting/session.go b/modules/setting/session.go index bce73b51da..9e6193e826 100644 --- a/modules/setting/session.go +++ b/modules/setting/session.go @@ -10,8 +10,8 @@ import ( "path/filepath" "strings" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" - jsoniter "github.com/json-iterator/go" ) var ( @@ -65,8 +65,6 @@ func newSessionService() { default: SessionConfig.SameSite = http.SameSiteLaxMode } - - json := jsoniter.ConfigCompatibleWithStandardLibrary shadowConfig, err := json.Marshal(SessionConfig) if err != nil { log.Fatal("Can't shadow session config: %v", err) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index e3da5796e4..593677344f 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -24,11 +24,11 @@ import ( "time" "code.gitea.io/gitea/modules/generate" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/user" "code.gitea.io/gitea/modules/util" - jsoniter "github.com/json-iterator/go" shellquote "github.com/kballard/go-shellquote" "github.com/unknwon/com" gossh "golang.org/x/crypto/ssh" @@ -1116,7 +1116,6 @@ func MakeManifestData(appName string, appURL string, absoluteAssetURL string) [] Icons []manifestIcon `json:"icons"` } - json := jsoniter.ConfigCompatibleWithStandardLibrary bytes, err := json.Marshal(&manifestJSON{ Name: appName, ShortName: appName, diff --git a/modules/setting/setting_test.go b/modules/setting/setting_test.go index 9bc77ab0bb..0e7f5a55ab 100644 --- a/modules/setting/setting_test.go +++ b/modules/setting/setting_test.go @@ -7,7 +7,8 @@ package setting import ( "testing" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" + "github.com/stretchr/testify/assert" ) @@ -28,6 +29,5 @@ func TestMakeAbsoluteAssetURL(t *testing.T) { func TestMakeManifestData(t *testing.T) { jsonBytes := MakeManifestData(`Example App '\"`, "https://example.com", "https://example.com/foo/bar") - json := jsoniter.ConfigCompatibleWithStandardLibrary assert.True(t, json.Valid(jsonBytes)) } diff --git a/modules/storage/helper.go b/modules/storage/helper.go index 4d8ba8e6d9..1a25716ccb 100644 --- a/modules/storage/helper.go +++ b/modules/storage/helper.go @@ -7,7 +7,7 @@ package storage import ( "reflect" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) // Mappable represents an interface that can MapTo another interface @@ -20,8 +20,6 @@ type Mappable interface { // It will tolerate the cfg being passed as a []byte or string of a json representation of the // exemplar or the correct type of the exemplar itself func toConfig(exemplar, cfg interface{}) (interface{}, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary - // First of all check if we've got the same type as the exemplar - if so it's all fine. if reflect.TypeOf(cfg).AssignableTo(reflect.TypeOf(exemplar)) { return cfg, nil @@ -48,7 +46,6 @@ func toConfig(exemplar, cfg interface{}) (interface{}, error) { if !ok { // hmm ... can we marshal it to json? var err error - configBytes, err = json.Marshal(cfg) ok = err == nil } diff --git a/modules/structs/hook.go b/modules/structs/hook.go index ad86cb35a4..163fb5c94d 100644 --- a/modules/structs/hook.go +++ b/modules/structs/hook.go @@ -10,7 +10,7 @@ import ( "strings" "time" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) var ( @@ -132,14 +132,12 @@ type CreatePayload struct { // JSONPayload return payload information func (p *CreatePayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.MarshalIndent(p, "", " ") } // ParseCreateHook parses create event hook content. func ParseCreateHook(raw []byte) (*CreatePayload, error) { hook := new(CreatePayload) - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal(raw, hook); err != nil { return nil, err } @@ -183,7 +181,6 @@ type DeletePayload struct { // JSONPayload implements Payload func (p *DeletePayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.MarshalIndent(p, "", " ") } @@ -203,7 +200,6 @@ type ForkPayload struct { // JSONPayload implements Payload func (p *ForkPayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.MarshalIndent(p, "", " ") } @@ -230,7 +226,6 @@ type IssueCommentPayload struct { // JSONPayload implements Payload func (p *IssueCommentPayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.MarshalIndent(p, "", " ") } @@ -261,7 +256,6 @@ type ReleasePayload struct { // JSONPayload implements Payload func (p *ReleasePayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.MarshalIndent(p, "", " ") } @@ -287,14 +281,12 @@ type PushPayload struct { // JSONPayload FIXME func (p *PushPayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.MarshalIndent(p, "", " ") } // ParsePushHook parses push event hook content. func ParsePushHook(raw []byte) (*PushPayload, error) { hook := new(PushPayload) - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal(raw, hook); err != nil { return nil, err } @@ -362,7 +354,6 @@ type IssuePayload struct { // JSONPayload encodes the IssuePayload to JSON, with an indentation of two spaces. func (p *IssuePayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.MarshalIndent(p, "", " ") } @@ -398,7 +389,6 @@ type PullRequestPayload struct { // JSONPayload FIXME func (p *PullRequestPayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.MarshalIndent(p, "", " ") } @@ -435,6 +425,5 @@ type RepositoryPayload struct { // JSONPayload JSON representation of the payload func (p *RepositoryPayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.MarshalIndent(p, "", " ") } diff --git a/modules/structs/user.go b/modules/structs/user.go index a3c8f0c32a..431e230fac 100644 --- a/modules/structs/user.go +++ b/modules/structs/user.go @@ -7,7 +7,7 @@ package structs import ( "time" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) // User represents a user @@ -56,7 +56,6 @@ type User struct { func (u User) MarshalJSON() ([]byte, error) { // Re-declaring User to avoid recursion type shadow User - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.Marshal(struct { shadow CompatUserName string `json:"username"` diff --git a/modules/task/migrate.go b/modules/task/migrate.go index d7655112d3..481422403b 100644 --- a/modules/task/migrate.go +++ b/modules/task/migrate.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/graceful" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/migrations" migration "code.gitea.io/gitea/modules/migrations/base" @@ -20,7 +21,6 @@ import ( "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" - jsoniter "github.com/json-iterator/go" ) func handleCreateError(owner *models.User, err error) error { @@ -112,7 +112,6 @@ func runMigrateTask(t *models.Task) (err error) { Format: format, Args: args, } - json := jsoniter.ConfigCompatibleWithStandardLibrary bs, _ := json.Marshal(message) t.Message = string(bs) _ = t.UpdateCols("message") diff --git a/modules/task/task.go b/modules/task/task.go index 1c0a87e1f6..4e782869f9 100644 --- a/modules/task/task.go +++ b/modules/task/task.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/graceful" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/migrations/base" "code.gitea.io/gitea/modules/queue" @@ -18,7 +19,6 @@ import ( "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" - jsoniter "github.com/json-iterator/go" ) // taskQueue is a global queue of tasks @@ -85,8 +85,6 @@ func CreateMigrateTask(doer, u *models.User, opts base.MigrateOptions) (*models. return nil, err } opts.AuthToken = "" - - json := jsoniter.ConfigCompatibleWithStandardLibrary bs, err := json.Marshal(&opts) if err != nil { return nil, err diff --git a/modules/templates/helper.go b/modules/templates/helper.go index d4913f7c41..1e8f00b669 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -8,7 +8,6 @@ package templates import ( "bytes" "container/list" - "encoding/json" "errors" "fmt" "html" @@ -28,6 +27,7 @@ import ( "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/emoji" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/repository" @@ -38,7 +38,6 @@ import ( "code.gitea.io/gitea/services/gitdiff" "github.com/editorconfig/editorconfig-core-go/v2" - jsoniter "github.com/json-iterator/go" ) // Used from static.go && dynamic.go @@ -46,7 +45,6 @@ var mailSubjectSplit = regexp.MustCompile(`(?m)^-{3,}[\s]*$`) // NewFuncMap returns functions for injecting to templates func NewFuncMap() []template.FuncMap { - jsonED := jsoniter.ConfigCompatibleWithStandardLibrary return []template.FuncMap{map[string]interface{}{ "GoVer": func() string { return strings.Title(runtime.Version()) @@ -221,7 +219,7 @@ func NewFuncMap() []template.FuncMap { return fmt.Sprintf("%f", float64(adds)/(float64(adds)+float64(dels))*100) }, "Json": func(in interface{}) string { - out, err := jsonED.Marshal(in) + out, err := json.Marshal(in) if err != nil { return "" } @@ -847,7 +845,6 @@ func ActionContent2Commits(act Actioner) *repository.PushCommits { return push } - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal([]byte(act.GetContent()), push); err != nil { log.Error("json.Unmarshal:\n%s\nERROR: %v", act.GetContent(), err) } diff --git a/routers/api/v1/utils/hook.go b/routers/api/v1/utils/hook.go index 5f2be65a29..a257260cad 100644 --- a/routers/api/v1/utils/hook.go +++ b/routers/api/v1/utils/hook.go @@ -12,11 +12,11 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" + "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/routers/utils" "code.gitea.io/gitea/services/webhook" - jsoniter "github.com/json-iterator/go" ) // GetOrgHook get an organization's webhook. If there is an error, write to @@ -147,7 +147,6 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID return nil, false } - json := jsoniter.ConfigCompatibleWithStandardLibrary meta, err := json.Marshal(&webhook.SlackMeta{ Channel: strings.TrimSpace(channel), Username: form.Config["username"], @@ -222,7 +221,6 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *models.Webho if w.Type == models.SLACK { if channel, ok := form.Config["channel"]; ok { - json := jsoniter.ConfigCompatibleWithStandardLibrary meta, err := json.Marshal(&webhook.SlackMeta{ Channel: channel, Username: form.Config["username"], diff --git a/routers/private/mail.go b/routers/private/mail.go index d7bd5155f6..6b9100159a 100644 --- a/routers/private/mail.go +++ b/routers/private/mail.go @@ -11,11 +11,11 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/private" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/services/mailer" - jsoniter "github.com/json-iterator/go" ) // SendEmail pushes messages to mail queue @@ -32,7 +32,7 @@ func SendEmail(ctx *context.PrivateContext) { var mail private.Email rd := ctx.Req.Body defer rd.Close() - json := jsoniter.ConfigCompatibleWithStandardLibrary + if err := json.NewDecoder(rd).Decode(&mail); err != nil { log.Error("%v", err) ctx.JSON(http.StatusInternalServerError, private.Response{ diff --git a/routers/private/manager.go b/routers/private/manager.go index 7d010f3f81..2b4cfb8ef4 100644 --- a/routers/private/manager.go +++ b/routers/private/manager.go @@ -10,12 +10,12 @@ import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/graceful" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/private" "code.gitea.io/gitea/modules/queue" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/web" - jsoniter "github.com/json-iterator/go" ) // FlushQueues flushes all the Queues @@ -130,7 +130,6 @@ func AddLogger(ctx *context.PrivateContext) { } bufferLen := setting.Cfg.Section("log").Key("BUFFER_LEN").MustInt64(10000) - json := jsoniter.ConfigCompatibleWithStandardLibrary byteConfig, err := json.Marshal(opts.Config) if err != nil { log.Error("Failed to marshal log configuration: %v %v", opts.Config, err) diff --git a/routers/private/restore_repo.go b/routers/private/restore_repo.go index 36d17dd95c..87d60537cd 100644 --- a/routers/private/restore_repo.go +++ b/routers/private/restore_repo.go @@ -9,14 +9,13 @@ import ( "net/http" myCtx "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/migrations" "code.gitea.io/gitea/modules/private" - jsoniter "github.com/json-iterator/go" ) // RestoreRepo restore a repository from data func RestoreRepo(ctx *myCtx.PrivateContext) { - json := jsoniter.ConfigCompatibleWithStandardLibrary bs, err := ioutil.ReadAll(ctx.Req.Body) if err != nil { ctx.JSON(http.StatusInternalServerError, private.Response{ diff --git a/routers/web/admin/admin.go b/routers/web/admin/admin.go index c2d94ab9c9..125bef9b53 100644 --- a/routers/web/admin/admin.go +++ b/routers/web/admin/admin.go @@ -20,6 +20,7 @@ import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/cron" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/queue" @@ -28,7 +29,6 @@ import ( "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/services/forms" "code.gitea.io/gitea/services/mailer" - jsoniter "github.com/json-iterator/go" "gitea.com/go-chi/session" ) @@ -275,7 +275,6 @@ func Config(ctx *context.Context) { sessionCfg := setting.SessionConfig if sessionCfg.Provider == "VirtualSession" { var realSession session.Options - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal([]byte(sessionCfg.ProviderConfig), &realSession); err != nil { log.Error("Unable to unmarshall session config for virtualed provider config: %s\nError: %v", sessionCfg.ProviderConfig, err) } diff --git a/routers/web/events/events.go b/routers/web/events/events.go index f9cc274851..a630d9c224 100644 --- a/routers/web/events/events.go +++ b/routers/web/events/events.go @@ -13,10 +13,10 @@ import ( "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/eventsource" "code.gitea.io/gitea/modules/graceful" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/routers/web/user" - jsoniter "github.com/json-iterator/go" ) // Events listens for events @@ -103,7 +103,6 @@ loop: log.Error("Unable to APIFormat stopwatches: %v", err) continue } - json := jsoniter.ConfigCompatibleWithStandardLibrary dataBs, err := json.Marshal(apiSWs) if err != nil { log.Error("Unable to marshal stopwatches: %v", err) diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index 0f978c7b01..792258da84 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -16,6 +16,7 @@ import ( "code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/repofiles" repo_module "code.gitea.io/gitea/modules/repository" @@ -26,7 +27,6 @@ import ( "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/utils" "code.gitea.io/gitea/services/forms" - jsoniter "github.com/json-iterator/go" ) const ( @@ -165,7 +165,6 @@ func GetEditorConfig(ctx *context.Context, treePath string) string { if err == nil { def, err := ec.GetDefinitionForFilename(treePath) if err == nil { - json := jsoniter.ConfigCompatibleWithStandardLibrary jsonStr, _ := json.Marshal(def) return string(jsonStr) } diff --git a/routers/web/repo/webhook.go b/routers/web/repo/webhook.go index 30fa98f809..946801cb76 100644 --- a/routers/web/repo/webhook.go +++ b/routers/web/repo/webhook.go @@ -17,13 +17,13 @@ import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/services/forms" "code.gitea.io/gitea/services/webhook" - jsoniter "github.com/json-iterator/go" ) const ( @@ -306,7 +306,6 @@ func DiscordHooksNewPost(ctx *context.Context) { return } - json := jsoniter.ConfigCompatibleWithStandardLibrary meta, err := json.Marshal(&webhook.DiscordMeta{ Username: form.Username, IconURL: form.IconURL, @@ -402,7 +401,6 @@ func TelegramHooksNewPost(ctx *context.Context) { return } - json := jsoniter.ConfigCompatibleWithStandardLibrary meta, err := json.Marshal(&webhook.TelegramMeta{ BotToken: form.BotToken, ChatID: form.ChatID, @@ -455,7 +453,6 @@ func MatrixHooksNewPost(ctx *context.Context) { return } - json := jsoniter.ConfigCompatibleWithStandardLibrary meta, err := json.Marshal(&webhook.MatrixMeta{ HomeserverURL: form.HomeserverURL, Room: form.RoomID, @@ -560,7 +557,6 @@ func SlackHooksNewPost(ctx *context.Context) { return } - json := jsoniter.ConfigCompatibleWithStandardLibrary meta, err := json.Marshal(&webhook.SlackMeta{ Channel: strings.TrimSpace(form.Channel), Username: form.Username, @@ -848,7 +844,6 @@ func SlackHooksEditPost(ctx *context.Context) { return } - json := jsoniter.ConfigCompatibleWithStandardLibrary meta, err := json.Marshal(&webhook.SlackMeta{ Channel: strings.TrimSpace(form.Channel), Username: form.Username, @@ -894,7 +889,6 @@ func DiscordHooksEditPost(ctx *context.Context) { return } - json := jsoniter.ConfigCompatibleWithStandardLibrary meta, err := json.Marshal(&webhook.DiscordMeta{ Username: form.Username, IconURL: form.IconURL, @@ -970,7 +964,7 @@ func TelegramHooksEditPost(ctx *context.Context) { ctx.HTML(http.StatusOK, orCtx.NewTemplate) return } - json := jsoniter.ConfigCompatibleWithStandardLibrary + meta, err := json.Marshal(&webhook.TelegramMeta{ BotToken: form.BotToken, ChatID: form.ChatID, @@ -1012,7 +1006,7 @@ func MatrixHooksEditPost(ctx *context.Context) { ctx.HTML(http.StatusOK, orCtx.NewTemplate) return } - json := jsoniter.ConfigCompatibleWithStandardLibrary + meta, err := json.Marshal(&webhook.MatrixMeta{ HomeserverURL: form.HomeserverURL, Room: form.RoomID, diff --git a/routers/web/user/home.go b/routers/web/user/home.go index d3fc36c730..fcf5295c9e 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -18,6 +18,7 @@ import ( "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" issue_indexer "code.gitea.io/gitea/modules/indexer/issues" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" @@ -26,7 +27,6 @@ import ( issue_service "code.gitea.io/gitea/services/issue" pull_service "code.gitea.io/gitea/services/pull" - jsoniter "github.com/json-iterator/go" "github.com/keybase/go-crypto/openpgp" "github.com/keybase/go-crypto/openpgp/armor" "xorm.io/builder" @@ -703,7 +703,6 @@ func buildIssueOverview(ctx *context.Context, unitType models.UnitType) { } // Convert []int64 to string - json := jsoniter.ConfigCompatibleWithStandardLibrary reposParam, _ := json.Marshal(repoIDs) ctx.Data["ReposParam"] = string(reposParam) diff --git a/routers/web/user/oauth.go b/routers/web/user/oauth.go index 07fff8370c..18fc943483 100644 --- a/routers/web/user/oauth.go +++ b/routers/web/user/oauth.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" @@ -25,7 +26,6 @@ import ( "gitea.com/go-chi/binding" "github.com/golang-jwt/jwt" - jsoniter "github.com/json-iterator/go" ) const ( @@ -507,7 +507,7 @@ func OIDCKeys(ctx *context.Context) { } ctx.Resp.Header().Set("Content-Type", "application/json") - enc := jsoniter.NewEncoder(ctx.Resp) + enc := json.NewEncoder(ctx.Resp) if err := enc.Encode(jwks); err != nil { log.Error("Failed to encode representation as json. Error: %v", err) } diff --git a/routers/web/user/task.go b/routers/web/user/task.go index 8e7b66ef95..c71d435233 100644 --- a/routers/web/user/task.go +++ b/routers/web/user/task.go @@ -9,7 +9,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) // TaskStatus returns task's status @@ -26,7 +26,6 @@ func TaskStatus(ctx *context.Context) { if task.Message != "" && task.Message[0] == '{' { // assume message is actually a translatable string - json := jsoniter.ConfigCompatibleWithStandardLibrary var translatableMessage models.TranslatableMessage if err := json.Unmarshal([]byte(message), &translatableMessage); err != nil { translatableMessage = models.TranslatableMessage{ diff --git a/services/auth/source/ldap/source.go b/services/auth/source/ldap/source.go index 87be0117ee..79f118f784 100644 --- a/services/auth/source/ldap/source.go +++ b/services/auth/source/ldap/source.go @@ -8,10 +8,9 @@ import ( "strings" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/secret" "code.gitea.io/gitea/modules/setting" - - jsoniter "github.com/json-iterator/go" ) // .____ ________ _____ __________ @@ -79,7 +78,6 @@ func (source *Source) ToDB() ([]byte, error) { return nil, err } source.BindPassword = "" - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.Marshal(source) } diff --git a/services/auth/source/oauth2/source.go b/services/auth/source/oauth2/source.go index e9c49ef90b..40d8973b4b 100644 --- a/services/auth/source/oauth2/source.go +++ b/services/auth/source/oauth2/source.go @@ -6,8 +6,7 @@ package oauth2 import ( "code.gitea.io/gitea/models" - - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) // ________ _____ __ .__ ________ @@ -37,7 +36,6 @@ func (source *Source) FromDB(bs []byte) error { // ToDB exports an SMTPConfig to a serialized format. func (source *Source) ToDB() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.Marshal(source) } diff --git a/services/auth/source/pam/source.go b/services/auth/source/pam/source.go index b717ee6fe8..75aa99e45f 100644 --- a/services/auth/source/pam/source.go +++ b/services/auth/source/pam/source.go @@ -6,8 +6,7 @@ package pam import ( "code.gitea.io/gitea/models" - - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) // __________ _____ _____ @@ -33,7 +32,6 @@ func (source *Source) FromDB(bs []byte) error { // ToDB exports a PAMConfig to a serialized format. func (source *Source) ToDB() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.Marshal(source) } diff --git a/services/auth/source/smtp/source.go b/services/auth/source/smtp/source.go index 0f948d5381..3d80aea68a 100644 --- a/services/auth/source/smtp/source.go +++ b/services/auth/source/smtp/source.go @@ -6,8 +6,7 @@ package smtp import ( "code.gitea.io/gitea/models" - - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) // _________ __________________________ @@ -37,7 +36,6 @@ func (source *Source) FromDB(bs []byte) error { // ToDB exports an SMTPConfig to a serialized format. func (source *Source) ToDB() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.Marshal(source) } diff --git a/services/auth/source/sspi/source.go b/services/auth/source/sspi/source.go index e4be446f30..58cb10de1d 100644 --- a/services/auth/source/sspi/source.go +++ b/services/auth/source/sspi/source.go @@ -6,7 +6,7 @@ package sspi import ( "code.gitea.io/gitea/models" - jsoniter "github.com/json-iterator/go" + "code.gitea.io/gitea/modules/json" ) // _________ ___________________.___ @@ -32,7 +32,6 @@ func (cfg *Source) FromDB(bs []byte) error { // ToDB exports an SSPIConfig to a serialized format. func (cfg *Source) ToDB() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary return json.Marshal(cfg) } diff --git a/services/gitdiff/gitdiff_test.go b/services/gitdiff/gitdiff_test.go index 2386552efe..94ed1bc407 100644 --- a/services/gitdiff/gitdiff_test.go +++ b/services/gitdiff/gitdiff_test.go @@ -14,8 +14,9 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/highlight" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" - jsoniter "github.com/json-iterator/go" + dmp "github.com/sergi/go-diff/diffmatchpatch" "github.com/stretchr/testify/assert" "gopkg.in/ini.v1" @@ -297,7 +298,7 @@ index 6961180..9ba1a00 100644 t.Errorf("ParsePatch(%q) error = %v, wantErr %v", testcase.name, err, testcase.wantErr) return } - json := jsoniter.ConfigCompatibleWithStandardLibrary + gotMarshaled, _ := json.MarshalIndent(got, "", " ") if got.NumFiles != 1 { t.Errorf("ParsePath(%q) did not receive 1 file:\n%s", testcase.name, string(gotMarshaled)) diff --git a/services/lfs/locks.go b/services/lfs/locks.go index 20ba12e65b..641c1c595b 100644 --- a/services/lfs/locks.go +++ b/services/lfs/locks.go @@ -12,11 +12,11 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" + "code.gitea.io/gitea/modules/json" lfs_module "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" ) func handleLockListOut(ctx *context.Context, repo *models.Repository, lock *models.LFSLock, err error) { @@ -159,7 +159,7 @@ func PostLockHandler(ctx *context.Context) { var req api.LFSLockRequest bodyReader := ctx.Req.Body defer bodyReader.Close() - json := jsoniter.ConfigCompatibleWithStandardLibrary + dec := json.NewDecoder(bodyReader) if err := dec.Decode(&req); err != nil { log.Warn("Failed to decode lock request as json. Error: %v", err) @@ -293,7 +293,7 @@ func UnLockHandler(ctx *context.Context) { var req api.LFSLockDeleteRequest bodyReader := ctx.Req.Body defer bodyReader.Close() - json := jsoniter.ConfigCompatibleWithStandardLibrary + dec := json.NewDecoder(bodyReader) if err := dec.Decode(&req); err != nil { log.Warn("Failed to decode lock request as json. Error: %v", err) diff --git a/services/lfs/server.go b/services/lfs/server.go index 6b79a3a364..0d357939d5 100644 --- a/services/lfs/server.go +++ b/services/lfs/server.go @@ -17,12 +17,12 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/json" lfs_module "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "github.com/golang-jwt/jwt" - jsoniter "github.com/json-iterator/go" ) // requestContext contain variables from the HTTP request. @@ -243,7 +243,7 @@ func BatchHandler(ctx *context.Context) { ctx.Resp.Header().Set("Content-Type", lfs_module.MediaType) - enc := jsoniter.NewEncoder(ctx.Resp) + enc := json.NewEncoder(ctx.Resp) if err := enc.Encode(respobj); err != nil { log.Error("Failed to encode representation as json. Error: %v", err) } @@ -336,7 +336,7 @@ func VerifyHandler(ctx *context.Context) { func decodeJSON(req *http.Request, v interface{}) error { defer req.Body.Close() - dec := jsoniter.NewDecoder(req.Body) + dec := json.NewDecoder(req.Body) return dec.Decode(v) } @@ -429,7 +429,7 @@ func writeStatusMessage(ctx *context.Context, status int, message string) { er := lfs_module.ErrorResponse{Message: message} - enc := jsoniter.NewEncoder(ctx.Resp) + enc := json.NewEncoder(ctx.Resp) if err := enc.Encode(er); err != nil { log.Error("Failed to encode error response as json. Error: %v", err) } diff --git a/services/pull/pull.go b/services/pull/pull.go index 6b3acd2004..6c108c224f 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -16,11 +16,11 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/graceful" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/setting" issue_service "code.gitea.io/gitea/services/issue" - jsoniter "github.com/json-iterator/go" ) // NewPullRequest creates new pull request with labels for repository. @@ -86,7 +86,6 @@ func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int6 data.CommitIDs = append(data.CommitIDs, e.Value.(*git.Commit).ID.String()) } - json := jsoniter.ConfigCompatibleWithStandardLibrary dataJSON, err := json.Marshal(data) if err != nil { return err diff --git a/services/webhook/dingtalk.go b/services/webhook/dingtalk.go index 49e161ea57..3296b1114a 100644 --- a/services/webhook/dingtalk.go +++ b/services/webhook/dingtalk.go @@ -10,9 +10,9 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" dingtalk "github.com/lunny/dingtalk_webhook" ) @@ -27,7 +27,6 @@ var ( // JSONPayload Marshals the DingtalkPayload to json func (d *DingtalkPayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary data, err := json.MarshalIndent(d, "", " ") if err != nil { return []byte{}, err diff --git a/services/webhook/discord.go b/services/webhook/discord.go index ea3879f198..9a8f8a27a5 100644 --- a/services/webhook/discord.go +++ b/services/webhook/discord.go @@ -12,10 +12,10 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" ) type ( @@ -68,7 +68,6 @@ type ( // GetDiscordHook returns discord metadata func GetDiscordHook(w *models.Webhook) *DiscordMeta { s := &DiscordMeta{} - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal([]byte(w.Meta), s); err != nil { log.Error("webhook.GetDiscordHook(%d): %v", w.ID, err) } @@ -99,7 +98,6 @@ var ( // JSONPayload Marshals the DiscordPayload to json func (d *DiscordPayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary data, err := json.MarshalIndent(d, "", " ") if err != nil { return []byte{}, err @@ -250,7 +248,6 @@ func GetDiscordPayload(p api.Payloader, event models.HookEventType, meta string) s := new(DiscordPayload) discord := &DiscordMeta{} - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal([]byte(meta), &discord); err != nil { return s, errors.New("GetDiscordPayload meta json:" + err.Error()) } diff --git a/services/webhook/feishu.go b/services/webhook/feishu.go index b280e67759..4428d54f92 100644 --- a/services/webhook/feishu.go +++ b/services/webhook/feishu.go @@ -10,8 +10,8 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" ) type ( @@ -37,7 +37,6 @@ func newFeishuTextPayload(text string) *FeishuPayload { // JSONPayload Marshals the FeishuPayload to json func (f *FeishuPayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary data, err := json.MarshalIndent(f, "", " ") if err != nil { return []byte{}, err diff --git a/services/webhook/matrix.go b/services/webhook/matrix.go index 6fca67ca84..10e0c8fd15 100644 --- a/services/webhook/matrix.go +++ b/services/webhook/matrix.go @@ -15,10 +15,10 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" ) const matrixPayloadSizeLimit = 1024 * 64 @@ -39,7 +39,6 @@ var messageTypeText = map[int]string{ // GetMatrixHook returns Matrix metadata func GetMatrixHook(w *models.Webhook) *MatrixMeta { s := &MatrixMeta{} - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal([]byte(w.Meta), s); err != nil { log.Error("webhook.GetMatrixHook(%d): %v", w.ID, err) } @@ -78,7 +77,6 @@ type MatrixPayloadSafe struct { // JSONPayload Marshals the MatrixPayloadUnsafe to json func (m *MatrixPayloadUnsafe) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary data, err := json.MarshalIndent(m, "", " ") if err != nil { return []byte{}, err @@ -228,7 +226,6 @@ func GetMatrixPayload(p api.Payloader, event models.HookEventType, meta string) s := new(MatrixPayloadUnsafe) matrix := &MatrixMeta{} - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal([]byte(meta), &matrix); err != nil { return s, errors.New("GetMatrixPayload meta json:" + err.Error()) } @@ -262,7 +259,6 @@ func getMessageBody(htmlText string) string { // The access_token is removed from t.PayloadContent func getMatrixHookRequest(w *models.Webhook, t *models.HookTask) (*http.Request, error) { payloadunsafe := MatrixPayloadUnsafe{} - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal([]byte(t.PayloadContent), &payloadunsafe); err != nil { log.Error("Matrix Hook delivery failed: %v", err) return nil, err diff --git a/services/webhook/msteams.go b/services/webhook/msteams.go index 035dbc1c4c..ed5e659058 100644 --- a/services/webhook/msteams.go +++ b/services/webhook/msteams.go @@ -10,8 +10,8 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" ) type ( @@ -57,7 +57,6 @@ type ( // JSONPayload Marshals the MSTeamsPayload to json func (m *MSTeamsPayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary data, err := json.MarshalIndent(m, "", " ") if err != nil { return []byte{}, err diff --git a/services/webhook/slack.go b/services/webhook/slack.go index f522ca35f2..803b5de0b2 100644 --- a/services/webhook/slack.go +++ b/services/webhook/slack.go @@ -11,10 +11,10 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" ) // SlackMeta contains the slack metadata @@ -28,7 +28,6 @@ type SlackMeta struct { // GetSlackHook returns slack metadata func GetSlackHook(w *models.Webhook) *SlackMeta { s := &SlackMeta{} - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal([]byte(w.Meta), s); err != nil { log.Error("webhook.GetSlackHook(%d): %v", w.ID, err) } @@ -58,7 +57,6 @@ type SlackAttachment struct { // JSONPayload Marshals the SlackPayload to json func (s *SlackPayload) JSONPayload() ([]byte, error) { - json := jsoniter.ConfigCompatibleWithStandardLibrary data, err := json.MarshalIndent(s, "", " ") if err != nil { return []byte{}, err @@ -279,7 +277,6 @@ func GetSlackPayload(p api.Payloader, event models.HookEventType, meta string) ( s := new(SlackPayload) slack := &SlackMeta{} - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal([]byte(meta), &slack); err != nil { return s, errors.New("GetSlackPayload meta json:" + err.Error()) } diff --git a/services/webhook/telegram.go b/services/webhook/telegram.go index 4c4230759d..c5a26f687d 100644 --- a/services/webhook/telegram.go +++ b/services/webhook/telegram.go @@ -10,10 +10,10 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" api "code.gitea.io/gitea/modules/structs" - jsoniter "github.com/json-iterator/go" ) type ( @@ -34,7 +34,6 @@ type ( // GetTelegramHook returns telegram metadata func GetTelegramHook(w *models.Webhook) *TelegramMeta { s := &TelegramMeta{} - json := jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal([]byte(w.Meta), s); err != nil { log.Error("webhook.GetTelegramHook(%d): %v", w.ID, err) } @@ -50,7 +49,6 @@ func (t *TelegramPayload) JSONPayload() ([]byte, error) { t.ParseMode = "HTML" t.DisableWebPreview = true t.Message = markup.Sanitize(t.Message) - json := jsoniter.ConfigCompatibleWithStandardLibrary data, err := json.MarshalIndent(t, "", " ") if err != nil { return []byte{}, err diff --git a/services/webhook/wechatwork.go b/services/webhook/wechatwork.go index 1e2d5f4a10..08c77ff2a5 100644 --- a/services/webhook/wechatwork.go +++ b/services/webhook/wechatwork.go @@ -5,12 +5,12 @@ package webhook import ( - "encoding/json" "fmt" "strings" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" )