FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JSON-java/JSONMappedObject.java at master · figgefred/JSON-java · GitHub
figgefred
/
JSON-java
Public
forked from
stleary/JSON-java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JSON-java
/
JSONMappedObject.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
102 lines (84 loc) · 3.63 KB
Breadcrumbs
JSON-java
/
JSONMappedObject.java
Copy path
File metadata and controls
102 lines (84 loc) · 3.63 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
package
com
.
tethik
.
json
;
import
java
.
lang
.
reflect
.
Array
;
import
java
.
lang
.
reflect
.
Constructor
;
import
java
.
lang
.
reflect
.
Field
;
import
java
.
lang
.
reflect
.
InvocationTargetException
;
public
abstract
class
JSONMappedObject
{
private
static
boolean
debug
=
false
;
protected
Object
getValue
(
Class
<?>
type
,
Object
obj
)
throws
InstantiationException
,
IllegalAccessException
,
IllegalArgumentException
,
InvocationTargetException
,
NoSuchMethodException
,
SecurityException
,
JSONException
{
Object
value
;
if
(
JSONMappedObject
.
class
.
isAssignableFrom
(
type
)) {
// Recursion
if
(
debug
)
System
.
out
.
println
(
"Object found"
);
Constructor
<?>
constructor
=
type
.
getConstructor
(
JSONObject
.
class
);
if
(
debug
)
System
.
out
.
println
(
"Constructor created"
);
value
=
constructor
.
newInstance
((
JSONObject
)
obj
);
}
else
if
(
type
.
isEnum
()) {
// Enums
if
(
debug
)
System
.
out
.
println
(
"Enum found"
);
value
=
Enum
.
valueOf
((
Class
<
Enum
>)
type
, (
String
)
obj
);
}
else
if
(
type
.
isArray
()) {
JSONArray
array
= (
JSONArray
)
obj
;
Object
[]
objects
=
new
Object
[
array
.
length
()];
for
(
int
i
=
0
;
i
<
array
.
length
();
i
++)
objects
[
i
] =
getValue
(
type
.
getComponentType
(),
array
.
get
(
i
));
value
=
objects
;
}
else
{
// Basic type. Or error :(
if
(
debug
)
System
.
out
.
println
(
"Basic found"
);
value
=
obj
;
}
return
value
;
}
protected
Object
getValue
(
Class
<?>
type
,
String
key
,
JSONObject
obj
)
throws
NoSuchMethodException
,
SecurityException
,
JSONException
,
InstantiationException
,
IllegalAccessException
,
IllegalArgumentException
,
InvocationTargetException
{
Object
value
;
if
(
JSONMappedObject
.
class
.
isAssignableFrom
(
type
)) {
// Recursion
if
(
debug
)
System
.
out
.
println
(
"Object found"
);
Constructor
<?>
constructor
=
type
.
getConstructor
(
JSONObject
.
class
);
if
(
debug
)
System
.
out
.
println
(
"Constructor created"
);
value
=
constructor
.
newInstance
(
obj
.
getJSONObject
(
key
));
}
else
if
(
type
.
isEnum
()) {
// Enums
if
(
debug
)
System
.
out
.
println
(
"Enum found"
);
value
=
Enum
.
valueOf
((
Class
<
Enum
>)
type
,
obj
.
getString
(
key
));
}
else
if
(
type
.
isArray
()) {
JSONArray
array
=
obj
.
getJSONArray
(
key
);
Object
objects
=
type
.
cast
(
Array
.
newInstance
(
type
.
getComponentType
(),
array
.
length
()));
for
(
int
i
=
0
;
i
<
array
.
length
();
i
++)
Array
.
set
(
objects
,
i
,
type
.
getComponentType
().
cast
(
getValue
(
type
.
getComponentType
(),
array
.
get
(
i
))));
value
=
objects
;
}
else
{
// Basic type. Or error :(
if
(
debug
)
System
.
out
.
println
(
"Basic found"
);
value
=
obj
.
get
(
key
);
}
return
value
;
}
protected
void
parseJson
(
JSONObject
obj
)
throws
JSONException
,
IllegalArgumentException
,
IllegalAccessException
,
InstantiationException
,
NoSuchMethodException
,
SecurityException
,
InvocationTargetException
{
if
(
debug
)
System
.
out
.
println
(
"parseJson called on "
+
this
.
getClass
().
getName
());
Field
[]
fields
=
this
.
getClass
().
getDeclaredFields
();
for
(
Field
field
:
fields
) {
field
.
setAccessible
(
true
);
if
(
debug
)
System
.
out
.
println
(
"Checking "
+
field
.
getName
());
if
(!
field
.
isAnnotationPresent
(
JSONObj
.
class
))
continue
;
String
key
=
field
.
getName
();
Class
<?>
type
=
field
.
getType
();
if
(
debug
)
System
.
out
.
println
(
"Annotation found on "
+
key
+
" type: "
+
type
.
getName
());
Object
value
=
getValue
(
type
,
key
,
obj
);
field
.
set
(
this
,
value
);
if
(
debug
)
System
.
out
.
println
(
"Field set to "
+
value
);
}
}
public
JSONMappedObject
(
JSONObject
obj
)
throws
JSONException
{
try
{
parseJson
(
obj
);
}
catch
(
IllegalArgumentException
|
IllegalAccessException
|
InstantiationException
|
NoSuchMethodException
|
SecurityException
|
InvocationTargetException
e
) {
throw
new
JSONException
(
e
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL