mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-29 16:58:34 +00:00
Commands: Use creflect.MarshalToJson() as output (#3674)
This commit is contained in:
parent
498d8eb3cc
commit
f0547bc04d
2 changed files with 16 additions and 10 deletions
|
@ -1,6 +1,7 @@
|
|||
package reflect
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue