FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java/src/main/java/com/jsoniter/CodegenImplEnum.java at master · baowchen/java · GitHub
baowchen
/
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
/
CodegenImplEnum.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
94 lines (87 loc) · 4.16 KB
Breadcrumbs
java
/
src
/
main
/
java
/
com
/
jsoniter
/
CodegenImplEnum.java
Copy path
File metadata and controls
94 lines (87 loc) · 4.16 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
package
com
.
jsoniter
;
import
com
.
jsoniter
.
spi
.
ClassInfo
;
import
java
.
util
.*;
class
CodegenImplEnum
{
public
static
String
genEnum
(
ClassInfo
classInfo
) {
StringBuilder
lines
=
new
StringBuilder
();
append
(
lines
,
"if (iter.readNull()) { return null; }"
);
append
(
lines
,
"com.jsoniter.spi.Slice field = com.jsoniter.CodegenAccess.readSlice(iter);"
);
append
(
lines
,
"switch (field.len()) {"
);
append
(
lines
,
renderTriTree
(
buildTriTree
(
Arrays
.
asList
(
classInfo
.
clazz
.
getEnumConstants
()))));
append
(
lines
,
"}"
);
// end of switch
append
(
lines
,
String
.
format
(
"throw iter.reportError(
\"
decode enum
\"
, field +
\"
is not valid enum for %s
\"
);"
,
classInfo
.
clazz
.
getName
()));
return
lines
.
toString
();
}
private
static
Map
<
Integer
,
Object
>
buildTriTree
(
List
<
Object
>
allConsts
) {
Map
<
Integer
,
Object
>
trieTree
=
new
HashMap
<
Integer
,
Object
>();
for
(
Object
e
:
allConsts
) {
byte
[]
fromNameBytes
=
e
.
toString
().
getBytes
();
Map
<
Byte
,
Object
>
current
= (
Map
<
Byte
,
Object
>)
trieTree
.
get
(
fromNameBytes
.
length
);
if
(
current
==
null
) {
current
=
new
HashMap
<
Byte
,
Object
>();
trieTree
.
put
(
fromNameBytes
.
length
,
current
);
}
for
(
int
i
=
0
;
i
<
fromNameBytes
.
length
-
1
;
i
++) {
byte
b
=
fromNameBytes
[
i
];
Map
<
Byte
,
Object
>
next
= (
Map
<
Byte
,
Object
>)
current
.
get
(
b
);
if
(
next
==
null
) {
next
=
new
HashMap
<
Byte
,
Object
>();
current
.
put
(
b
,
next
);
}
current
=
next
;
}
current
.
put
(
fromNameBytes
[
fromNameBytes
.
length
-
1
],
e
);
}
return
trieTree
;
}
private
static
String
renderTriTree
(
Map
<
Integer
,
Object
>
trieTree
) {
StringBuilder
switchBody
=
new
StringBuilder
();
for
(
Map
.
Entry
<
Integer
,
Object
>
entry
:
trieTree
.
entrySet
()) {
Integer
len
=
entry
.
getKey
();
append
(
switchBody
,
"case "
+
len
+
": "
);
Map
<
Byte
,
Object
>
current
= (
Map
<
Byte
,
Object
>)
entry
.
getValue
();
addFieldDispatch
(
switchBody
,
len
,
0
,
current
,
new
ArrayList
<
Byte
>());
append
(
switchBody
,
"break;"
);
}
return
switchBody
.
toString
();
}
private
static
void
addFieldDispatch
(
StringBuilder
lines
,
int
len
,
int
i
,
Map
<
Byte
,
Object
>
current
,
List
<
Byte
>
bytesToCompare
) {
for
(
Map
.
Entry
<
Byte
,
Object
>
entry
:
current
.
entrySet
()) {
Byte
b
=
entry
.
getKey
();
if
(
i
==
len
-
1
) {
append
(
lines
,
"if ("
);
for
(
int
j
=
0
;
j
<
bytesToCompare
.
size
();
j
++) {
Byte
a
=
bytesToCompare
.
get
(
j
);
append
(
lines
,
String
.
format
(
"field.at(%d)==%s && "
,
i
-
bytesToCompare
.
size
() +
j
,
a
));
}
append
(
lines
,
String
.
format
(
"field.at(%d)==%s"
,
i
,
b
));
append
(
lines
,
") {"
);
Object
e
=
entry
.
getValue
();
append
(
lines
,
String
.
format
(
"return %s.%s;"
,
e
.
getClass
().
getName
(),
e
.
toString
()));
append
(
lines
,
"}"
);
continue
;
}
Map
<
Byte
,
Object
>
next
= (
Map
<
Byte
,
Object
>)
entry
.
getValue
();
if
(
next
.
size
() ==
1
) {
ArrayList
<
Byte
>
nextBytesToCompare
=
new
ArrayList
<
Byte
>(
bytesToCompare
);
nextBytesToCompare
.
add
(
b
);
addFieldDispatch
(
lines
,
len
,
i
+
1
,
next
,
nextBytesToCompare
);
continue
;
}
append
(
lines
,
"if ("
);
for
(
int
j
=
0
;
j
<
bytesToCompare
.
size
();
j
++) {
Byte
a
=
bytesToCompare
.
get
(
j
);
append
(
lines
,
String
.
format
(
"field.at(%d)==%s && "
,
i
-
bytesToCompare
.
size
() +
j
,
a
));
}
append
(
lines
,
String
.
format
(
"field.at(%d)==%s"
,
i
,
b
));
append
(
lines
,
") {"
);
addFieldDispatch
(
lines
,
len
,
i
+
1
,
next
,
new
ArrayList
<
Byte
>());
append
(
lines
,
"}"
);
}
}
private
static
void
append
(
StringBuilder
lines
,
String
str
) {
lines
.
append
(
str
);
lines
.
append
(
"
\n
"
);
}
}
Back
|
FazBrowse Home
|
New Git URL