37
37
38
38
public class JsonToBeanConverter {
39
39
40
- public <T > T convert (Class <T > clazz , Object text ) throws JsonException {
40
+ public <T > T convert (Class <T > clazz , Object source ) throws JsonException {
41
41
try {
42
- return convert (clazz , text , 0 );
42
+ return convert (clazz , source , 0 );
43
43
} catch (JsonSyntaxException e ) {
44
- throw new JsonException (e , text );
44
+ throw new JsonException (e , source );
45
45
}
46
46
}
47
47
48
48
@ SuppressWarnings ("unchecked" )
49
- private <T > T convert (Class <T > clazz , Object text , int depth ) {
50
- if (text == null ) {
49
+ private <T > T convert (Class <T > clazz , Object source , int depth ) {
50
+ if (source == null ) {
51
51
return null ;
52
52
}
53
53
54
- if (text instanceof JsonElement ) {
55
- JsonElement json = (JsonElement ) text ;
54
+ if (source instanceof JsonElement ) {
55
+ JsonElement json = (JsonElement ) source ;
56
56
57
57
if (json .isJsonPrimitive ()) {
58
58
JsonPrimitive jp = json .getAsJsonPrimitive ();
@@ -77,20 +77,20 @@ private <T> T convert(Class<T> clazz, Object text, int depth) {
77
77
}
78
78
}
79
79
80
- if (isPrimitive (text .getClass ())) {
81
- return (T ) text ;
80
+ if (isPrimitive (source .getClass ())) {
81
+ return (T ) source ;
82
82
}
83
83
84
- if (isEnum (clazz , text )) {
85
- return (T ) convertEnum (clazz , text );
84
+ if (isEnum (clazz , source )) {
85
+ return (T ) convertEnum (clazz , source );
86
86
}
87
87
88
- if ("" .equals (String .valueOf (text ))) {
89
- return (T ) text ;
88
+ if ("" .equals (String .valueOf (source ))) {
89
+ return (T ) source ;
90
90
}
91
91
92
92
if (Command .class .equals (clazz )) {
93
- JsonObject json = new JsonParser ().parse ((String ) text ).getAsJsonObject ();
93
+ JsonObject json = new JsonParser ().parse ((String ) source ).getAsJsonObject ();
94
94
95
95
SessionId sessionId = null ;
96
96
if (json .has ("sessionId" ) && !json .get ("sessionId" ).isJsonNull ()) {
@@ -108,7 +108,9 @@ private <T> T convert(Class<T> clazz, Object text, int depth) {
108
108
109
109
if (Response .class .equals (clazz )) {
110
110
Response response = new Response ();
111
- JsonObject json = new JsonParser ().parse ((String ) text ).getAsJsonObject ();
111
+ JsonObject json = source instanceof JsonObject
112
+ ? (JsonObject ) source
113
+ : new JsonParser ().parse ((String ) source ).getAsJsonObject ();
112
114
113
115
if (json .has ("state" ) && ! json .get ("state" ).isJsonNull ()) {
114
116
String state = json .get ("state" ).getAsString ();
@@ -138,8 +140,8 @@ private <T> T convert(Class<T> clazz, Object text, int depth) {
138
140
139
141
if (SessionId .class .equals (clazz )) {
140
142
// Stupid heuristic to tell if we are dealing with a selenium 2 or 3 session id.
141
- JsonElement json = text instanceof String
142
- ? new JsonParser ().parse ((String ) text ).getAsJsonObject () : (JsonElement ) text ;
143
+ JsonElement json = source instanceof String
144
+ ? new JsonParser ().parse ((String ) source ).getAsJsonObject () : (JsonElement ) source ;
143
145
if (json .isJsonPrimitive ()) {
144
146
return (T ) new SessionId (json .getAsString ());
145
147
} else {
@@ -148,25 +150,25 @@ private <T> T convert(Class<T> clazz, Object text, int depth) {
148
150
}
149
151
150
152
if (Capabilities .class .isAssignableFrom (clazz )) {
151
- JsonObject json = text instanceof JsonElement
152
- ? ((JsonElement ) text ).getAsJsonObject ()
153
- : new JsonParser ().parse (text .toString ()).getAsJsonObject ();
153
+ JsonObject json = source instanceof JsonElement
154
+ ? ((JsonElement ) source ).getAsJsonObject ()
155
+ : new JsonParser ().parse (source .toString ()).getAsJsonObject ();
154
156
Map <String , Object > map = convertMap (json .getAsJsonObject (), depth );
155
157
return (T ) new DesiredCapabilities (map );
156
158
}
157
159
158
160
if (Date .class .equals (clazz )) {
159
- return (T ) new Date (Long .valueOf (String .valueOf (text )));
161
+ return (T ) new Date (Long .valueOf (String .valueOf (source )));
160
162
}
161
163
162
- if (text instanceof String && !((String ) text ).startsWith ("{" ) && Object .class .equals (clazz )) {
163
- return (T ) text ;
164
+ if (source instanceof String && !((String ) source ).startsWith ("{" ) && Object .class .equals (clazz )) {
165
+ return (T ) source ;
164
166
}
165
167
166
168
Method fromJson = getMethod (clazz , "fromJson" );
167
169
if (fromJson != null ) {
168
170
try {
169
- return (T ) fromJson .invoke (null , text .toString ());
171
+ return (T ) fromJson .invoke (null , source .toString ());
170
172
} catch (IllegalArgumentException e ) {
171
173
throw new WebDriverException (e );
172
174
} catch (IllegalAccessException e ) {
@@ -177,13 +179,13 @@ private <T> T convert(Class<T> clazz, Object text, int depth) {
177
179
}
178
180
179
181
if (depth == 0 ) {
180
- if (text instanceof String ) {
181
- text = new JsonParser ().parse ((String ) text );
182
+ if (source instanceof String ) {
183
+ source = new JsonParser ().parse ((String ) source );
182
184
}
183
185
}
184
186
185
- if (text instanceof JsonElement ) {
186
- JsonElement element = (JsonElement ) text ;
187
+ if (source instanceof JsonElement ) {
188
+ JsonElement element = (JsonElement ) source ;
187
189
188
190
if (element .isJsonPrimitive ()) {
189
191
return (T ) convertJsonPrimitive (element .getAsJsonPrimitive ());
@@ -210,7 +212,7 @@ private <T> T convert(Class<T> clazz, Object text, int depth) {
210
212
}
211
213
}
212
214
213
- return (T ) text ; // Crap shoot here; probably a string.
215
+ return (T ) source ; // Crap shoot here; probably a string.
214
216
}
215
217
216
218
private Method getMethod (Class <?> clazz , String methodName ) {
0 commit comments