mirror of
https://github.com/json-iterator/go.git
synced 2024-11-24 08:22:14 +02:00
#185 add jsoniter.Valid
This commit is contained in:
parent
0149a5cf4a
commit
6240e1e798
@ -125,3 +125,8 @@ func (adapter *Encoder) SetEscapeHTML(escapeHTML bool) {
|
|||||||
config.EscapeHTML = escapeHTML
|
config.EscapeHTML = escapeHTML
|
||||||
adapter.stream.cfg = config.Froze().(*frozenConfig)
|
adapter.stream.cfg = config.Froze().(*frozenConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Valid reports whether data is a valid JSON encoding.
|
||||||
|
func Valid(data []byte) bool {
|
||||||
|
return ConfigDefault.Valid(data)
|
||||||
|
}
|
||||||
|
@ -45,6 +45,7 @@ type API interface {
|
|||||||
Get(data []byte, path ...interface{}) Any
|
Get(data []byte, path ...interface{}) Any
|
||||||
NewEncoder(writer io.Writer) *Encoder
|
NewEncoder(writer io.Writer) *Encoder
|
||||||
NewDecoder(reader io.Reader) *Decoder
|
NewDecoder(reader io.Reader) *Decoder
|
||||||
|
Valid(data []byte) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConfigDefault the default API
|
// ConfigDefault the default API
|
||||||
@ -333,3 +334,10 @@ func (cfg *frozenConfig) NewDecoder(reader io.Reader) *Decoder {
|
|||||||
iter := Parse(cfg, reader, 512)
|
iter := Parse(cfg, reader, 512)
|
||||||
return &Decoder{iter}
|
return &Decoder{iter}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) Valid(data []byte) bool {
|
||||||
|
iter := cfg.BorrowIterator(data)
|
||||||
|
defer cfg.ReturnIterator(iter)
|
||||||
|
iter.Skip()
|
||||||
|
return iter.Error == nil
|
||||||
|
}
|
||||||
|
@ -215,7 +215,7 @@ func (iter *Iterator) ReportError(operation string, msg string) {
|
|||||||
}
|
}
|
||||||
context := string(iter.buf[contextStart:contextEnd])
|
context := string(iter.buf[contextStart:contextEnd])
|
||||||
iter.Error = fmt.Errorf("%s: %s, error found in #%v byte of ...|%s|..., bigger context ...|%s|...",
|
iter.Error = fmt.Errorf("%s: %s, error found in #%v byte of ...|%s|..., bigger context ...|%s|...",
|
||||||
operation, msg, iter.head - peekStart, parsing, context)
|
operation, msg, iter.head-peekStart, parsing, context)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CurrentBuffer gets current buffer as string for debugging purpose
|
// CurrentBuffer gets current buffer as string for debugging purpose
|
||||||
|
@ -42,7 +42,7 @@ func (iter *Iterator) ReadArrayCB(callback func(*Iterator) bool) (ret bool) {
|
|||||||
c = iter.nextToken()
|
c = iter.nextToken()
|
||||||
}
|
}
|
||||||
if c != ']' {
|
if c != ']' {
|
||||||
iter.ReportError("ReadArrayCB", "expect ] in the end, but found " + string([]byte{c}))
|
iter.ReportError("ReadArrayCB", "expect ] in the end, but found "+string([]byte{c}))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
@ -24,7 +24,7 @@ func (iter *Iterator) ReadObject() (ret string) {
|
|||||||
if c == '}' {
|
if c == '}' {
|
||||||
return "" // end of object
|
return "" // end of object
|
||||||
}
|
}
|
||||||
iter.ReportError("ReadObject", `expect " after {, but found ` + string([]byte{c}))
|
iter.ReportError("ReadObject", `expect " after {, but found `+string([]byte{c}))
|
||||||
return
|
return
|
||||||
case ',':
|
case ',':
|
||||||
return string(iter.readObjectFieldAsBytes())
|
return string(iter.readObjectFieldAsBytes())
|
||||||
@ -105,14 +105,14 @@ func (iter *Iterator) ReadObjectCB(callback func(*Iterator, string) bool) bool {
|
|||||||
if c == '}' {
|
if c == '}' {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
iter.ReportError("ReadObjectCB", `expect " after }, but found ` + string([]byte{c}))
|
iter.ReportError("ReadObjectCB", `expect " after }, but found `+string([]byte{c}))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if c == 'n' {
|
if c == 'n' {
|
||||||
iter.skipThreeBytes('u', 'l', 'l')
|
iter.skipThreeBytes('u', 'l', 'l')
|
||||||
return true // null
|
return true // null
|
||||||
}
|
}
|
||||||
iter.ReportError("ReadObjectCB", `expect { or n, but found ` + string([]byte{c}))
|
iter.ReportError("ReadObjectCB", `expect { or n, but found `+string([]byte{c}))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,7 +125,7 @@ func (iter *Iterator) ReadMapCB(callback func(*Iterator, string) bool) bool {
|
|||||||
iter.unreadByte()
|
iter.unreadByte()
|
||||||
field := iter.ReadString()
|
field := iter.ReadString()
|
||||||
if iter.nextToken() != ':' {
|
if iter.nextToken() != ':' {
|
||||||
iter.ReportError("ReadMapCB", "expect : after object field, but found " + string([]byte{c}))
|
iter.ReportError("ReadMapCB", "expect : after object field, but found "+string([]byte{c}))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if !callback(iter, field) {
|
if !callback(iter, field) {
|
||||||
@ -135,7 +135,7 @@ func (iter *Iterator) ReadMapCB(callback func(*Iterator, string) bool) bool {
|
|||||||
for c == ',' {
|
for c == ',' {
|
||||||
field = iter.ReadString()
|
field = iter.ReadString()
|
||||||
if iter.nextToken() != ':' {
|
if iter.nextToken() != ':' {
|
||||||
iter.ReportError("ReadMapCB", "expect : after object field, but found " + string([]byte{c}))
|
iter.ReportError("ReadMapCB", "expect : after object field, but found "+string([]byte{c}))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if !callback(iter, field) {
|
if !callback(iter, field) {
|
||||||
@ -152,14 +152,14 @@ func (iter *Iterator) ReadMapCB(callback func(*Iterator, string) bool) bool {
|
|||||||
if c == '}' {
|
if c == '}' {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
iter.ReportError("ReadMapCB", `expect " after }, but found ` + string([]byte{c}))
|
iter.ReportError("ReadMapCB", `expect " after }, but found `+string([]byte{c}))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if c == 'n' {
|
if c == 'n' {
|
||||||
iter.skipThreeBytes('u', 'l', 'l')
|
iter.skipThreeBytes('u', 'l', 'l')
|
||||||
return true // null
|
return true // null
|
||||||
}
|
}
|
||||||
iter.ReportError("ReadMapCB", `expect { or n, but found ` + string([]byte{c}))
|
iter.ReportError("ReadMapCB", `expect { or n, but found `+string([]byte{c}))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,7 +176,7 @@ func (iter *Iterator) readObjectStart() bool {
|
|||||||
iter.skipThreeBytes('u', 'l', 'l')
|
iter.skipThreeBytes('u', 'l', 'l')
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
iter.ReportError("readObjectStart", "expect { or n, but found " + string([]byte{c}))
|
iter.ReportError("readObjectStart", "expect { or n, but found "+string([]byte{c}))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,7 +192,7 @@ func (iter *Iterator) readObjectFieldAsBytes() (ret []byte) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if iter.buf[iter.head] != ':' {
|
if iter.buf[iter.head] != ':' {
|
||||||
iter.ReportError("readObjectFieldAsBytes", "expect : after object field, but found " + string([]byte{iter.buf[iter.head]}))
|
iter.ReportError("readObjectFieldAsBytes", "expect : after object field, but found "+string([]byte{iter.buf[iter.head]}))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
iter.head++
|
iter.head++
|
||||||
|
@ -25,7 +25,7 @@ func (iter *Iterator) ReadBool() (ret bool) {
|
|||||||
iter.skipFourBytes('a', 'l', 's', 'e')
|
iter.skipFourBytes('a', 'l', 's', 'e')
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
iter.ReportError("ReadBool", "expect t or f, but found " + string([]byte{c}))
|
iter.ReportError("ReadBool", "expect t or f, but found "+string([]byte{c}))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ func (iter *Iterator) ReadString() (ret string) {
|
|||||||
iter.skipThreeBytes('u', 'l', 'l')
|
iter.skipThreeBytes('u', 'l', 'l')
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
iter.ReportError("ReadString", `expects " or n, but found ` + string([]byte{c}))
|
iter.ReportError("ReadString", `expects " or n, but found `+string([]byte{c}))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,7 +139,7 @@ func (iter *Iterator) ReadStringAsSlice() (ret []byte) {
|
|||||||
}
|
}
|
||||||
return copied
|
return copied
|
||||||
}
|
}
|
||||||
iter.ReportError("ReadStringAsSlice", `expects " or n, but found ` + string([]byte{c}))
|
iter.ReportError("ReadStringAsSlice", `expects " or n, but found `+string([]byte{c}))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,7 +156,7 @@ func (iter *Iterator) readU4() (ret rune) {
|
|||||||
} else if c >= 'A' && c <= 'F' {
|
} else if c >= 'A' && c <= 'F' {
|
||||||
ret = ret*16 + rune(c-'A'+10)
|
ret = ret*16 + rune(c-'A'+10)
|
||||||
} else {
|
} else {
|
||||||
iter.ReportError("readU4", "expects 0~9 or a~f, but found " + string([]byte{c}))
|
iter.ReportError("readU4", "expects 0~9 or a~f, but found "+string([]byte{c}))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -608,7 +608,7 @@ type stringModeNumberDecoder struct {
|
|||||||
func (decoder *stringModeNumberDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (decoder *stringModeNumberDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
c := iter.nextToken()
|
c := iter.nextToken()
|
||||||
if c != '"' {
|
if c != '"' {
|
||||||
iter.ReportError("stringModeNumberDecoder", `expect ", but found ` + string([]byte{c}))
|
iter.ReportError("stringModeNumberDecoder", `expect ", but found `+string([]byte{c}))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
decoder.elemDecoder.Decode(ptr, iter)
|
decoder.elemDecoder.Decode(ptr, iter)
|
||||||
@ -617,7 +617,7 @@ func (decoder *stringModeNumberDecoder) Decode(ptr unsafe.Pointer, iter *Iterato
|
|||||||
}
|
}
|
||||||
c = iter.readByte()
|
c = iter.readByte()
|
||||||
if c != '"' {
|
if c != '"' {
|
||||||
iter.ReportError("stringModeNumberDecoder", `expect ", but found ` + string([]byte{c}))
|
iter.ReportError("stringModeNumberDecoder", `expect ", but found `+string([]byte{c}))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -130,3 +130,9 @@ func Test_invalid_number(t *testing.T) {
|
|||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(string(result2), string(result))
|
should.Equal(string(result2), string(result))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_valid(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
should.True(Valid([]byte(`{}`)))
|
||||||
|
should.False(Valid([]byte(`{`)))
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user