Skip to content

Commit 669362b

Browse files
committed
Add toJson to various classes
The reflective call to `toJson` was a relatively late addition in the development of Selenium. It is now time to use it.
1 parent 788936f commit 669362b

File tree

6 files changed

+26
-26
lines changed

6 files changed

+26
-26
lines changed

java/client/src/org/openqa/selenium/logging/LogEntries.java

+4
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,8 @@ public Iterator<LogEntry> iterator() {
7171
return entries.iterator();
7272
}
7373

74+
@Beta
75+
public List<LogEntry> toJson() {
76+
return getAll();
77+
}
7478
}

java/client/src/org/openqa/selenium/logging/LogEntry.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.openqa.selenium.logging;
1919

20+
import org.openqa.selenium.Beta;
21+
2022
import java.text.SimpleDateFormat;
2123
import java.util.Date;
2224
import java.util.HashMap;
@@ -84,7 +86,8 @@ public String toString() {
8486
DATE_FORMAT.get().format(new Date(timestamp)), level, message);
8587
}
8688

87-
public Map<String, Object> toMap() {
89+
@Beta
90+
public Map<String, Object> toJson() {
8891
Map<String, Object> map = new HashMap<>();
8992
map.put("timestamp", timestamp);
9093
map.put("level", level);

java/client/src/org/openqa/selenium/logging/LoggingPreferences.java

+12
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717

1818
package org.openqa.selenium.logging;
1919

20+
import org.openqa.selenium.Beta;
21+
2022
import java.io.Serializable;
2123
import java.util.HashMap;
2224
import java.util.HashSet;
2325
import java.util.Map;
2426
import java.util.Set;
27+
import java.util.TreeMap;
2528
import java.util.logging.Level;
2629

2730
/**
@@ -83,4 +86,13 @@ public LoggingPreferences addPreferences(LoggingPreferences prefs) {
8386
}
8487
return this;
8588
}
89+
90+
@Beta
91+
public Map<String, Object> toJson() {
92+
TreeMap<String, Object> converted = new TreeMap<>();
93+
for (String logType : getEnabledLogTypes()) {
94+
converted.put(logType, LogLevelMapping.getName(getLevel(logType)));
95+
}
96+
return converted;
97+
}
8698
}

java/client/src/org/openqa/selenium/logging/SessionLogs.java

+5
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,9 @@ public static SessionLogs fromJSON(JsonObject rawSessionLogs) {
7777
}
7878
return sessionLogs;
7979
}
80+
81+
@Beta
82+
public Map<String, LogEntries> toJson() {
83+
return getAll();
84+
}
8085
}

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

-20
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@
2828

2929
import org.openqa.selenium.Cookie;
3030
import org.openqa.selenium.WebDriverException;
31-
import org.openqa.selenium.logging.LogEntries;
3231
import org.openqa.selenium.logging.LogLevelMapping;
33-
import org.openqa.selenium.logging.LoggingPreferences;
34-
import org.openqa.selenium.logging.SessionLogs;
3532

3633
import java.io.File;
3734
import java.lang.reflect.Array;
@@ -114,23 +111,6 @@ private JsonElement convertObject(Object toConvert, int maxDepth) throws Excepti
114111
return new JsonPrimitive(toConvert.toString());
115112
}
116113

117-
if (toConvert instanceof LoggingPreferences) {
118-
LoggingPreferences prefs = (LoggingPreferences) toConvert;
119-
JsonObject converted = new JsonObject();
120-
for (String logType : prefs.getEnabledLogTypes()) {
121-
converted.addProperty(logType, LogLevelMapping.getName(prefs.getLevel(logType)));
122-
}
123-
return converted;
124-
}
125-
126-
if (toConvert instanceof SessionLogs) {
127-
return convertObject(((SessionLogs)toConvert).getAll(), maxDepth - 1);
128-
}
129-
130-
if (toConvert instanceof LogEntries) {
131-
return convertObject(((LogEntries)toConvert).getAll(), maxDepth - 1);
132-
}
133-
134114
if (toConvert instanceof Map) {
135115
Map<String, Object> map = (Map<String, Object>) toConvert;
136116
if (map.size() == 1 && map.containsKey("w3c cookie")) {

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

+1-5
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,7 @@ private <T> T convert(Class<T> clazz, Object source, int depth) {
183183
if (fromJson != null) {
184184
try {
185185
return (T) fromJson.invoke(null, source.toString());
186-
} catch (IllegalArgumentException e) {
187-
throw new WebDriverException(e);
188-
} catch (IllegalAccessException e) {
189-
throw new WebDriverException(e);
190-
} catch (InvocationTargetException e) {
186+
} catch (ReflectiveOperationException e) {
191187
throw new WebDriverException(e);
192188
}
193189
}

0 commit comments

Comments
 (0)