Compare commits
1 Commits
timeout
...
fix-stringify
| Author | SHA1 | Date | |
|---|---|---|---|
| b1c1d1d20c |
+23
-3
@@ -6,6 +6,7 @@ import (
|
|||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
"github.com/modern-go/reflect2"
|
"github.com/modern-go/reflect2"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"math"
|
"math"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -332,9 +333,28 @@ func (decoder *nullableFuzzyFloat64Decoder) Decode(ptr unsafe.Pointer, iter *jso
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Stringify(m interface{}) string {
|
func Stringify(a interface{}) string {
|
||||||
byt, _ := json.Marshal(m)
|
switch v := a.(type) {
|
||||||
return string(byt)
|
case *string:
|
||||||
|
return StringValue(v)
|
||||||
|
case string:
|
||||||
|
return v
|
||||||
|
case []byte:
|
||||||
|
return string(v)
|
||||||
|
case io.Reader:
|
||||||
|
byt, err := ioutil.ReadAll(v)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return string(byt)
|
||||||
|
}
|
||||||
|
byt := bytes.NewBuffer([]byte{})
|
||||||
|
jsonEncoder := json.NewEncoder(byt)
|
||||||
|
jsonEncoder.SetEscapeHTML(false)
|
||||||
|
if err := jsonEncoder.Encode(a); err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return string(bytes.TrimSpace(byt.Bytes()))
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseJSON(a string) interface{} {
|
func ParseJSON(a string) interface{} {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package dara
|
|||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/alibabacloud-go/tea/utils"
|
"github.com/alibabacloud-go/tea/utils"
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
@@ -878,3 +879,42 @@ func TestUnmarshalWithDefaultDecoders(t *testing.T) {
|
|||||||
err = jsoniter.Unmarshal(from, toUint64)
|
err = jsoniter.Unmarshal(from, toUint64)
|
||||||
utils.AssertNotNil(t, err)
|
utils.AssertNotNil(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_Stringify(t *testing.T) {
|
||||||
|
// interface
|
||||||
|
str := Stringify(map[string]interface{}{"test": "ok"})
|
||||||
|
utils.AssertEqual(t, `{"test":"ok"}`, str)
|
||||||
|
// string
|
||||||
|
str = Stringify("test")
|
||||||
|
utils.AssertEqual(t, "test", str)
|
||||||
|
// []byte
|
||||||
|
str = Stringify([]byte("test"))
|
||||||
|
utils.AssertEqual(t, "test", str)
|
||||||
|
// io.Reader
|
||||||
|
str = Stringify(strings.NewReader("test"))
|
||||||
|
utils.AssertEqual(t, "test", str)
|
||||||
|
|
||||||
|
str = Stringify("test")
|
||||||
|
utils.AssertEqual(t, "test", str)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func Test_ParseJSON(t *testing.T) {
|
||||||
|
obj := ParseJSON(`{"test":"ok"}`).(map[string]interface{})
|
||||||
|
utils.AssertEqual(t, "ok", obj["test"])
|
||||||
|
|
||||||
|
obj1 := ParseJSON(`["test1", "test2", "test3"]`).([]interface{})
|
||||||
|
utils.AssertEqual(t, "test2", obj1[1])
|
||||||
|
|
||||||
|
num := ParseJSON(`10`).(int)
|
||||||
|
utils.AssertEqual(t, 10, num)
|
||||||
|
|
||||||
|
boolVal := ParseJSON(`true`).(bool)
|
||||||
|
utils.AssertEqual(t, true, boolVal)
|
||||||
|
|
||||||
|
float64Val := ParseJSON(`1.00`).(float64)
|
||||||
|
utils.AssertEqual(t, 1.00, float64Val)
|
||||||
|
|
||||||
|
null := ParseJSON(`}}}}`)
|
||||||
|
utils.AssertEqual(t, nil, null)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user