forked from OpsLevel/opslevel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.go
54 lines (47 loc) · 1.01 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package opslevel
import (
"encoding/json"
"strconv"
)
// JSON is a specialized map[string]string to support proper graphql serialization
type JSON map[string]any
func (s JSON) GetGraphQLType() string { return "JSON" }
func NewJSON(data string) JSON {
result := make(JSON)
json.Unmarshal([]byte(data), &result)
return result
}
func (s JSON) ToJSON() string {
dto := map[string]any{}
for k, v := range s {
dto[k] = v
}
b, _ := json.Marshal(dto)
return string(b)
}
func (s JSON) MarshalJSON() ([]byte, error) {
dto := map[string]any{}
for k, v := range s {
dto[k] = v
}
b, err := json.Marshal(dto)
return []byte(strconv.Quote(string(b))), err
}
//
//func (s *JSON) UnmarshalJSON(data []byte) error {
// escaped, err := strconv.Unquote(string(data))
// if err != nil {
// return err
// }
// dto := map[string]string{}
// if err := json.Unmarshal([]byte(escaped), &dto); err != nil {
// return err
// }
// if (*s) == nil {
// (*s) = JSON{}
// }
// for k, v := range dto {
// (*s)[k] = v
// }
// return nil
//}