Use contextjson

This commit is contained in:
世界 2023-12-01 20:15:11 +08:00
parent 40ca0a3d95
commit bc5c601911
No known key found for this signature in database
GPG Key ID: CD109927C34A63C4
13 changed files with 37 additions and 14 deletions

View File

@ -1,11 +1,11 @@
package main package main
import ( import (
"encoding/json"
"io" "io"
"os" "os"
"github.com/sagernet/sing-box/common/geosite" "github.com/sagernet/sing-box/common/geosite"
"github.com/sagernet/sing-box/common/json"
C "github.com/sagernet/sing-box/constant" C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"

View File

@ -1,10 +1,10 @@
package badjsonmerge package badjsonmerge
import ( import (
"encoding/json"
"reflect" "reflect"
"github.com/sagernet/sing-box/common/badjson" "github.com/sagernet/sing-box/common/badjson"
"github.com/sagernet/sing-box/common/json"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
) )

21
common/json/context.go Normal file
View File

@ -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
)

View File

@ -1,3 +1,5 @@
//go:build !go1.21 || without_contextjson
package json package json
import "encoding/json" import "encoding/json"
@ -15,4 +17,5 @@ type (
Token = json.Token Token = json.Token
Delim = json.Delim Delim = json.Delim
SyntaxError = json.SyntaxError SyntaxError = json.SyntaxError
RawMessage = json.RawMessage
) )

View File

@ -3,9 +3,9 @@ package libbox
import ( import (
"bytes" "bytes"
"context" "context"
"encoding/json"
"github.com/sagernet/sing-box" "github.com/sagernet/sing-box"
"github.com/sagernet/sing-box/common/json"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
) )

View File

@ -1,9 +1,8 @@
package option package option
import ( import (
"encoding/json"
"github.com/sagernet/sing-box/common/humanize" "github.com/sagernet/sing-box/common/humanize"
"github.com/sagernet/sing-box/common/json"
) )
type DebugOptions struct { type DebugOptions struct {

View File

@ -114,7 +114,7 @@ func (h *Inbound) UnmarshalJSON(bytes []byte) error {
} }
err = UnmarshallExcluded(bytes, (*_Inbound)(h), v) err = UnmarshallExcluded(bytes, (*_Inbound)(h), v)
if err != nil { if err != nil {
return E.Cause(err, "inbound options") return err
} }
return nil return nil
} }

View File

@ -124,7 +124,7 @@ func (h *Outbound) UnmarshalJSON(bytes []byte) error {
} }
err = UnmarshallExcluded(bytes, (*_Outbound)(h), v) err = UnmarshallExcluded(bytes, (*_Outbound)(h), v)
if err != nil { if err != nil {
return E.Cause(err, "outbound options") return err
} }
return nil return nil
} }

View File

@ -48,7 +48,7 @@ func (r *Rule) UnmarshalJSON(bytes []byte) error {
} }
err = UnmarshallExcluded(bytes, (*_Rule)(r), v) err = UnmarshallExcluded(bytes, (*_Rule)(r), v)
if err != nil { if err != nil {
return E.Cause(err, "route rule") return err
} }
return nil return nil
} }

View File

@ -48,7 +48,7 @@ func (r *DNSRule) UnmarshalJSON(bytes []byte) error {
} }
err = UnmarshallExcluded(bytes, (*_DNSRule)(r), v) err = UnmarshallExcluded(bytes, (*_DNSRule)(r), v)
if err != nil { if err != nil {
return E.Cause(err, "dns route rule") return err
} }
return nil return nil
} }

View File

@ -64,7 +64,7 @@ func (r *RuleSet) UnmarshalJSON(bytes []byte) error {
} }
err = UnmarshallExcluded(bytes, (*_RuleSet)(r), v) err = UnmarshallExcluded(bytes, (*_RuleSet)(r), v)
if err != nil { if err != nil {
return E.Cause(err, "rule set") return err
} }
return nil return nil
} }
@ -118,7 +118,7 @@ func (r *HeadlessRule) UnmarshalJSON(bytes []byte) error {
} }
err = UnmarshallExcluded(bytes, (*_HeadlessRule)(r), v) err = UnmarshallExcluded(bytes, (*_HeadlessRule)(r), v)
if err != nil { if err != nil {
return E.Cause(err, "route rule-set rule") return err
} }
return nil return nil
} }
@ -209,7 +209,7 @@ func (r *PlainRuleSetCompat) UnmarshalJSON(bytes []byte) error {
} }
err = UnmarshallExcluded(bytes, (*_PlainRuleSetCompat)(r), v) err = UnmarshallExcluded(bytes, (*_PlainRuleSetCompat)(r), v)
if err != nil { if err != nil {
return E.Cause(err, "rule set") return err
} }
return nil return nil
} }

View File

@ -62,7 +62,7 @@ func (o *ACMEDNS01ChallengeOptions) UnmarshalJSON(bytes []byte) error {
} }
err = UnmarshallExcluded(bytes, (*_ACMEDNS01ChallengeOptions)(o), v) err = UnmarshallExcluded(bytes, (*_ACMEDNS01ChallengeOptions)(o), v)
if err != nil { if err != nil {
return E.Cause(err, "DNS01 challenge options") return err
} }
return nil return nil
} }

View File

@ -60,7 +60,7 @@ func (o *V2RayTransportOptions) UnmarshalJSON(bytes []byte) error {
} }
err = UnmarshallExcluded(bytes, (*_V2RayTransportOptions)(o), v) err = UnmarshallExcluded(bytes, (*_V2RayTransportOptions)(o), v)
if err != nil { if err != nil {
return E.Cause(err, "vmess transport options") return err
} }
return nil return nil
} }