Remove go-json

This commit is contained in:
世界 2022-07-26 13:53:25 +08:00
parent 75508bccb5
commit 0347a7c038
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
10 changed files with 14 additions and 40 deletions

View file

@ -2,6 +2,7 @@ package badjson
import ( import (
"bytes" "bytes"
"reflect"
"github.com/sagernet/sing-box/common/json" "github.com/sagernet/sing-box/common/json"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
@ -36,11 +37,15 @@ func (a *JSONArray[T]) UnmarshalJSON(content []byte) error {
func (a *JSONArray[T]) decodeJSON(decoder *json.Decoder) error { func (a *JSONArray[T]) decodeJSON(decoder *json.Decoder) error {
for decoder.More() { for decoder.More() {
var item T value, err := decodeJSON(decoder)
err := decoder.Decode(&item)
if err != nil { if err != nil {
return err return err
} }
item, ok := value.(T)
if !ok {
var defValue T
return E.New("can't cast ", value, " to ", reflect.TypeOf(defValue))
}
*a = append(*a, item) *a = append(*a, item)
} }
return nil return nil

View file

@ -49,7 +49,7 @@ func (m *JSONObject) UnmarshalJSON(content []byte) error {
} }
err = m.decodeJSON(decoder) err = m.decodeJSON(decoder)
if err != nil { if err != nil {
return err return E.Cause(err, "decode json object content")
} }
objectEnd, err := decoder.Token() objectEnd, err := decoder.Token()
if err != nil { if err != nil {
@ -63,14 +63,15 @@ func (m *JSONObject) UnmarshalJSON(content []byte) error {
func (m *JSONObject) decodeJSON(decoder *json.Decoder) error { func (m *JSONObject) decodeJSON(decoder *json.Decoder) error {
for decoder.More() { for decoder.More() {
var entryKey string var entryKey string
err := decoder.Decode(&entryKey) keyToken, err := decoder.Token()
if err != nil { if err != nil {
return err return err
} }
entryKey = keyToken.(string)
var entryValue any var entryValue any
entryValue, err = decodeJSON(decoder) entryValue, err = decodeJSON(decoder)
if err != nil { if err != nil {
return err return E.Cause(err, "decode value for ", entryKey)
} }
m.Put(entryKey, entryValue) m.Put(entryKey, entryValue)
} }

View file

@ -1,20 +0,0 @@
//go:build !with_std_json
package json
import "github.com/goccy/go-json"
var (
Marshal = json.Marshal
Unmarshal = json.Unmarshal
NewEncoder = json.NewEncoder
NewDecoder = json.NewDecoder
)
type (
Encoder = json.Encoder
Decoder = json.Decoder
Token = json.Token
Delim = json.Delim
SyntaxError = json.SyntaxError
)

View file

@ -1,5 +1,3 @@
//go:build with_std_json
package json package json
import "encoding/json" import "encoding/json"

1
go.mod
View file

@ -8,7 +8,6 @@ require (
github.com/go-chi/chi/v5 v5.0.7 github.com/go-chi/chi/v5 v5.0.7
github.com/go-chi/cors v1.2.1 github.com/go-chi/cors v1.2.1
github.com/go-chi/render v1.0.1 github.com/go-chi/render v1.0.1
github.com/goccy/go-json v0.9.10
github.com/gofrs/uuid v4.2.0+incompatible github.com/gofrs/uuid v4.2.0+incompatible
github.com/gorilla/websocket v1.5.0 github.com/gorilla/websocket v1.5.0
github.com/logrusorgru/aurora v2.0.3+incompatible github.com/logrusorgru/aurora v2.0.3+incompatible

2
go.sum
View file

@ -40,8 +40,6 @@ github.com/go-chi/render v1.0.1/go.mod h1:pq4Rr7HbnsdaeHagklXub+p6Wd16Af5l9koip1
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q= github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/goccy/go-json v0.9.10 h1:hCeNmprSNLB8B8vQKWl6DpuH0t60oEs+TAk9a7CScKc=
github.com/goccy/go-json v0.9.10/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0= github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0=
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=

View file

@ -58,12 +58,10 @@ func (r *DNSRule) UnmarshalJSON(bytes []byte) error {
if err != nil { if err != nil {
return err return err
} }
if r.Type == "" {
r.Type = C.RuleTypeDefault
}
var v any var v any
switch r.Type { switch r.Type {
case C.RuleTypeDefault: case "", C.RuleTypeDefault:
r.Type = C.RuleTypeDefault
v = &r.DefaultOptions v = &r.DefaultOptions
case C.RuleTypeLogical: case C.RuleTypeLogical:
v = &r.LogicalOptions v = &r.LogicalOptions

View file

@ -35,9 +35,7 @@ func MergeObjects(objects ...any) (*badjson.JSONObject, error) {
} }
func MarshallObjects(objects ...any) ([]byte, error) { func MarshallObjects(objects ...any) ([]byte, error) {
objects = common.Filter(objects, func(v any) bool { objects = common.FilterNotNil(objects)
return v != nil
})
if len(objects) == 1 { if len(objects) == 1 {
return json.Marshal(objects[0]) return json.Marshal(objects[0])
} }

View file

@ -28,7 +28,6 @@ require (
github.com/go-chi/cors v1.2.1 // indirect github.com/go-chi/cors v1.2.1 // indirect
github.com/go-chi/render v1.0.1 // indirect github.com/go-chi/render v1.0.1 // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/goccy/go-json v0.9.10 // indirect
github.com/gogo/protobuf v1.3.2 // indirect github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/btree v1.0.1 // indirect github.com/google/btree v1.0.1 // indirect
github.com/gorilla/websocket v1.5.0 // indirect github.com/gorilla/websocket v1.5.0 // indirect

View file

@ -53,8 +53,6 @@ github.com/go-chi/render v1.0.1/go.mod h1:pq4Rr7HbnsdaeHagklXub+p6Wd16Af5l9koip1
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q= github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/goccy/go-json v0.9.10 h1:hCeNmprSNLB8B8vQKWl6DpuH0t60oEs+TAk9a7CScKc=
github.com/goccy/go-json v0.9.10/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0= github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0=
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=