File tree 2 files changed +16
-10
lines changed
2 files changed +16
-10
lines changed Original file line number Diff line number Diff line change 1
1
package reflect
2
2
3
3
import (
4
+ "bytes"
4
5
"encoding/json"
5
6
"fmt"
6
7
"reflect"
@@ -13,13 +14,22 @@ import (
13
14
14
15
func MarshalToJson (v interface {}, insertTypeInfo bool ) (string , bool ) {
15
16
if itf := marshalInterface (v , true , insertTypeInfo ); itf != nil {
16
- if b , err := json . MarshalIndent (itf , "" , " " ); err == nil {
17
+ if b , err := JSONMarshalWithoutEscape (itf ); err == nil {
17
18
return string (b [:]), true
18
19
}
19
20
}
20
21
return "" , false
21
22
}
22
23
24
+ func JSONMarshalWithoutEscape (t interface {}) ([]byte , error ) {
25
+ buffer := & bytes.Buffer {}
26
+ encoder := json .NewEncoder (buffer )
27
+ encoder .SetIndent ("" , " " )
28
+ encoder .SetEscapeHTML (false )
29
+ err := encoder .Encode (t )
30
+ return buffer .Bytes (), err
31
+ }
32
+
23
33
func marshalTypedMessage (v * cserial.TypedMessage , ignoreNullValue bool , insertTypeInfo bool ) interface {} {
24
34
if v == nil {
25
35
return nil
Original file line number Diff line number Diff line change @@ -13,10 +13,10 @@ import (
13
13
"time"
14
14
15
15
"google.golang.org/grpc/credentials/insecure"
16
- "google.golang.org/protobuf/encoding/protojson"
17
16
18
17
"github.com/xtls/xray-core/common/buf"
19
18
"github.com/xtls/xray-core/main/commands/base"
19
+ creflect "github.com/xtls/xray-core/common/reflect"
20
20
"google.golang.org/grpc"
21
21
"google.golang.org/protobuf/proto"
22
22
)
@@ -107,20 +107,16 @@ func fetchHTTPContent(target string) ([]byte, error) {
107
107
return content , nil
108
108
}
109
109
110
- func protoToJSONString (m proto.Message , prefix , indent string ) (string , error ) {
111
- return strings .TrimSpace (protojson.MarshalOptions {Indent : indent }.Format (m )), nil
112
- }
113
-
114
110
func showJSONResponse (m proto.Message ) {
115
111
if isNil (m ) {
116
112
return
117
113
}
118
- output , err := protoToJSONString (m , "" , " " )
119
- if err != nil {
114
+ if j , ok := creflect .MarshalToJson (m , true ); ok {
115
+ fmt .Println (j )
116
+ } else {
120
117
fmt .Fprintf (os .Stdout , "%v\n " , m )
121
- base .Fatalf ("error encode json: %s" , err )
118
+ base .Fatalf ("error encode json" )
122
119
}
123
- fmt .Println (output )
124
120
}
125
121
126
122
func isNil (i interface {}) bool {
You can’t perform that action at this time.
0 commit comments