@@ -12,22 +12,42 @@ var anonymousEncoders = []byte{}
12
12
func generateEncoders (typeSpec * ast.TypeSpec ) []byte {
13
13
typeName := typeSpec .Name .Name
14
14
lines = []byte {}
15
- encodeType (typeSpec .Type )
15
+ encodeType (typeName , typeSpec .Type )
16
16
mainEncoder := lines
17
17
lines = []byte {}
18
+ switch x := typeSpec .Type .(type ) {
19
+ case * ast.StructType :
20
+ encodeStructField (x )
21
+ case * ast.StarExpr :
22
+ encodeOtherField (typeName )
23
+ case * ast.MapType :
24
+ encodeOtherField (typeName )
25
+ case * ast.ArrayType :
26
+ encodeOtherField (typeName )
27
+ case * ast.Ident :
28
+ encodeOtherField (typeName )
29
+ default :
30
+ reportError (fmt .Errorf ("not supported type: %s" , nodeToString (typeSpec )))
31
+ return nil
32
+ }
33
+ fieldsEncoder := lines
34
+ lines = []byte {}
18
35
_f ("func %s_json_marshal(stream *jsoniter.Stream, val %s) {" , prefix , typeName )
19
36
lines = append (lines , mainEncoder ... )
20
37
_l ("}" )
38
+ _f ("func %s_json_marshal_field(stream *jsoniter.Stream, val %s) {" , prefix , typeName )
39
+ lines = append (lines , fieldsEncoder ... )
40
+ _l ("}" )
21
41
lines = append (lines , anonymousEncoders ... )
22
42
return lines
23
43
}
24
44
25
- func encodeType (t ast.Expr ) {
45
+ func encodeType (typeName string , t ast.Expr ) {
26
46
switch x := t .(type ) {
27
47
case * ast.ArrayType :
28
48
encodeArray (x )
29
49
case * ast.StructType :
30
- encodeStruct (x )
50
+ encodeStruct (typeName , x )
31
51
case * ast.MapType :
32
52
encodeMap (x )
33
53
case * ast.StarExpr :
@@ -74,8 +94,11 @@ func encodeAnonymousStruct(structType *ast.StructType) string {
74
94
anonymousCounter ++
75
95
oldLines := lines
76
96
lines = []byte {}
97
+ _f ("func %s_json_marshal_field (stream *jsoniter.Stream, val %s) {" , encoderName , typeName )
98
+ encodeStructField (structType )
99
+ _l ("}" )
77
100
_f ("func %s_json_marshal (stream *jsoniter.Stream, val %s) {" , encoderName , typeName )
78
- encodeStruct (structType )
101
+ encodeStruct (encoderName , structType )
79
102
_l ("}" )
80
103
anonymousEncoders = append (anonymousEncoders , lines ... )
81
104
lines = oldLines
@@ -114,22 +137,13 @@ func encodeFixedSizeArray(arrayType *ast.ArrayType, length int) {
114
137
}
115
138
116
139
func encodeMap (mapType * ast.MapType ) {
117
- _l (" if len(val) == 0 {" )
118
- _l (" stream.WriteEmptyObject()" )
119
- _l (" } else {" )
120
- _l (" isFirst := true" )
121
- _l (" stream.WriteObjectHead()" )
122
- _l (" for k, v := range val {" )
123
- _l (" if isFirst {" )
124
- _l (" isFirst = false" )
125
- _l (" } else {" )
126
- _l (" stream.WriteMore()" )
127
- _l (" }" )
140
+ _l (" stream.WriteObjectHead()" )
141
+ _l (" for k, v := range val {" )
128
142
switch x := mapType .Key .(type ) {
129
143
case * ast.Ident :
130
144
switch x .Name {
131
145
case "string" :
132
- _l (" stream.WriteObjectField(k)" )
146
+ _l (" stream.WriteObjectField(k)" )
133
147
case "int" :
134
148
fallthrough
135
149
case "uint" :
@@ -159,14 +173,18 @@ func encodeMap(mapType *ast.MapType) {
159
173
reportError (fmt .Errorf ("unsupported map key type: %s" , nodeToString (mapType .Key )))
160
174
}
161
175
genEncodeStmt (mapType .Value , "v" )
162
- _l (" }" )
163
- _l (" stream.WriteObjectTail()" )
176
+ _l (" stream.WriteMore()" )
164
177
_l (" }" )
178
+ _l (" stream.WriteObjectTail()" )
165
179
}
166
180
167
- func encodeStruct (structType * ast.StructType ) {
181
+ func encodeStruct (typeName string , structType * ast.StructType ) {
168
182
_l (" stream.WriteObjectHead()" )
169
- isFirst := true
183
+ _f (" %s_json_marshal_field(stream, val)" , typeName )
184
+ _l (" stream.WriteObjectTail()" )
185
+ }
186
+
187
+ func encodeStructField (structType * ast.StructType ) {
170
188
for _ , field := range structType .Fields .List {
171
189
if len (field .Names ) == 0 {
172
190
continue
@@ -192,15 +210,16 @@ func encodeStruct(structType *ast.StructType) {
192
210
if isNotExported {
193
211
continue
194
212
}
195
- if isFirst {
196
- isFirst = false
197
- } else {
198
- _l (" stream.WriteMore()" )
199
- }
200
213
_f (" stream.WriteObjectField(`%s`)" , encodedFieldName )
201
214
genEncodeStmt (field .Type , fmt .Sprintf ("val.%s" , fieldName ))
215
+ _l (" stream.WriteMore()" )
202
216
}
203
- _l (" stream.WriteObjectTail()" )
217
+ }
218
+
219
+ func encodeOtherField (typeName string ) {
220
+ _f (` stream.WriteObjectField("%s")` , typeName )
221
+ _f (" %s_json_marshal(stream, val)" , prefix )
222
+ _l (" stream.WriteMore()" )
204
223
}
205
224
206
225
func genEncodeStmt (node ast.Node , val string ) {
0 commit comments