Skip to content

Commit

Permalink
#185 add jsoniter.Valid
Browse files Browse the repository at this point in the history
  • Loading branch information
taowen committed Oct 10, 2017
1 parent 0149a5c commit 6240e1e
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 17 deletions.
5 changes: 5 additions & 0 deletions feature_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,8 @@ func (adapter *Encoder) SetEscapeHTML(escapeHTML bool) {
config.EscapeHTML = escapeHTML
adapter.stream.cfg = config.Froze().(*frozenConfig)
}

// Valid reports whether data is a valid JSON encoding.
func Valid(data []byte) bool {
return ConfigDefault.Valid(data)
}
8 changes: 8 additions & 0 deletions feature_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type API interface {
Get(data []byte, path ...interface{}) Any
NewEncoder(writer io.Writer) *Encoder
NewDecoder(reader io.Reader) *Decoder
Valid(data []byte) bool
}

// ConfigDefault the default API
Expand Down Expand Up @@ -333,3 +334,10 @@ func (cfg *frozenConfig) NewDecoder(reader io.Reader) *Decoder {
iter := Parse(cfg, reader, 512)
return &Decoder{iter}
}

func (cfg *frozenConfig) Valid(data []byte) bool {
iter := cfg.BorrowIterator(data)
defer cfg.ReturnIterator(iter)
iter.Skip()
return iter.Error == nil
}
2 changes: 1 addition & 1 deletion feature_iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (iter *Iterator) ReportError(operation string, msg string) {
}
context := string(iter.buf[contextStart:contextEnd])
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
Expand Down
2 changes: 1 addition & 1 deletion feature_iter_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (iter *Iterator) ReadArrayCB(callback func(*Iterator) bool) (ret bool) {
c = iter.nextToken()
}
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 true
Expand Down
18 changes: 9 additions & 9 deletions feature_iter_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (iter *Iterator) ReadObject() (ret string) {
if c == '}' {
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
case ',':
return string(iter.readObjectFieldAsBytes())
Expand Down Expand Up @@ -105,14 +105,14 @@ func (iter *Iterator) ReadObjectCB(callback func(*Iterator, string) bool) bool {
if c == '}' {
return true
}
iter.ReportError("ReadObjectCB", `expect " after }, but found ` + string([]byte{c}))
iter.ReportError("ReadObjectCB", `expect " after }, but found `+string([]byte{c}))
return false
}
if c == 'n' {
iter.skipThreeBytes('u', 'l', 'l')
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
}

Expand All @@ -125,7 +125,7 @@ func (iter *Iterator) ReadMapCB(callback func(*Iterator, string) bool) bool {
iter.unreadByte()
field := iter.ReadString()
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
}
if !callback(iter, field) {
Expand All @@ -135,7 +135,7 @@ func (iter *Iterator) ReadMapCB(callback func(*Iterator, string) bool) bool {
for c == ',' {
field = iter.ReadString()
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
}
if !callback(iter, field) {
Expand All @@ -152,14 +152,14 @@ func (iter *Iterator) ReadMapCB(callback func(*Iterator, string) bool) bool {
if c == '}' {
return true
}
iter.ReportError("ReadMapCB", `expect " after }, but found ` + string([]byte{c}))
iter.ReportError("ReadMapCB", `expect " after }, but found `+string([]byte{c}))
return false
}
if c == 'n' {
iter.skipThreeBytes('u', 'l', 'l')
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
}

Expand All @@ -176,7 +176,7 @@ func (iter *Iterator) readObjectStart() bool {
iter.skipThreeBytes('u', 'l', 'l')
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
}

Expand All @@ -192,7 +192,7 @@ func (iter *Iterator) readObjectFieldAsBytes() (ret []byte) {
}
}
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
}
iter.head++
Expand Down
2 changes: 1 addition & 1 deletion feature_iter_skip.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (iter *Iterator) ReadBool() (ret bool) {
iter.skipFourBytes('a', 'l', 's', 'e')
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
}

Expand Down
6 changes: 3 additions & 3 deletions feature_iter_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (iter *Iterator) ReadString() (ret string) {
iter.skipThreeBytes('u', 'l', 'l')
return ""
}
iter.ReportError("ReadString", `expects " or n, but found ` + string([]byte{c}))
iter.ReportError("ReadString", `expects " or n, but found `+string([]byte{c}))
return
}

Expand Down Expand Up @@ -139,7 +139,7 @@ func (iter *Iterator) ReadStringAsSlice() (ret []byte) {
}
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
}

Expand All @@ -156,7 +156,7 @@ func (iter *Iterator) readU4() (ret rune) {
} else if c >= 'A' && c <= 'F' {
ret = ret*16 + rune(c-'A'+10)
} 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
}
}
Expand Down
4 changes: 2 additions & 2 deletions feature_reflect_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ type stringModeNumberDecoder struct {
func (decoder *stringModeNumberDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
c := iter.nextToken()
if c != '"' {
iter.ReportError("stringModeNumberDecoder", `expect ", but found ` + string([]byte{c}))
iter.ReportError("stringModeNumberDecoder", `expect ", but found `+string([]byte{c}))
return
}
decoder.elemDecoder.Decode(ptr, iter)
Expand All @@ -617,7 +617,7 @@ func (decoder *stringModeNumberDecoder) Decode(ptr unsafe.Pointer, iter *Iterato
}
c = iter.readByte()
if c != '"' {
iter.ReportError("stringModeNumberDecoder", `expect ", but found ` + string([]byte{c}))
iter.ReportError("stringModeNumberDecoder", `expect ", but found `+string([]byte{c}))
return
}
}
Expand Down
6 changes: 6 additions & 0 deletions jsoniter_invalid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,9 @@ func Test_invalid_number(t *testing.T) {
should.Nil(err)
should.Equal(string(result2), string(result))
}

func Test_valid(t *testing.T) {
should := require.New(t)
should.True(Valid([]byte(`{}`)))
should.False(Valid([]byte(`{`)))
}

0 comments on commit 6240e1e

Please sign in to comment.