Skip to content

Commit

Permalink
safari: Fixing Response conversion from JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Jun 9, 2015
1 parent 71166fe commit 26bf0bc
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions java/client/src/org/openqa/selenium/remote/JsonToBeanConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@

public class JsonToBeanConverter {

public <T> T convert(Class<T> clazz, Object text) throws JsonException {
public <T> T convert(Class<T> clazz, Object source) throws JsonException {
try {
return convert(clazz, text, 0);
return convert(clazz, source, 0);
} catch (JsonSyntaxException e) {
throw new JsonException(e, text);
throw new JsonException(e, source);
}
}

@SuppressWarnings("unchecked")
private <T> T convert(Class<T> clazz, Object text, int depth) {
if (text == null) {
private <T> T convert(Class<T> clazz, Object source, int depth) {
if (source == null) {
return null;
}

if (text instanceof JsonElement) {
JsonElement json = (JsonElement) text;
if (source instanceof JsonElement) {
JsonElement json = (JsonElement) source;

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

if (isPrimitive(text.getClass())) {
return (T) text;
if (isPrimitive(source.getClass())) {
return (T) source;
}

if (isEnum(clazz, text)) {
return (T) convertEnum(clazz, text);
if (isEnum(clazz, source)) {
return (T) convertEnum(clazz, source);
}

if ("".equals(String.valueOf(text))) {
return (T) text;
if ("".equals(String.valueOf(source))) {
return (T) source;
}

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

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

if (Response.class.equals(clazz)) {
Response response = new Response();
JsonObject json = new JsonParser().parse((String) text).getAsJsonObject();
JsonObject json = source instanceof JsonObject
? (JsonObject) source
: new JsonParser().parse((String) source).getAsJsonObject();

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

if (SessionId.class.equals(clazz)) {
// Stupid heuristic to tell if we are dealing with a selenium 2 or 3 session id.
JsonElement json = text instanceof String
? new JsonParser().parse((String) text).getAsJsonObject() : (JsonElement) text;
JsonElement json = source instanceof String
? new JsonParser().parse((String) source).getAsJsonObject() : (JsonElement) source;
if (json.isJsonPrimitive()) {
return (T) new SessionId(json.getAsString());
} else {
Expand All @@ -148,25 +150,25 @@ private <T> T convert(Class<T> clazz, Object text, int depth) {
}

if (Capabilities.class.isAssignableFrom(clazz)) {
JsonObject json = text instanceof JsonElement
? ((JsonElement) text).getAsJsonObject()
: new JsonParser().parse(text.toString()).getAsJsonObject();
JsonObject json = source instanceof JsonElement
? ((JsonElement) source).getAsJsonObject()
: new JsonParser().parse(source.toString()).getAsJsonObject();
Map<String, Object> map = convertMap(json.getAsJsonObject(), depth);
return (T) new DesiredCapabilities(map);
}

if (Date.class.equals(clazz)) {
return (T) new Date(Long.valueOf(String.valueOf(text)));
return (T) new Date(Long.valueOf(String.valueOf(source)));
}

if (text instanceof String && !((String) text).startsWith("{") && Object.class.equals(clazz)) {
return (T) text;
if (source instanceof String && !((String) source).startsWith("{") && Object.class.equals(clazz)) {
return (T) source;
}

Method fromJson = getMethod(clazz, "fromJson");
if (fromJson != null) {
try {
return (T) fromJson.invoke(null, text.toString());
return (T) fromJson.invoke(null, source.toString());
} catch (IllegalArgumentException e) {
throw new WebDriverException(e);
} catch (IllegalAccessException e) {
Expand All @@ -177,13 +179,13 @@ private <T> T convert(Class<T> clazz, Object text, int depth) {
}

if (depth == 0) {
if (text instanceof String) {
text = new JsonParser().parse((String) text);
if (source instanceof String) {
source = new JsonParser().parse((String) source);
}
}

if (text instanceof JsonElement) {
JsonElement element = (JsonElement) text;
if (source instanceof JsonElement) {
JsonElement element = (JsonElement) source;

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

return (T) text; // Crap shoot here; probably a string.
return (T) source; // Crap shoot here; probably a string.
}

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

0 comments on commit 26bf0bc

Please sign in to comment.