summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2021-03-01 21:08:10 +0000
committerGitHub <noreply@github.com>2021-03-01 22:08:10 +0100
commitf0e15250b9e322cc7731ba026d12387c2b549a42 (patch)
treef13d46119077ba924d620ef172b91daa315bda0a /models
parent59fd641d1fb021e35aea7f9f4a1916cc11ef5c51 (diff)
downloadgitea-f0e15250b9e322cc7731ba026d12387c2b549a42.tar.gz
gitea-f0e15250b9e322cc7731ba026d12387c2b549a42.zip
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>
Diffstat (limited to 'models')
-rw-r--r--models/issue_comment.go5
-rw-r--r--models/login_source.go12
-rw-r--r--models/migrations/v130.go4
-rw-r--r--models/repo_unit.go12
-rw-r--r--models/task.go4
-rw-r--r--models/user_heatmap_test.go3
-rw-r--r--models/webhook.go7
-rw-r--r--models/webhook_test.go3
8 files changed, 41 insertions, 9 deletions
diff --git a/models/issue_comment.go b/models/issue_comment.go
index 6cc03ba0e8..724cf921ea 100644
--- a/models/issue_comment.go
+++ b/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
diff --git a/models/login_source.go b/models/login_source.go
index d351f12861..37bbdc4597 100644
--- a/models/login_source.go
+++ b/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)
}
diff --git a/models/migrations/v130.go b/models/migrations/v130.go
index f7be11c400..4f044e26ab 100644
--- a/models/migrations/v130.go
+++ b/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
}
diff --git a/models/repo_unit.go b/models/repo_unit.go
index 8c2d458758..3ef3904833 100644
--- a/models/repo_unit.go
+++ b/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)
}
diff --git a/models/task.go b/models/task.go
index b729bb8632..35b77a878f 100644
--- a/models/task.go
+++ b/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
}
diff --git a/models/user_heatmap_test.go b/models/user_heatmap_test.go
index d98c4c63e4..6ec8a1a479 100644
--- a/models/user_heatmap_test.go
+++ b/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))
diff --git a/models/webhook.go b/models/webhook.go
index c7fcfba49e..9ad01f1b9d 100644
--- a/models/webhook.go
+++ b/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)
diff --git a/models/webhook_test.go b/models/webhook_test.go
index 1baf6ef44b..31cc29d0a4 100644
--- a/models/webhook_test.go
+++ b/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)
}