From c7a13de0a674abc51f49877c0c1c9fbd731efdbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Fri, 1 Dec 2023 20:15:11 +0800 Subject: [PATCH] Use contextjson --- cmd/sing-box/cmd_geosite_export.go | 2 +- common/badjsonmerge/merge.go | 2 +- common/json/context.go | 21 +++++++++++++++++++++ common/json/std.go | 3 +++ experimental/libbox/config.go | 2 +- option/debug.go | 3 +-- option/inbound.go | 2 +- option/outbound.go | 2 +- option/rule.go | 2 +- option/rule_dns.go | 2 +- option/rule_set.go | 6 +++--- option/tls_acme.go | 2 +- option/v2ray_transport.go | 2 +- 13 files changed, 37 insertions(+), 14 deletions(-) create mode 100644 common/json/context.go diff --git a/cmd/sing-box/cmd_geosite_export.go b/cmd/sing-box/cmd_geosite_export.go index 71f1018d..18163dae 100644 --- a/cmd/sing-box/cmd_geosite_export.go +++ b/cmd/sing-box/cmd_geosite_export.go @@ -1,11 +1,11 @@ package main import ( - "encoding/json" "io" "os" "github.com/sagernet/sing-box/common/geosite" + "github.com/sagernet/sing-box/common/json" C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" diff --git a/common/badjsonmerge/merge.go b/common/badjsonmerge/merge.go index 39635e66..9c928698 100644 --- a/common/badjsonmerge/merge.go +++ b/common/badjsonmerge/merge.go @@ -1,10 +1,10 @@ package badjsonmerge import ( - "encoding/json" "reflect" "github.com/sagernet/sing-box/common/badjson" + "github.com/sagernet/sing-box/common/json" "github.com/sagernet/sing-box/option" E "github.com/sagernet/sing/common/exceptions" ) diff --git a/common/json/context.go b/common/json/context.go new file mode 100644 index 00000000..8a8aba30 --- /dev/null +++ b/common/json/context.go @@ -0,0 +1,21 @@ +//go:build go1.21 && !without_contextjson + +package json + +import "github.com/sagernet/sing-box/common/contextjson" + +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 + RawMessage = json.RawMessage +) diff --git a/common/json/std.go b/common/json/std.go index edc3502b..e9280dcf 100644 --- a/common/json/std.go +++ b/common/json/std.go @@ -1,3 +1,5 @@ +//go:build !go1.21 || without_contextjson + package json import "encoding/json" @@ -15,4 +17,5 @@ type ( Token = json.Token Delim = json.Delim SyntaxError = json.SyntaxError + RawMessage = json.RawMessage ) diff --git a/experimental/libbox/config.go b/experimental/libbox/config.go index 66651b60..68fb5107 100644 --- a/experimental/libbox/config.go +++ b/experimental/libbox/config.go @@ -3,9 +3,9 @@ package libbox import ( "bytes" "context" - "encoding/json" "github.com/sagernet/sing-box" + "github.com/sagernet/sing-box/common/json" "github.com/sagernet/sing-box/option" E "github.com/sagernet/sing/common/exceptions" ) diff --git a/option/debug.go b/option/debug.go index 17fb05d0..7c9de09e 100644 --- a/option/debug.go +++ b/option/debug.go @@ -1,9 +1,8 @@ package option import ( - "encoding/json" - "github.com/sagernet/sing-box/common/humanize" + "github.com/sagernet/sing-box/common/json" ) type DebugOptions struct { diff --git a/option/inbound.go b/option/inbound.go index c61428ad..7fa5950c 100644 --- a/option/inbound.go +++ b/option/inbound.go @@ -114,7 +114,7 @@ func (h *Inbound) UnmarshalJSON(bytes []byte) error { } err = UnmarshallExcluded(bytes, (*_Inbound)(h), v) if err != nil { - return E.Cause(err, "inbound options") + return err } return nil } diff --git a/option/outbound.go b/option/outbound.go index 2985319e..654fe822 100644 --- a/option/outbound.go +++ b/option/outbound.go @@ -124,7 +124,7 @@ func (h *Outbound) UnmarshalJSON(bytes []byte) error { } err = UnmarshallExcluded(bytes, (*_Outbound)(h), v) if err != nil { - return E.Cause(err, "outbound options") + return err } return nil } diff --git a/option/rule.go b/option/rule.go index 1201d123..e75f6771 100644 --- a/option/rule.go +++ b/option/rule.go @@ -48,7 +48,7 @@ func (r *Rule) UnmarshalJSON(bytes []byte) error { } err = UnmarshallExcluded(bytes, (*_Rule)(r), v) if err != nil { - return E.Cause(err, "route rule") + return err } return nil } diff --git a/option/rule_dns.go b/option/rule_dns.go index 50d9e612..6075dd6d 100644 --- a/option/rule_dns.go +++ b/option/rule_dns.go @@ -48,7 +48,7 @@ func (r *DNSRule) UnmarshalJSON(bytes []byte) error { } err = UnmarshallExcluded(bytes, (*_DNSRule)(r), v) if err != nil { - return E.Cause(err, "dns route rule") + return err } return nil } diff --git a/option/rule_set.go b/option/rule_set.go index 7a6f7e92..da67a3ac 100644 --- a/option/rule_set.go +++ b/option/rule_set.go @@ -64,7 +64,7 @@ func (r *RuleSet) UnmarshalJSON(bytes []byte) error { } err = UnmarshallExcluded(bytes, (*_RuleSet)(r), v) if err != nil { - return E.Cause(err, "rule set") + return err } return nil } @@ -118,7 +118,7 @@ func (r *HeadlessRule) UnmarshalJSON(bytes []byte) error { } err = UnmarshallExcluded(bytes, (*_HeadlessRule)(r), v) if err != nil { - return E.Cause(err, "route rule-set rule") + return err } return nil } @@ -209,7 +209,7 @@ func (r *PlainRuleSetCompat) UnmarshalJSON(bytes []byte) error { } err = UnmarshallExcluded(bytes, (*_PlainRuleSetCompat)(r), v) if err != nil { - return E.Cause(err, "rule set") + return err } return nil } diff --git a/option/tls_acme.go b/option/tls_acme.go index 1068237e..46cdbd7d 100644 --- a/option/tls_acme.go +++ b/option/tls_acme.go @@ -62,7 +62,7 @@ func (o *ACMEDNS01ChallengeOptions) UnmarshalJSON(bytes []byte) error { } err = UnmarshallExcluded(bytes, (*_ACMEDNS01ChallengeOptions)(o), v) if err != nil { - return E.Cause(err, "DNS01 challenge options") + return err } return nil } diff --git a/option/v2ray_transport.go b/option/v2ray_transport.go index 63af28a3..2698476d 100644 --- a/option/v2ray_transport.go +++ b/option/v2ray_transport.go @@ -60,7 +60,7 @@ func (o *V2RayTransportOptions) UnmarshalJSON(bytes []byte) error { } err = UnmarshallExcluded(bytes, (*_V2RayTransportOptions)(o), v) if err != nil { - return E.Cause(err, "vmess transport options") + return err } return nil }