FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java-json-api/src/main/java/com/github/webee/json/Utils.java at master · webee/java-json-api · GitHub
webee
/
java-json-api
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java-json-api
/
src
/
main
/
java
/
com
/
github
/
webee
/
json
/
Utils.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
131 lines (118 loc) · 3.81 KB
Breadcrumbs
java-json-api
/
src
/
main
/
java
/
com
/
github
/
webee
/
json
/
Utils.java
Copy path
File metadata and controls
131 lines (118 loc) · 3.81 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
package
com
.
github
.
webee
.
json
;
import
java
.
util
.
HashMap
;
import
java
.
util
.
Map
;
/**
* Created by webee on 16/11/25.
*/
public
final
class
Utils
{
/**
* judge value's json type.
* @param value the value to judge.
* @return the json type.
*/
public
static
JSONType
getType
(
Object
value
) {
if
(
value
==
null
) {
return
JSONType
.
Null
;
}
if
(
value
instanceof
String
) {
return
JSONType
.
String
;
}
else
if
(
value
instanceof
Number
) {
return
JSONType
.
Number
;
}
else
if
(
value
instanceof
Boolean
) {
return
JSONType
.
Boolean
;
}
else
if
(
value
instanceof
JSONObject
) {
return
JSONType
.
Object
;
}
else
if
(
value
instanceof
JSONArray
) {
return
JSONType
.
Array
;
}
else
if
(
value
instanceof
Map
) {
return
JSONType
.
Object
;
}
else
if
(
value
instanceof
Object
[]) {
return
JSONType
.
Array
;
}
return
null
;
}
/**
* convert JSONArray to a Object[] array mixed tree with pure java objects.
* @param array the JSONArray to convert.
* @return the pure java objects array mixed tree.
*/
public
static
Object
[]
arrayToObjects
(
JSONArray
array
) {
if
(
array
==
null
) {
return
null
;
}
Object
[]
res
=
new
Object
[
array
.
size
()];
for
(
int
i
=
0
;
i
<
array
.
size
();
i
++) {
res
[
i
] =
resolveValue
(
array
.
get
(
i
));
}
return
res
;
}
public
static
Object
[]
arrayToObjects
(
Object
[]
array
) {
if
(
array
==
null
) {
return
null
;
}
Object
[]
res
=
new
Object
[
array
.
length
];
for
(
int
i
=
0
;
i
<
array
.
length
;
i
++) {
res
[
i
] =
resolveValue
(
array
[
i
]);
}
return
res
;
}
/**
* convert JSONObject to a Map tree with pure java objects.
* @param object the JSONObject to convert.
* @return the pure java objects tree .
*/
public
static
Map
<
String
,
Object
>
objectToMap
(
JSONObject
object
) {
if
(
object
==
null
) {
return
null
;
}
Map
<
String
,
Object
>
res
=
new
HashMap
<>();
for
(
String
key
:
object
.
keySet
()) {
res
.
put
(
key
,
resolveValue
(
object
.
get
(
key
)));
}
return
res
;
}
public
static
Map
<
String
,
Object
>
objectToMap
(
Map
<
String
,
Object
>
object
) {
if
(
object
==
null
) {
return
null
;
}
Map
<
String
,
Object
>
res
=
new
HashMap
<>();
for
(
String
key
:
object
.
keySet
()) {
res
.
put
(
key
,
resolveValue
(
object
.
get
(
key
)));
}
return
res
;
}
/**
* convert value to pure java objects.
* @param value the value to convert.
* @return pure java object.
*/
public
static
Object
resolveValue
(
Object
value
) {
return
resolveValue
(
value
,
getType
(
value
));
}
public
static
Object
resolveValue
(
Object
value
,
JSONType
t
) {
if
(
t
!=
null
) {
switch
(
t
) {
case
Object
:
if
(
value
instanceof
JSONObject
) {
return
Utils
.
objectToMap
((
JSONObject
)
value
);
}
else
if
(
value
instanceof
Map
) {
return
Utils
.
objectToMap
((
Map
<
String
,
Object
>)
value
);
}
break
;
case
Array
:
if
(
value
instanceof
JSONArray
) {
return
Utils
.
arrayToObjects
((
JSONArray
)
value
);
}
else
if
(
value
instanceof
Object
[]) {
return
Utils
.
arrayToObjects
((
Object
[])
value
);
}
break
;
case
Null
:
return
null
;
default
:
return
value
;
}
}
// FIXME: some type should be ignored.
return
null
;
}
}
Back
|
FazBrowse Home
|
New Git URL