|
|
@@ -102,38 +102,38 @@ public final class LexicalCast {
|
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
- public static String object2json(Object value) throws JsonCastException {
|
|
|
+ public static String object2json(Object value) throws LexicalCastException {
|
|
|
try {
|
|
|
return MAPPER.writeValueAsString(value);
|
|
|
} catch(IOException cause) {
|
|
|
- throw new JsonCastException(value.getClass(), cause);
|
|
|
+ throw new LexicalCastException(value.getClass()+" cannot be converted to JSON", cause);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static <T> T json2object(String value, Class<T> clazz) throws JsonCastException {
|
|
|
+ public static <T> T json2object(String value, Class<T> clazz) throws LexicalCastException {
|
|
|
try {
|
|
|
return MAPPER.readValue(value, clazz);
|
|
|
} catch(IOException cause) {
|
|
|
- throw new JsonCastException(value.getClass(), cause);
|
|
|
+ throw new LexicalCastException(clazz + " cannot be created from JSON", cause);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- public static <T> T json2object(String value, TypeToken<T> token) throws JsonCastException {
|
|
|
+ public static <T> T json2object(String value, TypeToken<T> token) throws LexicalCastException {
|
|
|
try {
|
|
|
return (T)MAPPER.readValue(value, TYPE_FACTORY.constructType( token.type() ) );
|
|
|
} catch(IOException cause) {
|
|
|
- throw new JsonCastException(value.getClass(), cause);
|
|
|
+ throw new LexicalCastException(token + " cannot be created from JSON", cause);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- public static <T> T cast(Object value, Class<T> clazz) {
|
|
|
+ public static <T> T cast(Object value, Class<T> clazz) throws ClassCastException {
|
|
|
return (T)rawCast(value, null, clazz);
|
|
|
}
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- public static <T> T cast(Object value, TypeToken<T> token) throws JsonCastException {
|
|
|
+ public static <T> T cast(Object value, TypeToken<T> token) throws LexicalCastException {
|
|
|
return (T)rawCast(value, token, TYPE_FACTORY.constructType( token.type() ).getRawClass());
|
|
|
}
|
|
|
|
|
|
@@ -198,10 +198,16 @@ public final class LexicalCast {
|
|
|
return ((Number)value).toString();
|
|
|
}
|
|
|
}
|
|
|
- if(token == null) {
|
|
|
- return LexicalCast.json2object( LexicalCast.object2json(value), clazz);
|
|
|
- } else {
|
|
|
- return LexicalCast.json2object( LexicalCast.object2json(value), token);
|
|
|
+ try {
|
|
|
+ if(token == null) {
|
|
|
+ return LexicalCast.json2object( LexicalCast.object2json(value), clazz);
|
|
|
+ } else {
|
|
|
+ return LexicalCast.json2object( LexicalCast.object2json(value), token);
|
|
|
+ }
|
|
|
+ } catch(LexicalCastException cause) {
|
|
|
+ ClassCastException error = new ClassCastException(value + " cannot be cast to " + clazz);
|
|
|
+ error.initCause(cause);
|
|
|
+ throw error;
|
|
|
}
|
|
|
|
|
|
}
|