Skip to content

Commit

Permalink
Allow json property setters to switch mid-read
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Apr 2, 2019
1 parent 49fb9d0 commit 0a59fc1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
13 changes: 8 additions & 5 deletions java/client/src/org/openqa/selenium/json/JsonInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ public class JsonInput implements Closeable {
this.setter = Objects.requireNonNull(setter);
}

public JsonInput propertySetting(PropertySetting setter) {
if (readPerformed) {
throw new JsonException("JsonInput has already been used and may not be modified");
}
/**
* Change how property setting is done. It's polite to set the value back once done processing.
* @param setter The new {@link PropertySetting} to use.
* @return The previous {@link PropertySetting} that has just been replaced.
*/
public PropertySetting propertySetting(PropertySetting setter) {
PropertySetting previous = this.setter;
this.setter = Objects.requireNonNull(setter);
return this;
return previous;
}

public JsonInput addCoercers(TypeCoercer<?>... coercers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ public static <T> T toType(String json, Type typeOfT) {
}

public static <T> T toType(JsonInput jsonInput, Type typeOfT) {
return jsonInput
.propertySetting(PropertySetting.BY_FIELD)
PropertySetting previous = jsonInput.propertySetting(PropertySetting.BY_FIELD);
T value = jsonInput
.addCoercers(new CapabilityMatcherCoercer(), new PrioritizerCoercer())
.read(typeOfT);
jsonInput.propertySetting(previous);
return value;
}

private static class SimpleClassNameCoercer<T> extends TypeCoercer<T> {
Expand Down

0 comments on commit 0a59fc1

Please sign in to comment.