package dataformat
import (
"strings"
jsoniter "github.com/json-iterator/go"
)
type JSON struct{}
var (
_ DataFormat = &JSON{}
)
func NewJSON() *JSON {
return &JSON{}
}
func (j *JSON) IsType(data string) bool {
return strings.HasPrefix(data, "{") && strings.HasSuffix(data, "}")
}
func (j *JSON) Encode(data KV) (string, error) {
encoded, err := jsoniter.Marshal(data.Map)
return string(encoded), err
}
func (j *JSON) Decode(data string) (KV, error) {
var decoded map[string]interface{}
err := jsoniter.Unmarshal([]byte(data), &decoded)
return KVMap(decoded), err
}
func (j *JSON) Name() string {
return JSONDataFormat
}