mirror of
https://github.com/json-iterator/go.git
synced 2025-04-23 11:37:32 +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
|
||||||
|
}
|
||||||
|
@ -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…
x
Reference in New Issue
Block a user