Browse Source

*: use jsoniter to replace encoding/json

pull/5260/merge
Unknwon 7 years ago
parent
commit
fbecc18e2e
No known key found for this signature in database
GPG Key ID: 7A02C406FAC875A2
  1. 6
      models/action.go
  2. 14
      models/login_source.go
  3. 10
      models/migrations/v13.go
  4. 14
      models/webhook.go
  5. 5
      models/webhook_dingtalk.go
  6. 9
      models/webhook_discord.go
  7. 9
      models/webhook_slack.go
  8. 9
      pkg/httplib/httplib.go
  9. 6
      pkg/template/template.go
  10. 4
      routes/admin/admin.go
  11. 7
      routes/api/v1/repo/hook.go
  12. 10
      routes/repo/webhook.go

6
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
}

14
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.

10
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`=?",

14
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)
}

5
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
}

9
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 {

9
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 {

9
pkg/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"
"github.com/json-iterator/go"
)
var defaultSetting = Settings{false, "GogsServer", 60 * time.Second, 60 * time.Second, nil, nil, nil, false}
@ -416,8 +417,7 @@ func (r *Request) ToJson(v interface{}) error {
if err != nil {
return err
}
err = json.Unmarshal(data, v)
return err
return jsoniter.Unmarshal(data, v)
}
// ToXml returns the map that marshals from the body bytes as xml in response .
@ -427,8 +427,7 @@ func (r *Request) ToXml(v interface{}) error {
if err != nil {
return err
}
err = xml.Unmarshal(data, v)
return err
return xml.Unmarshal(data, v)
}
// Response executes request client gets response mannually.

6
pkg/template/template.go

@ -6,7 +6,6 @@ package template
import (
"container/list"
"encoding/json"
"fmt"
"html/template"
"mime"
@ -15,6 +14,7 @@ import (
"strings"
"time"
"github.com/json-iterator/go"
"github.com/microcosm-cc/bluemonday"
"golang.org/x/net/html/charset"
"golang.org/x/text/transform"
@ -281,8 +281,8 @@ func ActionIcon(opType int) string {
func ActionContent2Commits(act Actioner) *models.PushCommits {
push := models.NewPushCommits()
if err := json.Unmarshal([]byte(act.GetContent()), push); err != nil {
log.Error(4, "json.Unmarshal:\n%s\nERROR: %v", act.GetContent(), err)
if err := jsoniter.Unmarshal([]byte(act.GetContent()), push); err != nil {
log.Error(4, "Unmarshal:\n%s\nERROR: %v", act.GetContent(), err)
}
return push
}

4
routes/admin/admin.go

@ -5,13 +5,13 @@
package admin
import (
"encoding/json"
"fmt"
"runtime"
"strings"
"time"
"github.com/Unknwon/com"
"github.com/json-iterator/go"
"gopkg.in/macaron.v1"
"github.com/gogs/gogs/models"
@ -241,7 +241,7 @@ func Config(c *context.Context) {
Mode: strings.Title(setting.LogModes[i]),
}
result, _ := json.MarshalIndent(setting.LogConfigs[i], "", " ")
result, _ := jsoniter.MarshalIndent(setting.LogConfigs[i], "", " ")
loggers[i].Config = string(result)
}
c.Data["Loggers"] = loggers

7
routes/api/v1/repo/hook.go

@ -5,9 +5,8 @@
package repo
import (
"encoding/json"
"github.com/Unknwon/com"
"github.com/json-iterator/go"
api "github.com/gogs/go-gogs-client"
@ -79,7 +78,7 @@ func CreateHook(c *context.APIContext, form api.CreateHookOption) {
c.Error(422, "", "Missing config option: channel")
return
}
meta, err := json.Marshal(&models.SlackMeta{
meta, err := jsoniter.Marshal(&models.SlackMeta{
Channel: channel,
Username: form.Config["username"],
IconURL: form.Config["icon_url"],
@ -129,7 +128,7 @@ func EditHook(c *context.APIContext, form api.EditHookOption) {
if w.HookTaskType == models.SLACK {
if channel, ok := form.Config["channel"]; ok {
meta, err := json.Marshal(&models.SlackMeta{
meta, err := jsoniter.Marshal(&models.SlackMeta{
Channel: channel,
Username: form.Config["username"],
IconURL: form.Config["icon_url"],

10
routes/repo/webhook.go

@ -5,11 +5,11 @@
package repo
import (
"encoding/json"
"fmt"
"strings"
"github.com/Unknwon/com"
"github.com/json-iterator/go"
git "github.com/gogs/git-module"
api "github.com/gogs/go-gogs-client"
@ -185,7 +185,7 @@ func SlackHooksNewPost(c *context.Context, f form.NewSlackHook) {
return
}
meta, err := json.Marshal(&models.SlackMeta{
meta, err := jsoniter.Marshal(&models.SlackMeta{
Channel: f.Channel,
Username: f.Username,
IconURL: f.IconURL,
@ -236,7 +236,7 @@ func DiscordHooksNewPost(c *context.Context, f form.NewDiscordHook) {
return
}
meta, err := json.Marshal(&models.SlackMeta{
meta, err := jsoniter.Marshal(&models.SlackMeta{
Username: f.Username,
IconURL: f.IconURL,
Color: f.Color,
@ -415,7 +415,7 @@ func SlackHooksEditPost(c *context.Context, f form.NewSlackHook) {
return
}
meta, err := json.Marshal(&models.SlackMeta{
meta, err := jsoniter.Marshal(&models.SlackMeta{
Channel: f.Channel,
Username: f.Username,
IconURL: f.IconURL,
@ -459,7 +459,7 @@ func DiscordHooksEditPost(c *context.Context, f form.NewDiscordHook) {
return
}
meta, err := json.Marshal(&models.SlackMeta{
meta, err := jsoniter.Marshal(&models.SlackMeta{
Username: f.Username,
IconURL: f.IconURL,
Color: f.Color,

Loading…
Cancel
Save