Browse Source

Migrate to use jsoniter instead of encoding/json (#14841)

* Migrate to use jsoniter

* fix tests

* update gitea.com/go-chi/binding

Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: 6543 <6543@obermui.de>
mj-v1.14.3
zeripath 1 year ago
committed by GitHub
parent
commit
f0e15250b9
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      build/generate-emoji.go
  2. 3
      cmd/dump.go
  3. 3
      cmd/serv.go
  4. 3
      go.mod
  5. 4
      go.sum
  6. 3
      integrations/api_admin_test.go
  7. 4
      integrations/api_helper_for_declarative_test.go
  8. 4
      integrations/create_no_session_test.go
  9. 6
      integrations/integration_test.go
  10. 10
      integrations/oauth_test.go
  11. 3
      integrations/repo_commits_test.go
  12. 3
      integrations/testlogger.go
  13. 5
      models/issue_comment.go
  14. 12
      models/login_source.go
  15. 4
      models/migrations/v130.go
  16. 12
      models/repo_unit.go
  17. 4
      models/task.go
  18. 3
      models/user_heatmap_test.go
  19. 7
      models/webhook.go
  20. 3
      models/webhook_test.go
  21. 3
      modules/context/context.go
  22. 4
      modules/eventsource/event.go
  23. 4
      modules/httplib/httplib.go
  24. 3
      modules/indexer/code/elastic_search.go
  25. 4
      modules/lfs/locks.go
  26. 7
      modules/lfs/server.go
  27. 7
      modules/log/conn.go
  28. 3
      modules/log/conn_test.go
  29. 7
      modules/log/console.go
  30. 3
      modules/log/console_test.go
  31. 5
      modules/log/file.go
  32. 4
      modules/log/level.go
  33. 3
      modules/log/level_test.go
  34. 7
      modules/log/smtp.go
  35. 4
      modules/notification/action/action.go
  36. 4
      modules/private/hook.go
  37. 3
      modules/private/internal.go
  38. 3
      modules/private/mail.go
  39. 4
      modules/private/manager.go
  40. 4
      modules/private/serv.go
  41. 5
      modules/queue/helper.go
  42. 3
      modules/queue/manager.go
  43. 4
      modules/queue/queue_bytefifo.go
  44. 3
      modules/queue/queue_test.go
  45. 3
      modules/queue/setting.go
  46. 3
      modules/recaptcha/recaptcha.go
  47. 3
      modules/session/virtual.go
  48. 3
      modules/setting/log.go
  49. 3
      modules/setting/session.go
  50. 3
      modules/setting/setting.go
  51. 3
      modules/setting/setting_test.go
  52. 4
      modules/storage/helper.go
  53. 14
      modules/structs/hook.go
  54. 4
      modules/structs/user.go
  55. 3
      modules/task/task.go
  56. 6
      modules/templates/helper.go
  57. 3
      routers/admin/admin.go
  58. 4
      routers/api/v1/utils/hook.go
  59. 3
      routers/events/events.go
  60. 3
      routers/private/mail.go
  61. 3
      routers/private/manager.go
  62. 3
      routers/repo/editor.go
  63. 10
      routers/repo/webhook.go
  64. 3
      routers/user/home.go
  65. 5
      services/gitdiff/gitdiff_test.go
  66. 3
      services/pull/pull.go
  67. 3
      services/webhook/dingtalk.go
  68. 5
      services/webhook/discord.go
  69. 3
      services/webhook/feishu.go
  70. 6
      services/webhook/matrix.go
  71. 3
      services/webhook/msteams.go
  72. 5
      services/webhook/slack.go
  73. 4
      services/webhook/telegram.go
  74. 4
      vendor/gitea.com/go-chi/binding/binding.go
  75. 1
      vendor/gitea.com/go-chi/binding/go.mod
  76. 15
      vendor/gitea.com/go-chi/binding/go.sum
  77. 3
      vendor/modules.txt

5
build/generate-emoji.go

@ -8,7 +8,6 @@
package main
import (
"encoding/json"
"flag"
"fmt"
"go/format"
@ -20,6 +19,8 @@ import (
"strconv"
"strings"
"unicode/utf8"
jsoniter "github.com/json-iterator/go"
)
const (
@ -50,6 +51,7 @@ func (e Emoji) MarshalJSON() ([]byte, error) {
x.UnicodeVersion = ""
x.Description = ""
x.SkinTones = false
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(x)
}
@ -101,6 +103,7 @@ func generate() ([]byte, error) {
// unmarshal
var data Gemoji
json := jsoniter.ConfigCompatibleWithStandardLibrary
err = json.Unmarshal(body, &data)
if err != nil {
return nil, err

3
cmd/dump.go

@ -6,7 +6,6 @@
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
@ -22,6 +21,7 @@ import (
"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"
)
@ -296,6 +296,7 @@ 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
}

3
cmd/serv.go

@ -6,7 +6,6 @@
package cmd
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
@ -25,6 +24,7 @@ import (
"code.gitea.io/gitea/modules/setting"
"github.com/dgrijalva/jwt-go"
jsoniter "github.com/json-iterator/go"
"github.com/kballard/go-shellquote"
"github.com/urfave/cli"
)
@ -255,6 +255,7 @@ 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 {

3
go.mod

@ -6,7 +6,7 @@ require (
cloud.google.com/go v0.78.0 // indirect
code.gitea.io/gitea-vet v0.2.1
code.gitea.io/sdk/gitea v0.13.2
gitea.com/go-chi/binding v0.0.0-20210113025129-03f1d313373c
gitea.com/go-chi/binding v0.0.0-20210301195521-1fe1c9a555e7
gitea.com/go-chi/cache v0.0.0-20210110083709-82c4c9ce2d5e
gitea.com/go-chi/captcha v0.0.0-20210110083842-e7696c336a1e
gitea.com/go-chi/session v0.0.0-20210108030337-0cb48c5ba8ee
@ -66,6 +66,7 @@ require (
github.com/issue9/assert v1.3.2 // indirect
github.com/issue9/identicon v1.0.1
github.com/jaytaylor/html2text v0.0.0-20200412013138-3577fbdbcff7
github.com/json-iterator/go v1.1.10
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/keybase/go-crypto v0.0.0-20200123153347-de78d2cb44f4

4
go.sum

@ -41,8 +41,8 @@ code.gitea.io/gitea-vet v0.2.1/go.mod h1:zcNbT/aJEmivCAhfmkHOlT645KNOf9W2KnkLgFj
code.gitea.io/sdk/gitea v0.13.2 h1:wAnT/J7Z62q3fJXbgnecoaOBh8CM1Qq0/DakWxiv4yA=
code.gitea.io/sdk/gitea v0.13.2/go.mod h1:lee2y8LeV3kQb2iK+hHlMqoadL4bp27QOkOV/hawLKg=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
gitea.com/go-chi/binding v0.0.0-20210113025129-03f1d313373c h1:NTtrGYjR40WUdkCdn26Y5LGFT52rIkFPkjmtgCAyiTs=
gitea.com/go-chi/binding v0.0.0-20210113025129-03f1d313373c/go.mod h1:9bGA9dIsrz+wVQKH1DzvxuAvrudHaQ8Wx8hLme/GVGQ=
gitea.com/go-chi/binding v0.0.0-20210301195521-1fe1c9a555e7 h1:xCVJPY823C8RWpgMabTw2kOglDrg0iS3GcQU6wdwHkU=
gitea.com/go-chi/binding v0.0.0-20210301195521-1fe1c9a555e7/go.mod h1:AyfTrwtfYN54R/HmVvMYPnSTenH5bVoyh8x6tBluxEA=
gitea.com/go-chi/cache v0.0.0-20210110083709-82c4c9ce2d5e h1:zgPGaf3kXP0cVm9J0l8ZA2+XDzILYATg0CXbihR6N+o=
gitea.com/go-chi/cache v0.0.0-20210110083709-82c4c9ce2d5e/go.mod h1:k2V/gPDEtXGjjMGuBJiapffAXTv76H4snSmlJRLUhH0=
gitea.com/go-chi/captcha v0.0.0-20210110083842-e7696c336a1e h1:YjaQU6XFicdhPN+MlGolcXO8seYY2+EY5g7vZPB17CQ=

3
integrations/api_admin_test.go

@ -5,7 +5,6 @@
package integrations
import (
"encoding/json"
"fmt"
"net/http"
"testing"
@ -13,6 +12,7 @@ import (
"code.gitea.io/gitea/models"
api "code.gitea.io/gitea/modules/structs"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
@ -190,6 +190,7 @@ 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))

4
integrations/api_helper_for_declarative_test.go

@ -6,7 +6,6 @@ package integrations
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
@ -18,6 +17,7 @@ import (
"code.gitea.io/gitea/modules/queue"
api "code.gitea.io/gitea/modules/structs"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
@ -212,6 +212,8 @@ func doAPICreatePullRequest(ctx APITestContext, owner, repo, baseBranch, headBra
expected = ctx.ExpectedCode
}
resp := ctx.Session.MakeRequest(t, req, expected)
json := jsoniter.ConfigCompatibleWithStandardLibrary
decoder := json.NewDecoder(resp.Body)
pr := api.PullRequest{}
err := decoder.Decode(&pr)

4
integrations/create_no_session_test.go

@ -5,7 +5,6 @@
package integrations
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
@ -18,6 +17,7 @@ import (
"code.gitea.io/gitea/routers/routes"
"gitea.com/go-chi/session"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
@ -62,6 +62,8 @@ func TestSessionFileCreation(t *testing.T) {
}()
var config session.Options
json := jsoniter.ConfigCompatibleWithStandardLibrary
err := json.Unmarshal([]byte(oldSessionConfig), &config)
assert.NoError(t, err)

6
integrations/integration_test.go

@ -8,7 +8,6 @@ import (
"bytes"
"context"
"database/sql"
"encoding/json"
"fmt"
"io"
"net/http"
@ -36,6 +35,7 @@ import (
"code.gitea.io/gitea/routers/routes"
"github.com/PuerkitoBio/goquery"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
@ -376,6 +376,8 @@ 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))
@ -453,6 +455,8 @@ 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))
}

10
integrations/oauth_test.go

@ -6,12 +6,12 @@ package integrations
import (
"bytes"
"encoding/json"
"io/ioutil"
"testing"
"code.gitea.io/gitea/modules/setting"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
@ -70,6 +70,8 @@ func TestAccessTokenExchange(t *testing.T) {
RefreshToken string `json:"refresh_token"`
}
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)
@ -93,6 +95,8 @@ func TestAccessTokenExchangeWithoutPKCE(t *testing.T) {
RefreshToken string `json:"refresh_token"`
}
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)
@ -181,6 +185,8 @@ func TestAccessTokenExchangeWithBasicAuth(t *testing.T) {
RefreshToken string `json:"refresh_token"`
}
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)
@ -223,6 +229,8 @@ func TestRefreshTokenInvalidation(t *testing.T) {
RefreshToken string `json:"refresh_token"`
}
parsed := new(response)
json := jsoniter.ConfigCompatibleWithStandardLibrary
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
// test without invalidation

3
integrations/repo_commits_test.go

@ -5,7 +5,6 @@
package integrations
import (
"encoding/json"
"net/http"
"net/http/httptest"
"path"
@ -14,6 +13,7 @@ import (
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
@ -82,6 +82,7 @@ func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) {
}
func testRepoCommitsWithStatus(t *testing.T, resp *httptest.ResponseRecorder, state string) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
decoder := json.NewDecoder(resp.Body)
statuses := []*api.CommitStatus{}
assert.NoError(t, decoder.Decode(&statuses))

3
integrations/testlogger.go

@ -6,7 +6,6 @@ package integrations
import (
"context"
"encoding/json"
"fmt"
"os"
"runtime"
@ -17,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/queue"
jsoniter "github.com/json-iterator/go"
)
var (
@ -158,6 +158,7 @@ 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

5
models/issue_comment.go

@ -8,7 +8,6 @@ package models
import (
"container/list"
"encoding/json"
"fmt"
"regexp"
"strconv"
@ -21,6 +20,7 @@ import (
"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"
@ -655,6 +655,7 @@ func (c *Comment) LoadPushCommits() (err error) {
var data PushActionContent
json := jsoniter.ConfigCompatibleWithStandardLibrary
err = json.Unmarshal([]byte(c.Content), &data)
if err != nil {
return
@ -1241,6 +1242,8 @@ 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

12
models/login_source.go

@ -7,7 +7,6 @@ package models
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"net/smtp"
@ -22,6 +21,7 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
jsoniter "github.com/json-iterator/go"
"xorm.io/xorm"
"xorm.io/xorm/convert"
@ -75,11 +75,13 @@ type LDAPConfig struct {
// FromDB fills up a LDAPConfig from serialized format.
func (cfg *LDAPConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, &cfg)
}
// ToDB exports a LDAPConfig to a serialized format.
func (cfg *LDAPConfig) ToDB() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(cfg)
}
@ -101,11 +103,13 @@ type SMTPConfig struct {
// FromDB fills up an SMTPConfig from serialized format.
func (cfg *SMTPConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, cfg)
}
// ToDB exports an SMTPConfig to a serialized format.
func (cfg *SMTPConfig) ToDB() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(cfg)
}
@ -116,11 +120,13 @@ type PAMConfig struct {
// FromDB fills up a PAMConfig from serialized format.
func (cfg *PAMConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, &cfg)
}
// ToDB exports a PAMConfig to a serialized format.
func (cfg *PAMConfig) ToDB() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(cfg)
}
@ -136,11 +142,13 @@ type OAuth2Config struct {
// FromDB fills up an OAuth2Config from serialized format.
func (cfg *OAuth2Config) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, cfg)
}
// ToDB exports an SMTPConfig to a serialized format.
func (cfg *OAuth2Config) ToDB() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(cfg)
}
@ -155,11 +163,13 @@ type SSPIConfig struct {
// FromDB fills up an SSPIConfig from serialized format.
func (cfg *SSPIConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, cfg)
}
// ToDB exports an SSPIConfig to a serialized format.
func (cfg *SSPIConfig) ToDB() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(cfg)
}

4
models/migrations/v130.go

@ -5,9 +5,8 @@
package migrations
import (
"encoding/json"
"code.gitea.io/gitea/modules/setting"
jsoniter "github.com/json-iterator/go"
"xorm.io/xorm"
)
@ -72,6 +71,7 @@ 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
}

12
models/repo_unit.go

@ -5,11 +5,11 @@
package models
import (
"encoding/json"
"fmt"
"code.gitea.io/gitea/modules/timeutil"
jsoniter "github.com/json-iterator/go"
"xorm.io/xorm"
"xorm.io/xorm/convert"
)
@ -29,11 +29,13 @@ type UnitConfig struct {
// FromDB fills up a UnitConfig from serialized format.
func (cfg *UnitConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, &cfg)
}
// ToDB exports a UnitConfig to a serialized format.
func (cfg *UnitConfig) ToDB() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(cfg)
}
@ -44,11 +46,13 @@ type ExternalWikiConfig struct {
// FromDB fills up a ExternalWikiConfig from serialized format.
func (cfg *ExternalWikiConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, &cfg)
}
// ToDB exports a ExternalWikiConfig to a serialized format.
func (cfg *ExternalWikiConfig) ToDB() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(cfg)
}
@ -61,11 +65,13 @@ type ExternalTrackerConfig struct {
// FromDB fills up a ExternalTrackerConfig from serialized format.
func (cfg *ExternalTrackerConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, &cfg)
}
// ToDB exports a ExternalTrackerConfig to a serialized format.
func (cfg *ExternalTrackerConfig) ToDB() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(cfg)
}
@ -78,11 +84,13 @@ type IssuesConfig struct {
// FromDB fills up a IssuesConfig from serialized format.
func (cfg *IssuesConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, &cfg)
}
// ToDB exports a IssuesConfig to a serialized format.
func (cfg *IssuesConfig) ToDB() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(cfg)
}
@ -97,11 +105,13 @@ type PullRequestsConfig struct {
// FromDB fills up a PullRequestsConfig from serialized format.
func (cfg *PullRequestsConfig) FromDB(bs []byte) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(bs, &cfg)
}
// ToDB exports a PullRequestsConfig to a serialized format.
func (cfg *PullRequestsConfig) ToDB() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(cfg)
}

4
models/task.go

@ -5,12 +5,12 @@
package models
import (
"encoding/json"
"fmt"
migration "code.gitea.io/gitea/modules/migrations/base"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
jsoniter "github.com/json-iterator/go"
"xorm.io/builder"
)
@ -105,6 +105,7 @@ 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
@ -162,6 +163,7 @@ 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
}

3
models/user_heatmap_test.go

@ -5,10 +5,10 @@
package models
import (
"encoding/json"
"fmt"
"testing"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
@ -56,6 +56,7 @@ func TestGetUserHeatmapDataByUser(t *testing.T) {
assert.Equal(t, tc.CountResult, len(heatmap), 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))

7
models/webhook.go

@ -7,7 +7,6 @@ package models
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
@ -18,6 +17,7 @@ import (
"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
@ -145,6 +145,8 @@ 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)
}
@ -157,6 +159,7 @@ 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
@ -689,6 +692,7 @@ 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)
}
@ -702,6 +706,7 @@ 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)

3
models/webhook_test.go

@ -6,12 +6,12 @@ package models
import (
"context"
"encoding/json"
"testing"
"time"
api "code.gitea.io/gitea/modules/structs"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
@ -58,6 +58,7 @@ 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)
}

3
modules/context/context.go

@ -9,7 +9,6 @@ import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"html"
"html/template"
@ -34,6 +33,7 @@ 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"
@ -370,6 +370,7 @@ 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)
}

4
modules/eventsource/event.go

@ -6,11 +6,12 @@ package eventsource
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strings"
"time"
jsoniter "github.com/json-iterator/go"
)
func wrapNewlines(w io.Writer, prefix []byte, value []byte) (sum int64, err error) {
@ -79,6 +80,7 @@ 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

4
modules/httplib/httplib.go

@ -8,7 +8,6 @@ package httplib
import (
"bytes"
"crypto/tls"
"encoding/json"
"encoding/xml"
"io"
"io/ioutil"
@ -23,6 +22,8 @@ import (
"strings"
"sync"
"time"
jsoniter "github.com/json-iterator/go"
)
var defaultSetting = Settings{false, "GiteaServer", 60 * time.Second, 60 * time.Second, nil, nil, nil, false}
@ -434,6 +435,7 @@ func (r *Request) ToJSON(v interface{}) error {
if err != nil {
return err
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
err = json.Unmarshal(data, v)
return err
}

3
modules/indexer/code/elastic_search.go

@ -6,7 +6,6 @@ package code
import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
@ -22,6 +21,7 @@ import (
"code.gitea.io/gitea/modules/timeutil"
"github.com/go-enry/go-enry/v2"
jsoniter "github.com/json-iterator/go"
"github.com/olivere/elastic/v7"
)
@ -300,6 +300,7 @@ 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
}

4
modules/lfs/locks.go

@ -5,7 +5,6 @@
package lfs
import (
"encoding/json"
"strconv"
"strings"
@ -15,6 +14,7 @@ import (
"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"
)
//checkIsValidRequest check if it a valid request in case of bad request it write the response to ctx.
@ -184,6 +184,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)
@ -319,6 +320,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)

7
modules/lfs/server.go

@ -6,7 +6,6 @@ package lfs
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
@ -23,6 +22,7 @@ import (
"code.gitea.io/gitea/modules/storage"
"github.com/dgrijalva/jwt-go"
jsoniter "github.com/json-iterator/go"
)
const (
@ -233,6 +233,7 @@ func getMetaHandler(ctx *context.Context) {
ctx.Resp.Header().Set("Content-Type", metaMediaType)
if ctx.Req.Method == "GET" {
json := jsoniter.ConfigCompatibleWithStandardLibrary
enc := json.NewEncoder(ctx.Resp)
if err := enc.Encode(Represent(rv, meta, true, false)); err != nil {
log.Error("Failed to encode representation as json. Error: %v", err)
@ -304,6 +305,7 @@ func PostHandler(ctx *context.Context) {
}
ctx.Resp.WriteHeader(sentStatus)
json := jsoniter.ConfigCompatibleWithStandardLibrary
enc := json.NewEncoder(ctx.Resp)
if err := enc.Encode(Represent(rv, meta, meta.Existing, true)); err != nil {
log.Error("Failed to encode representation as json. Error: %v", err)
@ -394,6 +396,7 @@ func BatchHandler(ctx *context.Context) {
respobj := &BatchResponse{Objects: responseObjects}
json := jsoniter.ConfigCompatibleWithStandardLibrary
enc := json.NewEncoder(ctx.Resp)
if err := enc.Encode(respobj); err != nil {
log.Error("Failed to encode representation as json. Error: %v", err)
@ -531,6 +534,7 @@ func unpack(ctx *context.Context) *RequestVars {
var p RequestVars
bodyReader := r.Body
defer bodyReader.Close()
json := jsoniter.ConfigCompatibleWithStandardLibrary
dec := json.NewDecoder(bodyReader)
err := dec.Decode(&p)
if err != nil {
@ -554,6 +558,7 @@ func unpackbatch(ctx *context.Context) *BatchVars {
bodyReader := r.Body
defer bodyReader.Close()
json := jsoniter.ConfigCompatibleWithStandardLibrary
dec := json.NewDecoder(bodyReader)
err := dec.Decode(&bv)
if err != nil {

7
modules/log/conn.go

@ -6,9 +6,11 @@
package log
import (
"encoding/json"
"fmt"
"io"
"net"
jsoniter "github.com/json-iterator/go"
)
type connWriter struct {
@ -104,9 +106,10 @@ 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 err
return fmt.Errorf("Unable to parse JSON: %v", err)
}
log.NewWriterLogger(&connWriter{
ReconnectOnMsg: log.ReconnectOnMsg,

3
modules/log/conn_test.go

@ -98,7 +98,8 @@ func TestConnLoggerBadConfig(t *testing.T) {
logger := NewConn()
err := logger.Init("{")
assert.Equal(t, "unexpected end of JSON input", err.Error())
assert.Error(t, err)
assert.Contains(t, err.Error(), "Unable to parse JSON")
logger.Close()
}

7
modules/log/console.go

@ -6,9 +6,11 @@
package log
import (
"encoding/json"
"fmt"
"io"
"os"
jsoniter "github.com/json-iterator/go"
)
// CanColorStdout reports if we can color the Stdout
@ -50,9 +52,10 @@ 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 err
return fmt.Errorf("Unable to parse JSON: %v", err)
}
if log.Stderr {
log.NewWriterLogger(&nopWriteCloser{

3
modules/log/console_test.go

@ -17,7 +17,8 @@ func TestConsoleLoggerBadConfig(t *testing.T) {
logger := NewConsoleLogger()
err := logger.Init("{")
assert.Equal(t, "unexpected end of JSON input", err.Error())
assert.Error(t, err)
assert.Contains(t, err.Error(), "Unable to parse JSON")
logger.Close()
}

5
modules/log/file.go

@ -7,7 +7,6 @@ package log
import (
"bufio"
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"os"
@ -17,6 +16,7 @@ import (
"time"
"code.gitea.io/gitea/modules/util"
jsoniter "github.com/json-iterator/go"
)
// FileLogger implements LoggerProvider.
@ -101,8 +101,9 @@ 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 err
return fmt.Errorf("Unable to parse JSON: %v", err)
}
if len(log.Filename) == 0 {
return errors.New("config must have filename")

4
modules/log/level.go

@ -6,10 +6,11 @@ package log
import (
"bytes"
"encoding/json"
"fmt"
"os"
"strings"
jsoniter "github.com/json-iterator/go"
)
// Level is the level of the logger
@ -103,6 +104,7 @@ 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)

3
modules/log/level_test.go

@ -5,10 +5,10 @@
package log
import (
"encoding/json"
"fmt"
"testing"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
@ -17,6 +17,7 @@ type testLevel struct {
}
func TestLevelMarshalUnmarshalJSON(t *testing.T) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
levelBytes, err := json.Marshal(testLevel{
Level: INFO,
})

7
modules/log/smtp.go

@ -6,9 +6,11 @@
package log
import (
"encoding/json"
"fmt"
"net/smtp"
"strings"
jsoniter "github.com/json-iterator/go"
)
type smtpWriter struct {
@ -55,9 +57,10 @@ 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 err
return fmt.Errorf("Unable to parse JSON: %v", err)
}
log.NewWriterLogger(&smtpWriter{
owner: log,

4
modules/notification/action/action.go

@ -5,7 +5,6 @@
package action
import (
"encoding/json"
"fmt"
"path"
"strings"
@ -14,6 +13,7 @@ import (
"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,6 +296,7 @@ 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)
@ -365,6 +366,7 @@ 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)

4
modules/private/hook.go

@ -5,7 +5,6 @@
package private
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
@ -13,6 +12,7 @@ import (
"time"
"code.gitea.io/gitea/modules/setting"
jsoniter "github.com/json-iterator/go"
)
// Git environment variables
@ -80,6 +80,7 @@ func HookPreReceive(ownerName, repoName string, opts HookOptions) (int, string)
)
req := newInternalRequest(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)
@ -106,6 +107,7 @@ func HookPostReceive(ownerName, repoName string, opts HookOptions) (*HookPostRec
req := newInternalRequest(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()

3
modules/private/internal.go

@ -6,13 +6,13 @@ package private
import (
"crypto/tls"
"encoding/json"
"fmt"
"net"
"net/http"
"code.gitea.io/gitea/modules/httplib"
"code.gitea.io/gitea/modules/setting"
jsoniter "github.com/json-iterator/go"
)
func newRequest(url, method string) *httplib.Request {
@ -27,6 +27,7 @@ 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()

3
modules/private/mail.go

@ -5,12 +5,12 @@
package private
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"code.gitea.io/gitea/modules/setting"
jsoniter "github.com/json-iterator/go"
)
// Email structure holds a data for sending general emails
@ -32,6 +32,7 @@ func SendEmail(subject, message string, to []string) (int, string) {
req := newInternalRequest(reqURL, "POST")
req = req.Header("Content-Type", "application/json")
json := jsoniter.ConfigCompatibleWithStandardLibrary
jsonBytes, _ := json.Marshal(Email{
Subject: subject,
Message: message,

4
modules/private/manager.go

@ -5,13 +5,13 @@
package private
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
"code.gitea.io/gitea/modules/setting"
jsoniter "github.com/json-iterator/go"
)
// Shutdown calls the internal shutdown function
@ -65,6 +65,7 @@ func FlushQueues(timeout time.Duration, nonBlocking bool) (int, string) {
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,
@ -151,6 +152,7 @@ func AddLogger(group, name, mode string, config map[string]interface{}) (int, st
req := newInternalRequest(reqURL, "POST")
req = req.Header("Content-Type", "application/json")
json := jsoniter.ConfigCompatibleWithStandardLibrary
jsonBytes, _ := json.Marshal(LoggerOptions{
Group: group,
Name: name,

4
modules/private/serv.go

@ -5,13 +5,13 @@