Skip to content

Commit 1a0f31e

Browse files
committed
Allow the number coercer to implicitly coerce strings to numbers
1 parent ab4f24d commit 1a0f31e

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

java/client/src/org/openqa/selenium/json/NumberCoercer.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.primitives.Primitives;
2121

2222
import java.lang.reflect.Type;
23+
import java.math.BigDecimal;
2324
import java.util.Objects;
2425
import java.util.function.BiFunction;
2526
import java.util.function.Function;
@@ -42,7 +43,23 @@ public boolean test(Class<?> type) {
4243
@Override
4344
public BiFunction<JsonInput, PropertySetting, T> apply(Type ignored) {
4445
return (jsonInput, setting) -> {
45-
Number number = jsonInput.nextNumber();
46+
Number number;
47+
switch (jsonInput.peek()) {
48+
case NUMBER:
49+
number = jsonInput.nextNumber();
50+
break;
51+
52+
case STRING:
53+
try {
54+
number = new BigDecimal(jsonInput.nextString());
55+
} catch (NumberFormatException e) {
56+
throw new JsonException(e);
57+
}
58+
break;
59+
60+
default:
61+
throw new JsonException("Unable to coerce to a number: " + jsonInput.peek());
62+
}
4663
return mapper.apply(number);
4764
};
4865
}

0 commit comments

Comments
 (0)