diff --git a/common/badjson/array.go b/common/badjson/array.go index a7042ca6..f64b8b95 100644 --- a/common/badjson/array.go +++ b/common/badjson/array.go @@ -2,6 +2,7 @@ package badjson import ( "bytes" + "reflect" "github.com/sagernet/sing-box/common/json" 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 { for decoder.More() { - var item T - err := decoder.Decode(&item) + value, err := decodeJSON(decoder) if err != nil { 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) } return nil diff --git a/common/badjson/object.go b/common/badjson/object.go index dc62aed9..ca555971 100644 --- a/common/badjson/object.go +++ b/common/badjson/object.go @@ -49,7 +49,7 @@ func (m *JSONObject) UnmarshalJSON(content []byte) error { } err = m.decodeJSON(decoder) if err != nil { - return err + return E.Cause(err, "decode json object content") } objectEnd, err := decoder.Token() if err != nil { @@ -63,14 +63,15 @@ func (m *JSONObject) UnmarshalJSON(content []byte) error { func (m *JSONObject) decodeJSON(decoder *json.Decoder) error { for decoder.More() { var entryKey string - err := decoder.Decode(&entryKey) + keyToken, err := decoder.Token() if err != nil { return err } + entryKey = keyToken.(string) var entryValue any entryValue, err = decodeJSON(decoder) if err != nil { - return err + return E.Cause(err, "decode value for ", entryKey) } m.Put(entryKey, entryValue) } diff --git a/common/json/gojson.go b/common/json/gojson.go deleted file mode 100644 index 36c4b473..00000000 --- a/common/json/gojson.go +++ /dev/null @@ -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 -) diff --git a/common/json/std.go b/common/json/std.go index d93b031b..edc3502b 100644 --- a/common/json/std.go +++ b/common/json/std.go @@ -1,5 +1,3 @@ -//go:build with_std_json - package json import "encoding/json" diff --git a/go.mod b/go.mod index 91a7e4c3..01b2da76 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,6 @@ require ( github.com/go-chi/chi/v5 v5.0.7 github.com/go-chi/cors v1.2.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/gorilla/websocket v1.5.0 github.com/logrusorgru/aurora v2.0.3+incompatible diff --git a/go.sum b/go.sum index 57a1ccc1..baee45e5 100644 --- a/go.sum +++ b/go.sum @@ -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-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/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/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= diff --git a/option/dns.go b/option/dns.go index a726f7fa..20e08c5a 100644 --- a/option/dns.go +++ b/option/dns.go @@ -58,12 +58,10 @@ func (r *DNSRule) UnmarshalJSON(bytes []byte) error { if err != nil { return err } - if r.Type == "" { - r.Type = C.RuleTypeDefault - } var v any switch r.Type { - case C.RuleTypeDefault: + case "", C.RuleTypeDefault: + r.Type = C.RuleTypeDefault v = &r.DefaultOptions case C.RuleTypeLogical: v = &r.LogicalOptions diff --git a/option/json.go b/option/json.go index 56a90860..d010da32 100644 --- a/option/json.go +++ b/option/json.go @@ -35,9 +35,7 @@ func MergeObjects(objects ...any) (*badjson.JSONObject, error) { } func MarshallObjects(objects ...any) ([]byte, error) { - objects = common.Filter(objects, func(v any) bool { - return v != nil - }) + objects = common.FilterNotNil(objects) if len(objects) == 1 { return json.Marshal(objects[0]) } diff --git a/test/go.mod b/test/go.mod index 844728b8..b7847a9c 100644 --- a/test/go.mod +++ b/test/go.mod @@ -28,7 +28,6 @@ require ( github.com/go-chi/cors v1.2.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/goccy/go-json v0.9.10 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/google/btree v1.0.1 // indirect github.com/gorilla/websocket v1.5.0 // indirect diff --git a/test/go.sum b/test/go.sum index 9628905d..faa114bb 100644 --- a/test/go.sum +++ b/test/go.sum @@ -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-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/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/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=