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>
This commit is contained in:
parent
59fd641d1f
commit
f0e15250b9
77 changed files with 264 additions and 82 deletions
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
})
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -5,13 +5,13 @@
|
|||
package private
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
// KeyAndOwner is the response from ServNoCommand
|
||||
|
@ -34,6 +34,7 @@ func ServNoCommand(keyID int64) (*models.PublicKey, *models.User, error) {
|
|||
}
|
||||
|
||||
var keyAndOwner KeyAndOwner
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
if err := json.NewDecoder(resp.Body).Decode(&keyAndOwner); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -90,6 +91,7 @@ func ServCommand(keyID int64, ownerName, repoName string, mode models.AccessMode
|
|||
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 {
|
||||
|
|
|
@ -5,8 +5,9 @@
|
|||
package queue
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
// Mappable represents an interface that can MapTo another interface
|
||||
|
@ -19,6 +20,7 @@ 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)) {
|
||||
|
@ -66,6 +68,7 @@ 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)
|
||||
|
|
|
@ -6,7 +6,6 @@ package queue
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
|
@ -14,6 +13,7 @@ import (
|
|||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
var manager *Manager
|
||||
|
@ -110,6 +110,7 @@ func (m *Manager) Add(managed interface{},
|
|||
configuration,
|
||||
exemplar interface{}) int64 {
|
||||
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
cfg, _ := json.Marshal(configuration)
|
||||
mq := &ManagedQueue{
|
||||
Type: t,
|
||||
|
|
|
@ -6,12 +6,12 @@ package queue
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
// ByteFIFOQueueConfiguration is the configuration for a ByteFIFOQueue
|
||||
|
@ -71,6 +71,7 @@ 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
|
||||
|
@ -229,6 +230,7 @@ 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
|
||||
|
|
|
@ -5,9 +5,9 @@
|
|||
package queue
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
@ -30,6 +30,7 @@ func TestToConfig(t *testing.T) {
|
|||
assert.NotEqual(t, cfg2, exemplar)
|
||||
assert.Equal(t, &cfg, &cfg2)
|
||||
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
cfgString, err := json.Marshal(cfg)
|
||||
assert.NoError(t, err)
|
||||
|
||||
|
|
|
@ -5,12 +5,12 @@
|
|||
package queue
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func validType(t string) (Type, error) {
|
||||
|
@ -28,6 +28,7 @@ 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)
|
||||
|
|
|
@ -6,7 +6,6 @@ package recaptcha
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
@ -15,6 +14,7 @@ import (
|
|||
|
||||
"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
|
||||
|
@ -51,6 +51,7 @@ func Verify(ctx context.Context, response string) (bool, error) {
|
|||
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)
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
|
@ -14,6 +13,7 @@ import (
|
|||
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,6 +25,7 @@ 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
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
package setting
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
golog "log"
|
||||
"os"
|
||||
|
@ -15,6 +14,7 @@ import (
|
|||
"sync"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
|
||||
ini "gopkg.in/ini.v1"
|
||||
)
|
||||
|
@ -205,6 +205,7 @@ 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)
|
||||
|
|
|
@ -5,12 +5,12 @@
|
|||
package setting
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -53,6 +53,7 @@ func newSessionService() {
|
|||
SessionConfig.Maxlifetime = sec.Key("SESSION_LIFE_TIME").MustInt64(86400)
|
||||
SessionConfig.Domain = sec.Key("DOMAIN").String()
|
||||
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
shadowConfig, err := json.Marshal(SessionConfig)
|
||||
if err != nil {
|
||||
log.Fatal("Can't shadow session config: %v", err)
|
||||
|
|
|
@ -7,7 +7,6 @@ package setting
|
|||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
@ -28,6 +27,7 @@ import (
|
|||
"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"
|
||||
|
@ -1111,6 +1111,7 @@ func MakeManifestData(appName string, appURL string, absoluteAssetURL string) []
|
|||
Icons []manifestIcon `json:"icons"`
|
||||
}
|
||||
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
bytes, err := json.Marshal(&manifestJSON{
|
||||
Name: appName,
|
||||
ShortName: appName,
|
||||
|
|
|
@ -5,9 +5,9 @@
|
|||
package setting
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
@ -28,5 +28,6 @@ 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))
|
||||
}
|
||||
|
|
|
@ -5,8 +5,9 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
// Mappable represents an interface that can MapTo another interface
|
||||
|
@ -19,6 +20,7 @@ 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)) {
|
||||
|
|
|
@ -6,10 +6,11 @@
|
|||
package structs
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -138,12 +139,14 @@ func (p *CreatePayload) SetSecret(secret string) {
|
|||
|
||||
// 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
|
||||
}
|
||||
|
@ -193,6 +196,7 @@ func (p *DeletePayload) SetSecret(secret string) {
|
|||
|
||||
// JSONPayload implements Payload
|
||||
func (p *DeletePayload) JSONPayload() ([]byte, error) {
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
return json.MarshalIndent(p, "", " ")
|
||||
}
|
||||
|
||||
|
@ -218,6 +222,7 @@ func (p *ForkPayload) SetSecret(secret string) {
|
|||
|
||||
// JSONPayload implements Payload
|
||||
func (p *ForkPayload) JSONPayload() ([]byte, error) {
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
return json.MarshalIndent(p, "", " ")
|
||||
}
|
||||
|
||||
|
@ -250,6 +255,7 @@ func (p *IssueCommentPayload) SetSecret(secret string) {
|
|||
|
||||
// JSONPayload implements Payload
|
||||
func (p *IssueCommentPayload) JSONPayload() ([]byte, error) {
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
return json.MarshalIndent(p, "", " ")
|
||||
}
|
||||
|
||||
|
@ -286,6 +292,7 @@ func (p *ReleasePayload) SetSecret(secret string) {
|
|||
|
||||
// JSONPayload implements Payload
|
||||
func (p *ReleasePayload) JSONPayload() ([]byte, error) {
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
return json.MarshalIndent(p, "", " ")
|
||||
}
|
||||
|
||||
|
@ -317,12 +324,14 @@ func (p *PushPayload) SetSecret(secret string) {
|
|||
|
||||
// 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
|
||||
}
|
||||
|
@ -396,6 +405,7 @@ func (p *IssuePayload) SetSecret(secret string) {
|
|||
|
||||
// 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, "", " ")
|
||||
}
|
||||
|
||||
|
@ -437,6 +447,7 @@ func (p *PullRequestPayload) SetSecret(secret string) {
|
|||
|
||||
// JSONPayload FIXME
|
||||
func (p *PullRequestPayload) JSONPayload() ([]byte, error) {
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
return json.MarshalIndent(p, "", " ")
|
||||
}
|
||||
|
||||
|
@ -479,5 +490,6 @@ func (p *RepositoryPayload) SetSecret(secret string) {
|
|||
|
||||
// JSONPayload JSON representation of the payload
|
||||
func (p *RepositoryPayload) JSONPayload() ([]byte, error) {
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
return json.MarshalIndent(p, "", " ")
|
||||
}
|
||||
|
|
|
@ -5,8 +5,9 @@
|
|||
package structs
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
// User represents a user
|
||||
|
@ -38,6 +39,7 @@ 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"`
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
package task
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
|
@ -16,6 +15,7 @@ import (
|
|||
repo_module "code.gitea.io/gitea/modules/repository"
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
// taskQueue is a global queue of tasks
|
||||
|
@ -65,6 +65,7 @@ func MigrateRepository(doer, u *models.User, opts base.MigrateOptions) error {
|
|||
|
||||
// CreateMigrateTask creates a migrate task
|
||||
func CreateMigrateTask(doer, u *models.User, opts base.MigrateOptions) (*models.Task, error) {
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
bs, err := json.Marshal(&opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
@ -38,6 +38,7 @@ import (
|
|||
mirror_service "code.gitea.io/gitea/services/mirror"
|
||||
|
||||
"github.com/editorconfig/editorconfig-core-go/v2"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
// Used from static.go && dynamic.go
|
||||
|
@ -45,6 +46,7 @@ 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())
|
||||
|
@ -215,7 +217,7 @@ func NewFuncMap() []template.FuncMap {
|
|||
return fmt.Sprintf("%f", float64(adds)/(float64(adds)+float64(dels))*100)
|
||||
},
|
||||
"Json": func(in interface{}) string {
|
||||
out, err := json.Marshal(in)
|
||||
out, err := jsonED.Marshal(in)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
@ -814,6 +816,8 @@ func ActionIcon(opType models.ActionType) string {
|
|||
// ActionContent2Commits converts action content to push commits
|
||||
func ActionContent2Commits(act Actioner) *repository.PushCommits {
|
||||
push := repository.NewPushCommits()
|
||||
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
if err := json.Unmarshal([]byte(act.GetContent()), push); err != nil {
|
||||
log.Error("json.Unmarshal:\n%s\nERROR: %v", act.GetContent(), err)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue