Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion common/reflect/marshal.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package reflect

import (
"bytes"
"encoding/json"
"fmt"
"reflect"
Expand All @@ -13,13 +14,22 @@ import (

func MarshalToJson(v interface{}, insertTypeInfo bool) (string, bool) {
if itf := marshalInterface(v, true, insertTypeInfo); itf != nil {
if b, err := json.MarshalIndent(itf, "", " "); err == nil {
if b, err := JSONMarshalWithoutEscape(itf); err == nil {
return string(b[:]), true
}
}
return "", false
}

func JSONMarshalWithoutEscape(t interface{}) ([]byte, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetIndent("", " ")
encoder.SetEscapeHTML(false)
err := encoder.Encode(t)
return buffer.Bytes(), err
}

func marshalTypedMessage(v *cserial.TypedMessage, ignoreNullValue bool, insertTypeInfo bool) interface{} {
if v == nil {
return nil
Expand Down
14 changes: 5 additions & 9 deletions main/commands/all/api/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
"time"

"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/encoding/protojson"

"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/main/commands/base"
creflect "github.com/xtls/xray-core/common/reflect"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -107,20 +107,16 @@ func fetchHTTPContent(target string) ([]byte, error) {
return content, nil
}

func protoToJSONString(m proto.Message, prefix, indent string) (string, error) {
return strings.TrimSpace(protojson.MarshalOptions{Indent: indent}.Format(m)), nil
}

func showJSONResponse(m proto.Message) {
if isNil(m) {
return
}
output, err := protoToJSONString(m, "", " ")
if err != nil {
if j, ok := creflect.MarshalToJson(m, true); ok {
fmt.Println(j)
} else {
fmt.Fprintf(os.Stdout, "%v\n", m)
base.Fatalf("error encode json: %s", err)
base.Fatalf("error encode json")
}
fmt.Println(output)
}

func isNil(i interface{}) bool {
Expand Down