Add with_std_json build tag

This commit is contained in:
世界 2022-07-24 17:58:52 +08:00
parent fe8e984608
commit 67edc163cb
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
17 changed files with 55 additions and 25 deletions

View file

@ -5,10 +5,10 @@ import (
"os" "os"
"github.com/sagernet/sing-box" "github.com/sagernet/sing-box"
"github.com/sagernet/sing-box/common/json"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
"github.com/goccy/go-json"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )

View file

@ -5,10 +5,10 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"github.com/sagernet/sing-box/common/json"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
"github.com/goccy/go-json"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )

View file

@ -7,10 +7,10 @@ import (
"syscall" "syscall"
"github.com/sagernet/sing-box" "github.com/sagernet/sing-box"
"github.com/sagernet/sing-box/common/json"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
"github.com/goccy/go-json"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )

View file

@ -3,9 +3,8 @@ package badjson
import ( import (
"bytes" "bytes"
"github.com/sagernet/sing-box/common/json"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
"github.com/goccy/go-json"
) )
type JSONArray[T any] []T type JSONArray[T any] []T

View file

@ -1,9 +1,8 @@
package badjson package badjson
import ( import (
"github.com/sagernet/sing-box/common/json"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
"github.com/goccy/go-json"
) )
func decodeJSON(decoder *json.Decoder) (any, error) { func decodeJSON(decoder *json.Decoder) (any, error) {

View file

@ -4,10 +4,9 @@ import (
"bytes" "bytes"
"strings" "strings"
"github.com/sagernet/sing-box/common/json"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/x/linkedhashmap" "github.com/sagernet/sing/common/x/linkedhashmap"
"github.com/goccy/go-json"
) )
type JSONObject struct { type JSONObject struct {

20
common/json/gojson.go Normal file
View file

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

20
common/json/std.go Normal file
View file

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

View file

@ -2,11 +2,11 @@ package clashapi
import ( import (
"bytes" "bytes"
"encoding/json"
"net/http" "net/http"
"strconv" "strconv"
"time" "time"
"github.com/sagernet/sing-box/common/json"
"github.com/sagernet/sing-box/experimental/clashapi/trafficontrol" "github.com/sagernet/sing-box/experimental/clashapi/trafficontrol"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"

View file

@ -11,6 +11,7 @@ import (
"time" "time"
"github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/adapter"
"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/experimental/clashapi/trafficontrol" "github.com/sagernet/sing-box/experimental/clashapi/trafficontrol"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
@ -22,7 +23,6 @@ import (
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/go-chi/cors" "github.com/go-chi/cors"
"github.com/go-chi/render" "github.com/go-chi/render"
"github.com/goccy/go-json"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )

View file

@ -4,10 +4,9 @@ import (
"bytes" "bytes"
"strings" "strings"
"github.com/sagernet/sing-box/common/json"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
"github.com/goccy/go-json"
) )
type _Options struct { type _Options struct {

View file

@ -1,11 +1,10 @@
package option package option
import ( import (
"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/common" "github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
"github.com/goccy/go-json"
) )
type DNSOptions struct { type DNSOptions struct {

View file

@ -1,12 +1,11 @@
package option package option
import ( import (
"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/common" "github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/auth" "github.com/sagernet/sing/common/auth"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
"github.com/goccy/go-json"
) )
type _Inbound struct { type _Inbound struct {

View file

@ -4,10 +4,9 @@ import (
"bytes" "bytes"
"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/common" "github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
"github.com/goccy/go-json"
) )
func ToMap(v any) (*badjson.JSONObject, error) { func ToMap(v any) (*badjson.JSONObject, error) {

View file

@ -1,12 +1,11 @@
package option package option
import ( import (
"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/common" "github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata" M "github.com/sagernet/sing/common/metadata"
"github.com/goccy/go-json"
) )
type _Outbound struct { type _Outbound struct {

View file

@ -1,11 +1,10 @@
package option package option
import ( import (
"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/common" "github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
"github.com/goccy/go-json"
) )
type RouteOptions struct { type RouteOptions struct {

View file

@ -5,11 +5,10 @@ import (
"strings" "strings"
"time" "time"
"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-dns" "github.com/sagernet/sing-dns"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
"github.com/goccy/go-json"
) )
type ListenAddress netip.Addr type ListenAddress netip.Addr