Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/fuzz/dataformat/json.go
2070 views
1
package dataformat
2
3
import (
4
"strings"
5
6
jsoniter "github.com/json-iterator/go"
7
)
8
9
// JSON is a JSON encoder
10
//
11
// For now JSON only supports objects as the root data type
12
// and not arrays
13
//
14
// TODO: Support arrays + other JSON oddities by
15
// adding more attirbutes to the map[string]interface{}
16
type JSON struct{}
17
18
var (
19
_ DataFormat = &JSON{}
20
)
21
22
// NewJSON returns a new JSON encoder
23
func NewJSON() *JSON {
24
return &JSON{}
25
}
26
27
// IsType returns true if the data is JSON encoded
28
func (j *JSON) IsType(data string) bool {
29
return strings.HasPrefix(data, "{") && strings.HasSuffix(data, "}")
30
}
31
32
// Encode encodes the data into JSON format
33
func (j *JSON) Encode(data KV) (string, error) {
34
encoded, err := jsoniter.Marshal(data.Map)
35
return string(encoded), err
36
}
37
38
// Decode decodes the data from JSON format
39
func (j *JSON) Decode(data string) (KV, error) {
40
var decoded map[string]interface{}
41
err := jsoniter.Unmarshal([]byte(data), &decoded)
42
return KVMap(decoded), err
43
}
44
45
// Name returns the name of the encoder
46
func (j *JSON) Name() string {
47
return JSONDataFormat
48
}
49
50