mirror of
https://github.com/json-iterator/go.git
synced 2024-11-27 08:30:57 +02:00
detect remaining bytes
This commit is contained in:
parent
a3fdd37b9a
commit
be70f29b04
@ -16,15 +16,6 @@ func Unmarshal(data []byte, v interface{}) error {
|
|||||||
return ConfigDefault.Unmarshal(data, v)
|
return ConfigDefault.Unmarshal(data, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalFromString convenient method to read from string instead of []byte
|
// UnmarshalFromString convenient method to read from string instead of []byte
|
||||||
func UnmarshalFromString(str string, v interface{}) error {
|
func UnmarshalFromString(str string, v interface{}) error {
|
||||||
return ConfigDefault.UnmarshalFromString(str, v)
|
return ConfigDefault.UnmarshalFromString(str, v)
|
||||||
|
32
config.go
32
config.go
@ -249,19 +249,17 @@ func (cfg *frozenConfig) MarshalIndent(v interface{}, prefix, indent string) ([]
|
|||||||
|
|
||||||
func (cfg *frozenConfig) UnmarshalFromString(str string, v interface{}) error {
|
func (cfg *frozenConfig) UnmarshalFromString(str string, v interface{}) error {
|
||||||
data := []byte(str)
|
data := []byte(str)
|
||||||
data = data[:lastNotSpacePos(data)]
|
|
||||||
iter := cfg.BorrowIterator(data)
|
iter := cfg.BorrowIterator(data)
|
||||||
defer cfg.ReturnIterator(iter)
|
defer cfg.ReturnIterator(iter)
|
||||||
iter.ReadVal(v)
|
iter.ReadVal(v)
|
||||||
if iter.head == iter.tail {
|
c := iter.nextToken()
|
||||||
iter.loadMore()
|
if c == 0 {
|
||||||
}
|
if iter.Error == io.EOF {
|
||||||
if iter.Error == io.EOF {
|
return nil
|
||||||
return nil
|
}
|
||||||
}
|
return iter.Error
|
||||||
if iter.Error == nil {
|
|
||||||
iter.ReportError("UnmarshalFromString", "there are bytes left after unmarshal")
|
|
||||||
}
|
}
|
||||||
|
iter.ReportError("Unmarshal", "there are bytes left after unmarshal")
|
||||||
return iter.Error
|
return iter.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -272,19 +270,17 @@ func (cfg *frozenConfig) Get(data []byte, path ...interface{}) Any {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *frozenConfig) Unmarshal(data []byte, v interface{}) error {
|
func (cfg *frozenConfig) Unmarshal(data []byte, v interface{}) error {
|
||||||
data = data[:lastNotSpacePos(data)]
|
|
||||||
iter := cfg.BorrowIterator(data)
|
iter := cfg.BorrowIterator(data)
|
||||||
defer cfg.ReturnIterator(iter)
|
defer cfg.ReturnIterator(iter)
|
||||||
iter.ReadVal(v)
|
iter.ReadVal(v)
|
||||||
if iter.head == iter.tail {
|
c := iter.nextToken()
|
||||||
iter.loadMore()
|
if c == 0 {
|
||||||
}
|
if iter.Error == io.EOF {
|
||||||
if iter.Error == io.EOF {
|
return nil
|
||||||
return nil
|
}
|
||||||
}
|
return iter.Error
|
||||||
if iter.Error == nil {
|
|
||||||
iter.ReportError("Unmarshal", "there are bytes left after unmarshal")
|
|
||||||
}
|
}
|
||||||
|
iter.ReportError("Unmarshal", "there are bytes left after unmarshal")
|
||||||
return iter.Error
|
return iter.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,11 @@ func init() {
|
|||||||
Field interface{}
|
Field interface{}
|
||||||
})(nil),
|
})(nil),
|
||||||
input: `{"Field": "hello"}`,
|
input: `{"Field": "hello"}`,
|
||||||
|
}, unmarshalCase{
|
||||||
|
ptr: (*struct {
|
||||||
|
Field interface{}
|
||||||
|
})(nil),
|
||||||
|
input: `{"Field": "hello"} `,
|
||||||
}, unmarshalCase{
|
}, unmarshalCase{
|
||||||
ptr: (*struct {
|
ptr: (*struct {
|
||||||
Field int `json:"field"`
|
Field int `json:"field"`
|
||||||
|
Loading…
Reference in New Issue
Block a user