Commit f2d3f51f authored by Stephane Nicoll's avatar Stephane Nicoll

Merge branch '1.5.x'

parents c7f5f073 4568f14c
...@@ -17,100 +17,114 @@ ...@@ -17,100 +17,114 @@
package org.springframework.boot.configurationprocessor.json; package org.springframework.boot.configurationprocessor.json;
class JSON { class JSON {
/** /**
* Returns the input if it is a JSON-permissible value; throws otherwise. * Returns the input if it is a JSON-permissible value; throws otherwise.
*/ */
static double checkDouble(double d) throws JSONException { static double checkDouble(double d) throws JSONException {
if (Double.isInfinite(d) || Double.isNaN(d)) { if (Double.isInfinite(d) || Double.isNaN(d)) {
throw new JSONException("Forbidden numeric value: " + d); throw new JSONException("Forbidden numeric value: " + d);
} }
return d; return d;
} }
static Boolean toBoolean(Object value) { static Boolean toBoolean(Object value) {
if (value instanceof Boolean) { if (value instanceof Boolean) {
return (Boolean) value; return (Boolean) value;
} else if (value instanceof String) { }
String stringValue = (String) value; else if (value instanceof String) {
if ("true".equalsIgnoreCase(stringValue)) { String stringValue = (String) value;
return true; if ("true".equalsIgnoreCase(stringValue)) {
} else if ("false".equalsIgnoreCase(stringValue)) { return true;
return false; }
} else if ("false".equalsIgnoreCase(stringValue)) {
} return false;
return null; }
} }
return null;
}
static Double toDouble(Object value) { static Double toDouble(Object value) {
if (value instanceof Double) { if (value instanceof Double) {
return (Double) value; return (Double) value;
} else if (value instanceof Number) { }
return ((Number) value).doubleValue(); else if (value instanceof Number) {
} else if (value instanceof String) { return ((Number) value).doubleValue();
try { }
return Double.valueOf((String) value); else if (value instanceof String) {
} catch (NumberFormatException ignored) { try {
} return Double.valueOf((String) value);
} }
return null; catch (NumberFormatException ignored) {
} }
}
return null;
}
static Integer toInteger(Object value) { static Integer toInteger(Object value) {
if (value instanceof Integer) { if (value instanceof Integer) {
return (Integer) value; return (Integer) value;
} else if (value instanceof Number) { }
return ((Number) value).intValue(); else if (value instanceof Number) {
} else if (value instanceof String) { return ((Number) value).intValue();
try { }
return (int) Double.parseDouble((String) value); else if (value instanceof String) {
} catch (NumberFormatException ignored) { try {
} return (int) Double.parseDouble((String) value);
} }
return null; catch (NumberFormatException ignored) {
} }
}
return null;
}
static Long toLong(Object value) { static Long toLong(Object value) {
if (value instanceof Long) { if (value instanceof Long) {
return (Long) value; return (Long) value;
} else if (value instanceof Number) { }
return ((Number) value).longValue(); else if (value instanceof Number) {
} else if (value instanceof String) { return ((Number) value).longValue();
try { }
return (long) Double.parseDouble((String) value); else if (value instanceof String) {
} catch (NumberFormatException ignored) { try {
} return (long) Double.parseDouble((String) value);
} }
return null; catch (NumberFormatException ignored) {
} }
}
return null;
}
static String toString(Object value) { static String toString(Object value) {
if (value instanceof String) { if (value instanceof String) {
return (String) value; return (String) value;
} else if (value != null) { }
return String.valueOf(value); else if (value != null) {
} return String.valueOf(value);
return null; }
} return null;
}
public static JSONException typeMismatch(Object indexOrName, Object actual, public static JSONException typeMismatch(Object indexOrName, Object actual,
String requiredType) throws JSONException { String requiredType) throws JSONException {
if (actual == null) { if (actual == null) {
throw new JSONException("Value at " + indexOrName + " is null."); throw new JSONException("Value at " + indexOrName + " is null.");
} else { }
throw new JSONException("Value " + actual + " at " + indexOrName else {
+ " of type " + actual.getClass().getName() throw new JSONException("Value " + actual + " at " + indexOrName
+ " cannot be converted to " + requiredType); + " of type " + actual.getClass().getName()
} + " cannot be converted to " + requiredType);
} }
}
public static JSONException typeMismatch(Object actual, String requiredType) public static JSONException typeMismatch(Object actual, String requiredType)
throws JSONException { throws JSONException {
if (actual == null) { if (actual == null) {
throw new JSONException("Value is null."); throw new JSONException("Value is null.");
} else { }
throw new JSONException("Value " + actual else {
+ " of type " + actual.getClass().getName() throw new JSONException("Value " + actual
+ " cannot be converted to " + requiredType); + " of type " + actual.getClass().getName()
} + " cannot be converted to " + requiredType);
} }
}
} }
...@@ -43,7 +43,7 @@ package org.springframework.boot.configurationprocessor.json; ...@@ -43,7 +43,7 @@ package org.springframework.boot.configurationprocessor.json;
*/ */
public class JSONException extends Exception { public class JSONException extends Exception {
public JSONException(String s) { public JSONException(String s) {
super(s); super(s);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment