Skip to content

Commit 26bf0bc

Browse files
committed
safari: Fixing Response conversion from JSON
1 parent 71166fe commit 26bf0bc

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

java/client/src/org/openqa/selenium/remote/JsonToBeanConverter.java

+31-29
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,22 @@
3737

3838
public class JsonToBeanConverter {
3939

40-
public <T> T convert(Class<T> clazz, Object text) throws JsonException {
40+
public <T> T convert(Class<T> clazz, Object source) throws JsonException {
4141
try {
42-
return convert(clazz, text, 0);
42+
return convert(clazz, source, 0);
4343
} catch (JsonSyntaxException e) {
44-
throw new JsonException(e, text);
44+
throw new JsonException(e, source);
4545
}
4646
}
4747

4848
@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) {
5151
return null;
5252
}
5353

54-
if (text instanceof JsonElement) {
55-
JsonElement json = (JsonElement) text;
54+
if (source instanceof JsonElement) {
55+
JsonElement json = (JsonElement) source;
5656

5757
if (json.isJsonPrimitive()) {
5858
JsonPrimitive jp = json.getAsJsonPrimitive();
@@ -77,20 +77,20 @@ private <T> T convert(Class<T> clazz, Object text, int depth) {
7777
}
7878
}
7979

80-
if (isPrimitive(text.getClass())) {
81-
return (T) text;
80+
if (isPrimitive(source.getClass())) {
81+
return (T) source;
8282
}
8383

84-
if (isEnum(clazz, text)) {
85-
return (T) convertEnum(clazz, text);
84+
if (isEnum(clazz, source)) {
85+
return (T) convertEnum(clazz, source);
8686
}
8787

88-
if ("".equals(String.valueOf(text))) {
89-
return (T) text;
88+
if ("".equals(String.valueOf(source))) {
89+
return (T) source;
9090
}
9191

9292
if (Command.class.equals(clazz)) {
93-
JsonObject json = new JsonParser().parse((String) text).getAsJsonObject();
93+
JsonObject json = new JsonParser().parse((String) source).getAsJsonObject();
9494

9595
SessionId sessionId = null;
9696
if (json.has("sessionId") && !json.get("sessionId").isJsonNull()) {
@@ -108,7 +108,9 @@ private <T> T convert(Class<T> clazz, Object text, int depth) {
108108

109109
if (Response.class.equals(clazz)) {
110110
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();
112114

113115
if (json.has("state") && ! json.get("state").isJsonNull()) {
114116
String state = json.get("state").getAsString();
@@ -138,8 +140,8 @@ private <T> T convert(Class<T> clazz, Object text, int depth) {
138140

139141
if (SessionId.class.equals(clazz)) {
140142
// 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;
143145
if (json.isJsonPrimitive()) {
144146
return (T) new SessionId(json.getAsString());
145147
} else {
@@ -148,25 +150,25 @@ private <T> T convert(Class<T> clazz, Object text, int depth) {
148150
}
149151

150152
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();
154156
Map<String, Object> map = convertMap(json.getAsJsonObject(), depth);
155157
return (T) new DesiredCapabilities(map);
156158
}
157159

158160
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)));
160162
}
161163

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;
164166
}
165167

166168
Method fromJson = getMethod(clazz, "fromJson");
167169
if (fromJson != null) {
168170
try {
169-
return (T) fromJson.invoke(null, text.toString());
171+
return (T) fromJson.invoke(null, source.toString());
170172
} catch (IllegalArgumentException e) {
171173
throw new WebDriverException(e);
172174
} catch (IllegalAccessException e) {
@@ -177,13 +179,13 @@ private <T> T convert(Class<T> clazz, Object text, int depth) {
177179
}
178180

179181
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);
182184
}
183185
}
184186

185-
if (text instanceof JsonElement) {
186-
JsonElement element = (JsonElement) text;
187+
if (source instanceof JsonElement) {
188+
JsonElement element = (JsonElement) source;
187189

188190
if (element.isJsonPrimitive()) {
189191
return (T) convertJsonPrimitive(element.getAsJsonPrimitive());
@@ -210,7 +212,7 @@ private <T> T convert(Class<T> clazz, Object text, int depth) {
210212
}
211213
}
212214

213-
return (T) text; // Crap shoot here; probably a string.
215+
return (T) source; // Crap shoot here; probably a string.
214216
}
215217

216218
private Method getMethod(Class<?> clazz, String methodName) {

0 commit comments

Comments
 (0)