FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java/src/main/java/com/jsoniter/CodegenImplNative.java at master · structure/java · GitHub
structure
/
java
Public
forked from
json-iterator/java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java
/
src
/
main
/
java
/
com
/
jsoniter
/
CodegenImplNative.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
272 lines (264 loc) · 11.6 KB
Breadcrumbs
java
/
src
/
main
/
java
/
com
/
jsoniter
/
CodegenImplNative.java
Copy path
File metadata and controls
272 lines (264 loc) · 11.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package
com
.
jsoniter
;
import
com
.
jsoniter
.
any
.
Any
;
import
com
.
jsoniter
.
spi
.*;
import
java
.
io
.
IOException
;
import
java
.
lang
.
reflect
.
ParameterizedType
;
import
java
.
lang
.
reflect
.
Type
;
import
java
.
lang
.
reflect
.
WildcardType
;
import
java
.
math
.
BigDecimal
;
import
java
.
math
.
BigInteger
;
import
java
.
util
.
HashMap
;
import
java
.
util
.
Map
;
class
CodegenImplNative
{
final
static
Map
<
String
,
String
>
NATIVE_READS
=
new
HashMap
<
String
,
String
>() {{
put
(
"float"
,
"iter.readFloat()"
);
put
(
"double"
,
"iter.readDouble()"
);
put
(
"boolean"
,
"iter.readBoolean()"
);
put
(
"byte"
,
"iter.readShort()"
);
put
(
"short"
,
"iter.readShort()"
);
put
(
"int"
,
"iter.readInt()"
);
put
(
"char"
,
"iter.readInt()"
);
put
(
"long"
,
"iter.readLong()"
);
put
(
Float
.
class
.
getName
(),
"(iter.readNull() ? null : java.lang.Float.valueOf(iter.readFloat()))"
);
put
(
Double
.
class
.
getName
(),
"(iter.readNull() ? null : java.lang.Double.valueOf(iter.readDouble()))"
);
put
(
Boolean
.
class
.
getName
(),
"(iter.readNull() ? null : java.lang.Boolean.valueOf(iter.readBoolean()))"
);
put
(
Byte
.
class
.
getName
(),
"(iter.readNull() ? null : java.lang.Byte.valueOf((byte)iter.readShort()))"
);
put
(
Character
.
class
.
getName
(),
"(iter.readNull() ? null : java.lang.Character.valueOf((char)iter.readShort()))"
);
put
(
Short
.
class
.
getName
(),
"(iter.readNull() ? null : java.lang.Short.valueOf(iter.readShort()))"
);
put
(
Integer
.
class
.
getName
(),
"(iter.readNull() ? null : java.lang.Integer.valueOf(iter.readInt()))"
);
put
(
Long
.
class
.
getName
(),
"(iter.readNull() ? null : java.lang.Long.valueOf(iter.readLong()))"
);
put
(
BigDecimal
.
class
.
getName
(),
"iter.readBigDecimal()"
);
put
(
BigInteger
.
class
.
getName
(),
"iter.readBigInteger()"
);
put
(
String
.
class
.
getName
(),
"iter.readString()"
);
put
(
Object
.
class
.
getName
(),
"iter.read()"
);
put
(
Any
.
class
.
getName
(),
"iter.readAny()"
);
}};
final
static
Map
<
Class
,
Decoder
>
NATIVE_DECODERS
=
new
HashMap
<
Class
,
Decoder
>() {{
put
(
float
.
class
,
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter
)
throws
IOException
{
return
iter
.
readFloat
();
}
});
put
(
Float
.
class
,
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter
)
throws
IOException
{
return
iter
.
readNull
() ?
null
:
iter
.
readFloat
();
}
});
put
(
double
.
class
,
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter
)
throws
IOException
{
return
iter
.
readDouble
();
}
});
put
(
Double
.
class
,
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter
)
throws
IOException
{
return
iter
.
readNull
() ?
null
:
iter
.
readDouble
();
}
});
put
(
boolean
.
class
,
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter
)
throws
IOException
{
return
iter
.
readBoolean
();
}
});
put
(
Boolean
.
class
,
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter
)
throws
IOException
{
return
iter
.
readNull
() ?
null
:
iter
.
readBoolean
();
}
});
put
(
byte
.
class
,
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter
)
throws
IOException
{
return
Byte
.
valueOf
((
byte
)
iter
.
readShort
());
}
});
put
(
Byte
.
class
,
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter
)
throws
IOException
{
return
iter
.
readNull
() ?
null
: (
byte
)
iter
.
readShort
();
}
});
put
(
short
.
class
,
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter
)
throws
IOException
{
return
iter
.
readShort
();
}
});
put
(
Short
.
class
,
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter
)
throws
IOException
{
return
iter
.
readNull
() ?
null
:
iter
.
readShort
();
}
});
put
(
int
.
class
,
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter
)
throws
IOException
{
return
iter
.
readInt
();
}
});
put
(
Integer
.
class
,
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter
)
throws
IOException
{
return
iter
.
readNull
() ?
null
:
iter
.
readInt
();
}
});
put
(
char
.
class
,
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter
)
throws
IOException
{
return
(
char
)
iter
.
readInt
();
}
});
put
(
Character
.
class
,
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter
)
throws
IOException
{
return
iter
.
readNull
() ?
null
: (
char
)
iter
.
readInt
();
}
});
put
(
long
.
class
,
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter
)
throws
IOException
{
return
iter
.
readLong
();
}
});
put
(
Long
.
class
,
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter
)
throws
IOException
{
return
iter
.
readNull
() ?
null
:
iter
.
readLong
();
}
});
put
(
BigDecimal
.
class
,
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter
)
throws
IOException
{
return
iter
.
readBigDecimal
();
}
});
put
(
BigInteger
.
class
,
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter
)
throws
IOException
{
return
iter
.
readBigInteger
();
}
});
put
(
String
.
class
,
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter
)
throws
IOException
{
return
iter
.
readString
();
}
});
put
(
Object
.
class
,
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter
)
throws
IOException
{
return
iter
.
read
();
}
});
put
(
Any
.
class
,
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter
)
throws
IOException
{
return
iter
.
readAny
();
}
});
}};
public
static
String
genReadOp
(
Type
type
) {
String
cacheKey
=
TypeLiteral
.
create
(
type
).
getDecoderCacheKey
();
return
String
.
format
(
"(%s)%s"
,
getTypeName
(
type
),
genReadOp
(
cacheKey
,
type
));
}
public
static
String
getTypeName
(
Type
fieldType
) {
if
(
fieldType
instanceof
Class
) {
Class
clazz
= (
Class
)
fieldType
;
return
clazz
.
getCanonicalName
();
}
else
if
(
fieldType
instanceof
ParameterizedType
) {
ParameterizedType
pType
= (
ParameterizedType
)
fieldType
;
Class
clazz
= (
Class
)
pType
.
getRawType
();
return
clazz
.
getCanonicalName
();
}
else
if
(
fieldType
instanceof
WildcardType
) {
return
Object
.
class
.
getCanonicalName
();
}
else
{
throw
new
JsonException
(
"unsupported type: "
+
fieldType
);
}
}
static
String
genField
(
Binding
field
) {
String
fieldCacheKey
=
field
.
decoderCacheKey
();
Type
fieldType
=
field
.
valueType
;
return
String
.
format
(
"(%s)%s"
,
getTypeName
(
fieldType
),
genReadOp
(
fieldCacheKey
,
fieldType
));
}
private
static
String
genReadOp
(
String
cacheKey
,
Type
valueType
) {
// the field decoder might be registered directly
Decoder
decoder
=
JsoniterSpi
.
getDecoder
(
cacheKey
);
if
(
decoder
==
null
) {
// if cache key is for field, and there is no field decoder specified
// update cache key for normal type
cacheKey
=
TypeLiteral
.
create
(
valueType
).
getDecoderCacheKey
();
decoder
=
JsoniterSpi
.
getDecoder
(
cacheKey
);
if
(
decoder
==
null
) {
if
(
valueType
instanceof
Class
) {
Class
clazz
= (
Class
)
valueType
;
String
nativeRead
=
NATIVE_READS
.
get
(
clazz
.
getCanonicalName
());
if
(
nativeRead
!=
null
) {
return
nativeRead
;
}
}
else
if
(
valueType
instanceof
WildcardType
) {
return
NATIVE_READS
.
get
(
Object
.
class
.
getCanonicalName
());
}
Codegen
.
getDecoder
(
cacheKey
,
valueType
);
if
(
Codegen
.
canStaticAccess
(
cacheKey
)) {
return
String
.
format
(
"%s.decode_(iter)"
,
cacheKey
);
}
else
{
// can not use static "decode_" method to access, go through codegen cache
return
String
.
format
(
"com.jsoniter.CodegenAccess.read(
\"
%s
\"
, iter)"
,
cacheKey
);
}
}
}
if
(
valueType
==
boolean
.
class
) {
if
(!(
decoder
instanceof
Decoder
.
BooleanDecoder
)) {
throw
new
JsonException
(
"decoder for "
+
cacheKey
+
"must implement Decoder.BooleanDecoder"
);
}
return
String
.
format
(
"com.jsoniter.CodegenAccess.readBoolean(
\"
%s
\"
, iter)"
,
cacheKey
);
}
if
(
valueType
==
byte
.
class
) {
if
(!(
decoder
instanceof
Decoder
.
ShortDecoder
)) {
throw
new
JsonException
(
"decoder for "
+
cacheKey
+
"must implement Decoder.ShortDecoder"
);
}
return
String
.
format
(
"com.jsoniter.CodegenAccess.readShort(
\"
%s
\"
, iter)"
,
cacheKey
);
}
if
(
valueType
==
short
.
class
) {
if
(!(
decoder
instanceof
Decoder
.
ShortDecoder
)) {
throw
new
JsonException
(
"decoder for "
+
cacheKey
+
"must implement Decoder.ShortDecoder"
);
}
return
String
.
format
(
"com.jsoniter.CodegenAccess.readShort(
\"
%s
\"
, iter)"
,
cacheKey
);
}
if
(
valueType
==
char
.
class
) {
if
(!(
decoder
instanceof
Decoder
.
IntDecoder
)) {
throw
new
JsonException
(
"decoder for "
+
cacheKey
+
"must implement Decoder.IntDecoder"
);
}
return
String
.
format
(
"com.jsoniter.CodegenAccess.readInt(
\"
%s
\"
, iter)"
,
cacheKey
);
}
if
(
valueType
==
int
.
class
) {
if
(!(
decoder
instanceof
Decoder
.
IntDecoder
)) {
throw
new
JsonException
(
"decoder for "
+
cacheKey
+
"must implement Decoder.IntDecoder"
);
}
return
String
.
format
(
"com.jsoniter.CodegenAccess.readInt(
\"
%s
\"
, iter)"
,
cacheKey
);
}
if
(
valueType
==
long
.
class
) {
if
(!(
decoder
instanceof
Decoder
.
LongDecoder
)) {
throw
new
JsonException
(
"decoder for "
+
cacheKey
+
"must implement Decoder.LongDecoder"
);
}
return
String
.
format
(
"com.jsoniter.CodegenAccess.readLong(
\"
%s
\"
, iter)"
,
cacheKey
);
}
if
(
valueType
==
float
.
class
) {
if
(!(
decoder
instanceof
Decoder
.
FloatDecoder
)) {
throw
new
JsonException
(
"decoder for "
+
cacheKey
+
"must implement Decoder.FloatDecoder"
);
}
return
String
.
format
(
"com.jsoniter.CodegenAccess.readFloat(
\"
%s
\"
, iter)"
,
cacheKey
);
}
if
(
valueType
==
double
.
class
) {
if
(!(
decoder
instanceof
Decoder
.
DoubleDecoder
)) {
throw
new
JsonException
(
"decoder for "
+
cacheKey
+
"must implement Decoder.DoubleDecoder"
);
}
return
String
.
format
(
"com.jsoniter.CodegenAccess.readDouble(
\"
%s
\"
, iter)"
,
cacheKey
);
}
return
String
.
format
(
"com.jsoniter.CodegenAccess.read(
\"
%s
\"
, iter)"
,
cacheKey
);
}
}
Back
|
FazBrowse Home
|
New Git URL