Commands: Use creflect.MarshalToJson() as output (#3674)

This commit is contained in:
yuhan6665 2024-08-08 00:41:19 -04:00 committed by mmmray
parent 498d8eb3cc
commit f0547bc04d
2 changed files with 16 additions and 10 deletions

View file

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