-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.go
97 lines (78 loc) · 1.82 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package chaff
import (
"encoding/json"
)
type (
// Additional properties can be a schema node or a boolean value.
// This handles both cases.
additionalData struct {
Schema *schemaNode
DisallowAdditional bool
}
// Used to handle the fact that "type" can be a string or an array of strings
multipleType struct {
SingleType string
MultipleTypes []string
}
// Used to handle the fact that "items" can be a schema node or an array of schema nodes
itemsData struct {
Node *schemaNode
Nodes []schemaNode
DisallowAdditionalItems bool
}
)
func (a *additionalData) UnmarshalJSON(data []byte) error {
if len(data) == 0 {
return nil
}
if string(data) == "false" {
a.DisallowAdditional = true
return nil
}
if string(data) == "true" {
a.DisallowAdditional = false
return nil
}
var schema schemaNode
err := json.Unmarshal(data, &schema)
if err != nil {
return err
}
a.Schema = &schema
return nil
}
func (m *multipleType) UnmarshalJSON(data []byte) error {
if len(data) == 0 {
return nil
}
var multipleTypes []string
var singleType string
// Try to parse an array of types
multipleTypesError := json.Unmarshal(data, &multipleTypes)
singleTypeError := json.Unmarshal(data, &singleType)
if multipleTypesError != nil && singleTypeError != nil {
return singleTypeError
}
m.MultipleTypes = multipleTypes
m.SingleType = singleType
return nil
}
func (i *itemsData) UnmarshalJSON(data []byte) error {
if len(data) == 0 {
return nil
}
if string(data) == "false" {
i.DisallowAdditionalItems = true
return nil
}
var nodes []schemaNode
var node *schemaNode
nodeErr := json.Unmarshal(data, &node)
nodesErr := json.Unmarshal(data, &nodes)
if nodeErr != nil && nodesErr != nil {
return nodeErr
}
i.Nodes = nodes
i.Node = node
return nil
}