Skip to content

Commit

Permalink
Implicitly convert booleans and numbers to strings when decoding json
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c authored and Grigory Mischenko committed Sep 20, 2018
1 parent ec63251 commit b99a8cf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions java/client/src/org/openqa/selenium/json/StringCoercer.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ public boolean test(Class<?> type) {
public BiFunction<JsonInput, PropertySetting, String> apply(Type type) {
return (jsonInput, setting) -> {
switch (jsonInput.peek()) {
case BOOLEAN:
return String.valueOf(jsonInput.nextBoolean());

case NAME:
return jsonInput.nextName();

case NUMBER:
return String.valueOf(jsonInput.nextNumber());

case STRING:
return jsonInput.nextString();

Expand Down
18 changes: 18 additions & 0 deletions java/client/test/org/openqa/selenium/json/JsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,24 @@ public void canConvertAnEnumWithALowerCaseValue() {
assertEquals(Proxy.ProxyType.PAC, type);
}

@Test
public void canCoerceSimpleValuesToStrings() {
Map<String, Object> value = ImmutableMap.of(
"boolean", true,
"integer", 42,
"float", 3.14);

Json json = new Json();
String raw = json.toJson(value);
Map<String, String> roundTripped = json.toType(
raw,
new TypeToken<Map<String, String>>(){}.getType());

assertEquals("true", roundTripped.get("boolean"));
assertEquals("42", roundTripped.get("integer"));
assertEquals("3.14", roundTripped.get("float"));
}

public static class SimpleBean {

private String value;
Expand Down

0 comments on commit b99a8cf

Please sign in to comment.