mirror of
https://github.com/json-iterator/go.git
synced 2025-02-13 19:41:56 +02:00
trim end space
This commit is contained in:
parent
a92111261c
commit
b893a0359d
@ -7,6 +7,7 @@ import (
|
||||
|
||||
// Unmarshal adapts to json/encoding APIs
|
||||
func Unmarshal(data []byte, v interface{}) error {
|
||||
data = data[:lastNotSpacePos(data)]
|
||||
iter := ParseBytes(data)
|
||||
iter.ReadVal(v)
|
||||
if iter.head == iter.tail {
|
||||
@ -22,6 +23,7 @@ func Unmarshal(data []byte, v interface{}) error {
|
||||
}
|
||||
|
||||
func UnmarshalAny(data []byte) (Any, error) {
|
||||
data = data[:lastNotSpacePos(data)]
|
||||
iter := ParseBytes(data)
|
||||
any := iter.ReadAny()
|
||||
if iter.head == iter.tail {
|
||||
@ -36,8 +38,18 @@ func UnmarshalAny(data []byte) (Any, error) {
|
||||
return any, iter.Error
|
||||
}
|
||||
|
||||
func lastNotSpacePos(data []byte) int {
|
||||
for i := len(data) - 1; i >= 0; i-- {
|
||||
if data[i] != ' ' && data[i] != '\t' && data[i] != '\r' && data[i] != '\n' {
|
||||
return i + 1
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func UnmarshalFromString(str string, v interface{}) error {
|
||||
data := []byte(str)
|
||||
data = data[:lastNotSpacePos(data)]
|
||||
iter := ParseBytes(data)
|
||||
iter.ReadVal(v)
|
||||
if iter.head == iter.tail {
|
||||
@ -54,6 +66,7 @@ func UnmarshalFromString(str string, v interface{}) error {
|
||||
|
||||
func UnmarshalAnyFromString(str string) (Any, error) {
|
||||
data := []byte(str)
|
||||
data = data[:lastNotSpacePos(data)]
|
||||
iter := ParseBytes(data)
|
||||
any := iter.ReadAny()
|
||||
if iter.head == iter.tail {
|
||||
@ -85,4 +98,4 @@ func MarshalToString(v interface{}) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
return string(buf), nil
|
||||
}
|
||||
}
|
||||
|
@ -3,13 +3,15 @@ package jsoniter
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"github.com/json-iterator/go/require"
|
||||
)
|
||||
|
||||
func Test_bind_api_demo(t *testing.T) {
|
||||
iter := ParseString(`[0,1,2,3]`)
|
||||
should := require.New(t)
|
||||
val := []int{}
|
||||
iter.ReadVal(&val)
|
||||
fmt.Println(val[3])
|
||||
err := UnmarshalFromString(`[0,1,2,3] `, &val)
|
||||
should.Nil(err)
|
||||
should.Equal([]int{0, 1, 2, 3}, val)
|
||||
}
|
||||
|
||||
func Test_iterator_api_demo(t *testing.T) {
|
||||
@ -20,4 +22,3 @@ func Test_iterator_api_demo(t *testing.T) {
|
||||
}
|
||||
fmt.Println(total)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user