diff options
author | Unknwon <u@gogs.io> | 2018-06-09 17:32:58 +0800 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2018-06-09 17:32:58 +0800 |
commit | fbecc18e2e4d4eb863a990a57218305f190ae6db (patch) | |
tree | 11278a833eec45d8bf710f4e6e27481975b96eaa /models | |
parent | b538c5345e1271fcaf413b82ba1ece555dd635a5 (diff) |
*: use jsoniter to replace encoding/json
Diffstat (limited to 'models')
-rw-r--r-- | models/action.go | 6 | ||||
-rw-r--r-- | models/login_source.go | 14 | ||||
-rw-r--r-- | models/migrations/v13.go | 10 | ||||
-rw-r--r-- | models/webhook.go | 14 | ||||
-rw-r--r-- | models/webhook_dingtalk.go | 5 | ||||
-rw-r--r-- | models/webhook_discord.go | 9 | ||||
-rw-r--r-- | models/webhook_slack.go | 9 |
7 files changed, 35 insertions, 32 deletions
diff --git a/models/action.go b/models/action.go index a5e1a526..61c6afc3 100644 --- a/models/action.go +++ b/models/action.go @@ -5,7 +5,6 @@ package models import ( - "encoding/json" "fmt" "path" "regexp" @@ -15,6 +14,7 @@ import ( "github.com/Unknwon/com" "github.com/go-xorm/xorm" + "github.com/json-iterator/go" log "gopkg.in/clog.v1" "github.com/gogs/git-module" @@ -501,7 +501,7 @@ func CommitRepoAction(opts CommitRepoActionOptions) error { opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum] } - data, err := json.Marshal(opts.Commits) + data, err := jsoniter.Marshal(opts.Commits) if err != nil { return fmt.Errorf("Marshal: %v", err) } @@ -717,7 +717,7 @@ func MirrorSyncPushAction(repo *Repository, opts MirrorSyncPushActionOptions) er return fmt.Errorf("PrepareWebhooks: %v", err) } - data, err := json.Marshal(opts.Commits) + data, err := jsoniter.Marshal(opts.Commits) if err != nil { return err } diff --git a/models/login_source.go b/models/login_source.go index e72aa496..88ad42dc 100644 --- a/models/login_source.go +++ b/models/login_source.go @@ -6,7 +6,6 @@ package models import ( "crypto/tls" - "encoding/json" "fmt" "net/smtp" "net/textproto" @@ -20,6 +19,7 @@ import ( "github.com/go-macaron/binding" "github.com/go-xorm/core" "github.com/go-xorm/xorm" + "github.com/json-iterator/go" log "gopkg.in/clog.v1" "gopkg.in/ini.v1" @@ -66,11 +66,11 @@ type LDAPConfig struct { } func (cfg *LDAPConfig) FromDB(bs []byte) error { - return json.Unmarshal(bs, &cfg) + return jsoniter.Unmarshal(bs, &cfg) } func (cfg *LDAPConfig) ToDB() ([]byte, error) { - return json.Marshal(cfg) + return jsoniter.Marshal(cfg) } func (cfg *LDAPConfig) SecurityProtocolName() string { @@ -87,11 +87,11 @@ type SMTPConfig struct { } func (cfg *SMTPConfig) FromDB(bs []byte) error { - return json.Unmarshal(bs, cfg) + return jsoniter.Unmarshal(bs, cfg) } func (cfg *SMTPConfig) ToDB() ([]byte, error) { - return json.Marshal(cfg) + return jsoniter.Marshal(cfg) } type PAMConfig struct { @@ -99,11 +99,11 @@ type PAMConfig struct { } func (cfg *PAMConfig) FromDB(bs []byte) error { - return json.Unmarshal(bs, &cfg) + return jsoniter.Unmarshal(bs, &cfg) } func (cfg *PAMConfig) ToDB() ([]byte, error) { - return json.Marshal(cfg) + return jsoniter.Marshal(cfg) } // AuthSourceFile contains information of an authentication source file. diff --git a/models/migrations/v13.go b/models/migrations/v13.go index f81271f9..3f7e81df 100644 --- a/models/migrations/v13.go +++ b/models/migrations/v13.go @@ -5,12 +5,12 @@ package migrations import ( - "encoding/json" "fmt" "strings" "github.com/Unknwon/com" "github.com/go-xorm/xorm" + "github.com/json-iterator/go" ) func ldapUseSSLToSecurityProtocol(x *xorm.Engine) error { @@ -30,17 +30,17 @@ func ldapUseSSLToSecurityProtocol(x *xorm.Engine) error { for _, result := range results { cfg := map[string]interface{}{} - if err = json.Unmarshal(result["cfg"], &cfg); err != nil { - return fmt.Errorf("decode JSON config: %v", err) + if err = jsoniter.Unmarshal(result["cfg"], &cfg); err != nil { + return fmt.Errorf("unmarshal JSON config: %v", err) } if com.ToStr(cfg["UseSSL"]) == "true" { cfg["SecurityProtocol"] = 1 // LDAPS } delete(cfg, "UseSSL") - data, err := json.Marshal(&cfg) + data, err := jsoniter.Marshal(&cfg) if err != nil { - return fmt.Errorf("encode JSON config: %v", err) + return fmt.Errorf("marshal JSON config: %v", err) } if _, err = sess.Exec("UPDATE `login_source` SET `cfg`=? WHERE `id`=?", diff --git a/models/webhook.go b/models/webhook.go index ca2ee588..59a1962a 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -9,13 +9,13 @@ import ( "crypto/sha256" "crypto/tls" "encoding/hex" - "encoding/json" "fmt" "io/ioutil" "strings" "time" "github.com/go-xorm/xorm" + "github.com/json-iterator/go" gouuid "github.com/satori/go.uuid" log "gopkg.in/clog.v1" @@ -126,7 +126,7 @@ func (w *Webhook) AfterSet(colName string, _ xorm.Cell) { switch colName { case "events": w.HookEvent = &HookEvent{} - if err = json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil { + if err = jsoniter.Unmarshal([]byte(w.Events), w.HookEvent); err != nil { log.Error(3, "Unmarshal [%d]: %v", w.ID, err) } case "created_unix": @@ -138,7 +138,7 @@ func (w *Webhook) AfterSet(colName string, _ xorm.Cell) { func (w *Webhook) GetSlackHook() *SlackMeta { s := &SlackMeta{} - if err := json.Unmarshal([]byte(w.Meta), s); err != nil { + if err := jsoniter.Unmarshal([]byte(w.Meta), s); err != nil { log.Error(2, "GetSlackHook [%d]: %v", w.ID, err) } return s @@ -151,7 +151,7 @@ func (w *Webhook) History(page int) ([]*HookTask, error) { // UpdateEvent handles conversion from HookEvent to Events. func (w *Webhook) UpdateEvent() error { - data, err := json.Marshal(w.HookEvent) + data, err := jsoniter.Marshal(w.HookEvent) w.Events = string(data) return err } @@ -456,7 +456,7 @@ func (t *HookTask) AfterSet(colName string, _ xorm.Cell) { } t.RequestInfo = &HookRequest{} - if err = json.Unmarshal([]byte(t.RequestContent), t.RequestInfo); err != nil { + if err = jsoniter.Unmarshal([]byte(t.RequestContent), t.RequestInfo); err != nil { log.Error(3, "Unmarshal[%d]: %v", t.ID, err) } @@ -466,14 +466,14 @@ func (t *HookTask) AfterSet(colName string, _ xorm.Cell) { } t.ResponseInfo = &HookResponse{} - if err = json.Unmarshal([]byte(t.ResponseContent), t.ResponseInfo); err != nil { + if err = jsoniter.Unmarshal([]byte(t.ResponseContent), t.ResponseInfo); err != nil { log.Error(3, "Unmarshal [%d]: %v", t.ID, err) } } } func (t *HookTask) MarshalJSON(v interface{}) string { - p, err := json.Marshal(v) + p, err := jsoniter.Marshal(v) if err != nil { log.Error(3, "Marshal [%d]: %v", t.ID, err) } diff --git a/models/webhook_dingtalk.go b/models/webhook_dingtalk.go index e910ede7..99b623cc 100644 --- a/models/webhook_dingtalk.go +++ b/models/webhook_dingtalk.go @@ -5,10 +5,11 @@ package models import ( - "encoding/json" "fmt" "strings" + "github.com/json-iterator/go" + "github.com/gogs/git-module" api "github.com/gogs/go-gogs-client" ) @@ -41,7 +42,7 @@ type DingtalkPayload struct { } func (p *DingtalkPayload) JSONPayload() ([]byte, error) { - data, err := json.MarshalIndent(p, "", " ") + data, err := jsoniter.MarshalIndent(p, "", " ") if err != nil { return []byte{}, err } diff --git a/models/webhook_discord.go b/models/webhook_discord.go index 952d093a..5bd61a7a 100644 --- a/models/webhook_discord.go +++ b/models/webhook_discord.go @@ -5,11 +5,12 @@ package models import ( - "encoding/json" "fmt" "strconv" "strings" + "github.com/json-iterator/go" + "github.com/gogs/git-module" api "github.com/gogs/go-gogs-client" @@ -49,7 +50,7 @@ type DiscordPayload struct { } func (p *DiscordPayload) JSONPayload() ([]byte, error) { - data, err := json.MarshalIndent(p, "", " ") + data, err := jsoniter.MarshalIndent(p, "", " ") if err != nil { return []byte{}, err } @@ -371,8 +372,8 @@ func getDiscordReleasePayload(p *api.ReleasePayload) (*DiscordPayload, error) { func GetDiscordPayload(p api.Payloader, event HookEventType, meta string) (payload *DiscordPayload, err error) { slack := &SlackMeta{} - if err := json.Unmarshal([]byte(meta), &slack); err != nil { - return nil, fmt.Errorf("json.Unmarshal: %v", err) + if err := jsoniter.Unmarshal([]byte(meta), &slack); err != nil { + return nil, fmt.Errorf("jsoniter.Unmarshal: %v", err) } switch event { diff --git a/models/webhook_slack.go b/models/webhook_slack.go index be75303e..09e58d3d 100644 --- a/models/webhook_slack.go +++ b/models/webhook_slack.go @@ -5,10 +5,11 @@ package models import ( - "encoding/json" "fmt" "strings" + "github.com/json-iterator/go" + "github.com/gogs/git-module" api "github.com/gogs/go-gogs-client" @@ -40,7 +41,7 @@ type SlackPayload struct { } func (p *SlackPayload) JSONPayload() ([]byte, error) { - data, err := json.MarshalIndent(p, "", " ") + data, err := jsoniter.MarshalIndent(p, "", " ") if err != nil { return []byte{}, err } @@ -288,8 +289,8 @@ func getSlackReleasePayload(p *api.ReleasePayload) (*SlackPayload, error) { func GetSlackPayload(p api.Payloader, event HookEventType, meta string) (payload *SlackPayload, err error) { slack := &SlackMeta{} - if err := json.Unmarshal([]byte(meta), &slack); err != nil { - return nil, fmt.Errorf("json.Unmarshal: %v", err) + if err := jsoniter.Unmarshal([]byte(meta), &slack); err != nil { + return nil, fmt.Errorf("Unmarshal: %v", err) } switch event { |