Revert "Silently cast to String"

This reverts commit f35876b7

Signed-off-by: Paulo Lopes <pmlopes@gmail.com>
This commit is contained in:
Paulo Lopes
2019-12-06 13:39:48 +01:00
parent 9d0c5c29cc
commit 9f7ea3317d
4 changed files with 10 additions and 15 deletions

View File

@@ -96,6 +96,7 @@ public class JsonArray implements Iterable<Object>, ClusterSerializable, Shareab
*
* @param pos the position in the array
* @return the String, or null if a null value present
* @throws java.lang.ClassCastException if the value cannot be converted to String
*/
public String getString(int pos) {
Object val = list.get(pos);
@@ -114,8 +115,7 @@ public class JsonArray implements Iterable<Object>, ClusterSerializable, Shareab
return ((Enum) val).name();
}
// silently cast to String
return val.toString();
throw new ClassCastException("class " + val.getClass().getName() + " cannot be cast to class java.lang.String");
}
/**

View File

@@ -125,6 +125,7 @@ public class JsonObject implements Iterable<Map.Entry<String, Object>>, ClusterS
*
* @param key the key to return the value for
* @return the value or null if no value for that key
* @throws java.lang.ClassCastException if the value is not a String
*/
public String getString(String key) {
Objects.requireNonNull(key);
@@ -143,8 +144,7 @@ public class JsonObject implements Iterable<Map.Entry<String, Object>>, ClusterS
return ((Enum) val).name();
}
// silently cast to String
return val.toString();
throw new ClassCastException("class " + val.getClass().getName() + " cannot be cast to class java.lang.String");
}
/**

View File

@@ -193,11 +193,9 @@ public class JsonArrayTest {
jsonArray.add(123);
try {
jsonArray.getString(1);
// OK, get string will not throw if type is not String
// instead the value is converted to String using the toString()
assertEquals("123", jsonArray.getString(1));
} catch (ClassCastException e) {
fail();
} catch (ClassCastException e) {
// OK
}
jsonArray.addNull();
assertNull(jsonArray.getString(2));

View File

@@ -331,11 +331,9 @@ public class JsonObjectTest {
jsonObject.put("bar", 123);
try {
jsonObject.getString("bar");
// OK, get string will not throw if type is not String
// instead the value is converted to String using the toString()
assertEquals("123", jsonObject.getString("bar"));
} catch (ClassCastException e) {
fail();
} catch (ClassCastException e) {
// Ok
}
// Null and absent values
@@ -360,10 +358,9 @@ public class JsonObjectTest {
jsonObject.put("bar", 123);
try {
jsonObject.getString("bar", "wibble");
// Ok, values are silently casted to string
assertEquals("123", jsonObject.getString("bar", "wibble"));
} catch (ClassCastException e) {
fail();
} catch (ClassCastException e) {
// Ok
}
// Null and absent values