️ 使用 json-iterator 替换 encoding/json 进行一些序列化/反序列化操作

This commit is contained in:
naiba
2022-03-18 23:13:22 +08:00
parent d928f65052
commit 3ca23d8d88
14 changed files with 46 additions and 37 deletions

View File

@@ -1,9 +1,9 @@
package model
import (
"encoding/json"
"time"
"github.com/naiba/nezha/pkg/utils"
"gorm.io/gorm"
)
@@ -27,7 +27,7 @@ type AlertRule struct {
}
func (r *AlertRule) BeforeSave(tx *gorm.DB) error {
data, err := json.Marshal(r.Rules)
data, err := utils.Json.Marshal(r.Rules)
if err != nil {
return err
}
@@ -36,7 +36,7 @@ func (r *AlertRule) BeforeSave(tx *gorm.DB) error {
}
func (r *AlertRule) AfterFind(tx *gorm.DB) error {
return json.Unmarshal([]byte(r.RulesRaw), &r.Rules)
return utils.Json.Unmarshal([]byte(r.RulesRaw), &r.Rules)
}
func (r *AlertRule) Enabled() bool {

View File

@@ -1,9 +1,9 @@
package model
import (
"encoding/json"
"time"
"github.com/naiba/nezha/pkg/utils"
"github.com/robfig/cron/v3"
"gorm.io/gorm"
)
@@ -29,5 +29,5 @@ type Cron struct {
}
func (c *Cron) AfterFind(tx *gorm.DB) error {
return json.Unmarshal([]byte(c.ServersRaw), &c.Servers)
return utils.Json.Unmarshal([]byte(c.ServersRaw), &c.Servers)
}

View File

@@ -1,9 +1,9 @@
package model
import (
"encoding/json"
"fmt"
"github.com/naiba/nezha/pkg/utils"
pb "github.com/naiba/nezha/proto"
"github.com/robfig/cron/v3"
"gorm.io/gorm"
@@ -66,7 +66,7 @@ func (m *Monitor) CronSpec() string {
func (m *Monitor) AfterFind(tx *gorm.DB) error {
var skipServers []uint64
if err := json.Unmarshal([]byte(m.SkipServersRaw), &skipServers); err != nil {
if err := utils.Json.Unmarshal([]byte(m.SkipServersRaw), &skipServers); err != nil {
return err
}
m.SkipServers = make(map[uint64]bool)
@@ -82,7 +82,7 @@ func IsServiceSentinelNeeded(t uint64) bool {
func (m *Monitor) InitSkipServers() error {
var skipServers []uint64
if err := json.Unmarshal([]byte(m.SkipServersRaw), &skipServers); err != nil {
if err := utils.Json.Unmarshal([]byte(m.SkipServersRaw), &skipServers); err != nil {
return err
}
m.SkipServers = make(map[uint64]bool)

View File

@@ -2,7 +2,6 @@ package model
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
@@ -10,6 +9,8 @@ import (
"net/url"
"strings"
"time"
"github.com/naiba/nezha/pkg/utils"
)
const (
@@ -58,12 +59,12 @@ func (n *Notification) reqBody(message string) (string, error) {
switch n.RequestType {
case NotificationRequestTypeJSON:
return replaceParamsInString(n.RequestBody, message, func(msg string) string {
msgBytes, _ := json.Marshal(msg)
msgBytes, _ := utils.Json.Marshal(msg)
return string(msgBytes)[1 : len(msgBytes)-1]
}), nil
case NotificationRequestTypeForm:
var data map[string]string
if err := json.Unmarshal([]byte(n.RequestBody), &data); err != nil {
if err := utils.Json.Unmarshal([]byte(n.RequestBody), &data); err != nil {
return "", err
}
params := url.Values{}
@@ -91,7 +92,7 @@ func (n *Notification) setRequestHeader(req *http.Request) error {
return nil
}
var m map[string]string
if err := json.Unmarshal([]byte(n.RequestHeader), &m); err != nil {
if err := utils.Json.Unmarshal([]byte(n.RequestHeader), &m); err != nil {
return err
}
for k, v := range m {

View File

@@ -1,11 +1,11 @@
package model
import (
"encoding/json"
"fmt"
"html/template"
"time"
"github.com/naiba/nezha/pkg/utils"
pb "github.com/naiba/nezha/proto"
)
@@ -39,9 +39,9 @@ func (s *Server) CopyFromRunningServer(old *Server) {
}
func (s Server) Marshal() template.JS {
name, _ := json.Marshal(s.Name)
tag, _ := json.Marshal(s.Tag)
note, _ := json.Marshal(s.Note)
secret, _ := json.Marshal(s.Secret)
name, _ := utils.Json.Marshal(s.Name)
tag, _ := utils.Json.Marshal(s.Tag)
note, _ := utils.Json.Marshal(s.Note)
secret, _ := utils.Json.Marshal(s.Secret)
return template.JS(fmt.Sprintf(`{"ID":%d,"Name":%s,"Secret":%s,"DisplayIndex":%d,"Tag":%s,"Note":%s}`, s.ID, name, secret, s.DisplayIndex, tag, note)) // #nosec
}

View File

@@ -1,9 +1,9 @@
package model
import (
"encoding/json"
"testing"
"github.com/naiba/nezha/pkg/utils"
"github.com/stretchr/testify/assert"
)
@@ -21,7 +21,7 @@ func TestServerMarshal(t *testing.T) {
}
serverStr := string(server.Marshal())
var serverRestore Server
assert.Nil(t, json.Unmarshal([]byte(serverStr), &serverRestore))
assert.Nil(t, utils.Json.Unmarshal([]byte(serverStr), &serverRestore))
assert.Equal(t, server, serverRestore)
}
}