-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrappedjson.go
188 lines (182 loc) · 3.59 KB
/
wrappedjson.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Content managed by Project Forge, see [projectforge.md] for details.
package types
import (
"encoding/json"
"github.com/pkg/errors"
"admini.dev/admini/app/util"
)
type wrappedUnmarshal struct {
K string `json:"k"`
T json.RawMessage `json:"t,omitempty"`
}
func (x *Wrapped) MarshalJSON() ([]byte, error) {
b := util.ToJSONBytes(x.T, false)
// needs better detection
if len(b) == 2 {
return util.ToJSONBytes(x.K, false), nil
}
return util.ToJSONBytes(wrappedUnmarshal{K: x.K, T: b}, false), nil
}
//nolint:funlen, gocyclo, cyclop
func (x *Wrapped) UnmarshalJSON(data []byte) error {
var wu wrappedUnmarshal
err := util.FromJSON(data, &wu)
if err != nil {
str := ""
newErr := util.FromJSON(data, &str)
if newErr != nil {
return err
}
wu = wrappedUnmarshal{K: str, T: []byte("{}")}
}
if wu.K == "boolean" {
wu.K = KeyBool
}
var t Type
switch wu.K {
case KeyAny:
tgt := &Any{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
case KeyBit:
tgt := &Bit{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
case KeyBool:
tgt := &Bool{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
case KeyByte:
tgt := &Byte{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
case KeyChar:
tgt := &Char{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
case KeyDate:
tgt := &Date{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
case KeyEnum:
tgt := &Enum{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
case KeyEnumValue:
tgt := &EnumValue{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
case KeyError:
tgt := &Error{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
case KeyFloat:
tgt := &Float{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
case KeyInt:
tgt := &Int{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
case KeyJSON:
tgt := &JSON{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
case KeyList:
tgt := &List{}
err = util.FromJSON(wu.T, &tgt)
if tgt.V == nil {
tgt.V = NewAny()
}
t = tgt
case KeyMap:
tgt := &Map{}
err = util.FromJSON(wu.T, &tgt)
if tgt.K == nil {
tgt.K = NewString()
}
if tgt.V == nil {
tgt.V = NewAny()
}
t = tgt
case KeyMethod:
tgt := &Method{}
err = util.FromJSON(wu.T, &tgt)
if tgt.Ret == nil {
tgt.Ret = NewAny()
}
t = tgt
case KeyNil:
tgt := &Nil{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
case KeyOption:
tgt := &Option{}
err = util.FromJSON(wu.T, &tgt)
if tgt.V == nil {
tgt.V = NewAny()
}
t = tgt
case KeyRange:
tgt := &Range{}
err = util.FromJSON(wu.T, &tgt)
if tgt.V == nil {
tgt.V = NewAny()
}
t = tgt
case KeyReference:
tgt := &Reference{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
case KeySet:
tgt := &Set{}
err = util.FromJSON(wu.T, &tgt)
if tgt.V == nil {
tgt.V = NewAny()
}
t = tgt
case KeyString:
tgt := &String{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
case KeyTime:
tgt := &Time{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
case KeyTimestamp:
tgt := &Timestamp{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
case KeyTimestampZoned:
tgt := &TimestampZoned{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
case KeyUnknown:
tgt := &Unknown{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
case KeyUUID:
tgt := &UUID{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
case KeyValueMap:
tgt := &ValueMap{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
case KeyXML:
tgt := &XML{}
err = util.FromJSON(wu.T, &tgt)
t = tgt
default:
t = &Unknown{X: "unmarshal:" + wu.K}
}
if err != nil {
return errors.Wrapf(err, "unable to unmarshal wrapped field of type [%s]", wu.K)
}
if t == nil {
return errors.New("nil type returned from unmarshal")
}
x.K = wu.K
x.T = t
return nil
}