️ 使用 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

@@ -3,7 +3,6 @@ package main
import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
@@ -402,7 +401,7 @@ func handleTerminalTask(task *pb.Task) {
return
}
var terminal model.TerminalTask
err := json.Unmarshal([]byte(task.GetData()), &terminal)
err := utils.Json.Unmarshal([]byte(task.GetData()), &terminal)
if err != nil {
println("Terminal 任务解析错误:", err)
return
@@ -470,7 +469,7 @@ func handleTerminalTask(task *pb.Task) {
case 0:
io.Copy(tty, reader)
case 1:
decoder := json.NewDecoder(reader)
decoder := utils.Json.NewDecoder(reader)
var resizeMessage WindowSize
err := decoder.Decode(&resizeMessage)
if err != nil {

View File

@@ -1,7 +1,6 @@
package monitor
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
@@ -69,7 +68,7 @@ func fetchGeoIP(servers []string, isV6 bool) geoIP {
continue
}
resp.Body.Close()
err = json.Unmarshal(body, &ip)
err = utils.Json.Unmarshal(body, &ip)
if err != nil {
continue
}

View File

@@ -1,7 +1,6 @@
package controller
import (
"encoding/json"
"errors"
"fmt"
"log"
@@ -16,6 +15,7 @@ import (
"github.com/naiba/nezha/model"
"github.com/naiba/nezha/pkg/mygin"
"github.com/naiba/nezha/pkg/utils"
"github.com/naiba/nezha/proto"
"github.com/naiba/nezha/service/singleton"
)
@@ -140,16 +140,23 @@ func (cp *commonPage) ws(c *gin.Context) {
return
}
defer conn.Close()
var servers []*model.Server
var bytesToWrite []byte
count := 0
for {
singleton.SortedServerLock.RLock()
servers = singleton.SortedServerList
singleton.SortedServerLock.RUnlock()
err = conn.WriteJSON(Data{
bytesToWrite, err = utils.Json.Marshal(Data{
Now: time.Now().Unix() * 1000,
Servers: servers,
Servers: singleton.SortedServerList,
})
singleton.SortedServerLock.RUnlock()
if err != nil {
break
}
writer, err := conn.NextWriter(websocket.TextMessage)
if err != nil {
break
}
_, err = writer.Write(bytesToWrite)
if err != nil {
break
}
@@ -241,7 +248,7 @@ func (cp *commonPage) terminal(c *gin.Context) {
return
}
terminalData, _ := json.Marshal(&model.TerminalTask{
terminalData, _ := utils.Json.Marshal(&model.TerminalTask{
Host: terminal.host,
UseSSL: terminal.useSSL,
Session: terminalID,

View File

@@ -2,7 +2,6 @@ package controller
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
@@ -281,7 +280,7 @@ func (ma *memberAPI) addOrEditCron(c *gin.Context) {
cr.PushSuccessful = cf.PushSuccessful == "on"
cr.ID = cf.ID
cr.Cover = cf.Cover
err = json.Unmarshal([]byte(cf.ServersRaw), &cr.Servers)
err = utils.Json.Unmarshal([]byte(cf.ServersRaw), &cr.Servers)
}
tx := singleton.DB.Begin()
if err == nil {
@@ -433,7 +432,7 @@ func (ma *memberAPI) addOrEditAlertRule(c *gin.Context) {
var r model.AlertRule
err := c.ShouldBindJSON(&arf)
if err == nil {
err = json.Unmarshal([]byte(arf.RulesRaw), &r.Rules)
err = utils.Json.Unmarshal([]byte(arf.RulesRaw), &r.Rules)
}
if err == nil {
if len(r.Rules) == 0 {