You've already forked json-iterator
mirror of
https://github.com/json-iterator/go.git
synced 2025-06-18 22:57:33 +02:00
#185 add jsoniter.Valid
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
@ -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
|
||||
}
|
||||
|
@ -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(`{`)))
|
||||
}
|
||||
|
Reference in New Issue
Block a user