From 67edc163cb21d7b1b952fa3f6d20449fca449f0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sun, 24 Jul 2022 17:58:52 +0800 Subject: [PATCH] Add with_std_json build tag --- cmd/sing-box/cmd_check.go | 2 +- cmd/sing-box/cmd_format.go | 2 +- cmd/sing-box/cmd_run.go | 2 +- common/badjson/array.go | 3 +-- common/badjson/json.go | 3 +-- common/badjson/object.go | 3 +-- common/json/gojson.go | 20 ++++++++++++++++++++ common/json/std.go | 20 ++++++++++++++++++++ experimental/clashapi/connections.go | 2 +- experimental/clashapi/server.go | 2 +- option/config.go | 3 +-- option/dns.go | 3 +-- option/inbound.go | 3 +-- option/json.go | 3 +-- option/outbound.go | 3 +-- option/route.go | 3 +-- option/types.go | 3 +-- 17 files changed, 55 insertions(+), 25 deletions(-) create mode 100644 common/json/gojson.go create mode 100644 common/json/std.go diff --git a/cmd/sing-box/cmd_check.go b/cmd/sing-box/cmd_check.go index e6d55d12..3360ea6c 100644 --- a/cmd/sing-box/cmd_check.go +++ b/cmd/sing-box/cmd_check.go @@ -5,10 +5,10 @@ import ( "os" "github.com/sagernet/sing-box" + "github.com/sagernet/sing-box/common/json" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" - "github.com/goccy/go-json" "github.com/spf13/cobra" ) diff --git a/cmd/sing-box/cmd_format.go b/cmd/sing-box/cmd_format.go index 9e51cd9d..ac778afb 100644 --- a/cmd/sing-box/cmd_format.go +++ b/cmd/sing-box/cmd_format.go @@ -5,10 +5,10 @@ import ( "os" "path/filepath" + "github.com/sagernet/sing-box/common/json" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" - "github.com/goccy/go-json" "github.com/spf13/cobra" ) diff --git a/cmd/sing-box/cmd_run.go b/cmd/sing-box/cmd_run.go index ff8581fc..c8a55dde 100644 --- a/cmd/sing-box/cmd_run.go +++ b/cmd/sing-box/cmd_run.go @@ -7,10 +7,10 @@ import ( "syscall" "github.com/sagernet/sing-box" + "github.com/sagernet/sing-box/common/json" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" - "github.com/goccy/go-json" "github.com/spf13/cobra" ) diff --git a/common/badjson/array.go b/common/badjson/array.go index 9f93acd4..a7042ca6 100644 --- a/common/badjson/array.go +++ b/common/badjson/array.go @@ -3,9 +3,8 @@ package badjson import ( "bytes" + "github.com/sagernet/sing-box/common/json" E "github.com/sagernet/sing/common/exceptions" - - "github.com/goccy/go-json" ) type JSONArray[T any] []T diff --git a/common/badjson/json.go b/common/badjson/json.go index 1c299227..7f00f09b 100644 --- a/common/badjson/json.go +++ b/common/badjson/json.go @@ -1,9 +1,8 @@ package badjson import ( + "github.com/sagernet/sing-box/common/json" E "github.com/sagernet/sing/common/exceptions" - - "github.com/goccy/go-json" ) func decodeJSON(decoder *json.Decoder) (any, error) { diff --git a/common/badjson/object.go b/common/badjson/object.go index eb0de894..dc62aed9 100644 --- a/common/badjson/object.go +++ b/common/badjson/object.go @@ -4,10 +4,9 @@ import ( "bytes" "strings" + "github.com/sagernet/sing-box/common/json" E "github.com/sagernet/sing/common/exceptions" "github.com/sagernet/sing/common/x/linkedhashmap" - - "github.com/goccy/go-json" ) type JSONObject struct { diff --git a/common/json/gojson.go b/common/json/gojson.go new file mode 100644 index 00000000..36c4b473 --- /dev/null +++ b/common/json/gojson.go @@ -0,0 +1,20 @@ +//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 new file mode 100644 index 00000000..d93b031b --- /dev/null +++ b/common/json/std.go @@ -0,0 +1,20 @@ +//go:build with_std_json + +package json + +import "encoding/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/experimental/clashapi/connections.go b/experimental/clashapi/connections.go index 5e1aa3a8..73f89059 100644 --- a/experimental/clashapi/connections.go +++ b/experimental/clashapi/connections.go @@ -2,11 +2,11 @@ package clashapi import ( "bytes" - "encoding/json" "net/http" "strconv" "time" + "github.com/sagernet/sing-box/common/json" "github.com/sagernet/sing-box/experimental/clashapi/trafficontrol" "github.com/go-chi/chi/v5" diff --git a/experimental/clashapi/server.go b/experimental/clashapi/server.go index f14bff5d..d7168019 100644 --- a/experimental/clashapi/server.go +++ b/experimental/clashapi/server.go @@ -11,6 +11,7 @@ import ( "time" "github.com/sagernet/sing-box/adapter" + "github.com/sagernet/sing-box/common/json" C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/experimental/clashapi/trafficontrol" "github.com/sagernet/sing-box/log" @@ -22,7 +23,6 @@ import ( "github.com/go-chi/chi/v5" "github.com/go-chi/cors" "github.com/go-chi/render" - "github.com/goccy/go-json" "github.com/gorilla/websocket" ) diff --git a/option/config.go b/option/config.go index 52672e11..c61e38a4 100644 --- a/option/config.go +++ b/option/config.go @@ -4,10 +4,9 @@ import ( "bytes" "strings" + "github.com/sagernet/sing-box/common/json" "github.com/sagernet/sing/common" E "github.com/sagernet/sing/common/exceptions" - - "github.com/goccy/go-json" ) type _Options struct { diff --git a/option/dns.go b/option/dns.go index a135cf5c..d42574e7 100644 --- a/option/dns.go +++ b/option/dns.go @@ -1,11 +1,10 @@ package option import ( + "github.com/sagernet/sing-box/common/json" C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing/common" E "github.com/sagernet/sing/common/exceptions" - - "github.com/goccy/go-json" ) type DNSOptions struct { diff --git a/option/inbound.go b/option/inbound.go index bc710166..eb52eb90 100644 --- a/option/inbound.go +++ b/option/inbound.go @@ -1,12 +1,11 @@ package option import ( + "github.com/sagernet/sing-box/common/json" C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing/common" "github.com/sagernet/sing/common/auth" E "github.com/sagernet/sing/common/exceptions" - - "github.com/goccy/go-json" ) type _Inbound struct { diff --git a/option/json.go b/option/json.go index f65c9d89..56a90860 100644 --- a/option/json.go +++ b/option/json.go @@ -4,10 +4,9 @@ import ( "bytes" "github.com/sagernet/sing-box/common/badjson" + "github.com/sagernet/sing-box/common/json" "github.com/sagernet/sing/common" E "github.com/sagernet/sing/common/exceptions" - - "github.com/goccy/go-json" ) func ToMap(v any) (*badjson.JSONObject, error) { diff --git a/option/outbound.go b/option/outbound.go index 74c9e40b..db0535de 100644 --- a/option/outbound.go +++ b/option/outbound.go @@ -1,12 +1,11 @@ package option import ( + "github.com/sagernet/sing-box/common/json" C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing/common" E "github.com/sagernet/sing/common/exceptions" M "github.com/sagernet/sing/common/metadata" - - "github.com/goccy/go-json" ) type _Outbound struct { diff --git a/option/route.go b/option/route.go index 8a942fa9..d7d33b8b 100644 --- a/option/route.go +++ b/option/route.go @@ -1,11 +1,10 @@ package option import ( + "github.com/sagernet/sing-box/common/json" C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing/common" E "github.com/sagernet/sing/common/exceptions" - - "github.com/goccy/go-json" ) type RouteOptions struct { diff --git a/option/types.go b/option/types.go index 978ee2ab..d5b8c997 100644 --- a/option/types.go +++ b/option/types.go @@ -5,11 +5,10 @@ import ( "strings" "time" + "github.com/sagernet/sing-box/common/json" C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-dns" E "github.com/sagernet/sing/common/exceptions" - - "github.com/goccy/go-json" ) type ListenAddress netip.Addr