Example usage for com.fasterxml.jackson.databind JsonMappingException getPathReference

List of usage examples for com.fasterxml.jackson.databind JsonMappingException getPathReference

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind JsonMappingException getPathReference.

Prototype

public StringBuilder getPathReference(StringBuilder paramStringBuilder) 

Source Link

Usage

From source file:org.sherlok.FileBased.java

/**
 * Read a JSON-serialized object from file and parse it back to an object.
 * Performs extensive error-catching to provide useful information in case
 * of error./*w  w  w .j  a v  a  2s.  co  m*/
 * 
 * @param f
 *            the file to read
 * @param clazz
 *            the class to cast this object into
 * @return the parsed object
 * @throws SherlokException
 *             if the object cannot be found or parsed
 */
public static <T> T read(File f, Class<T> clazz) throws SherlokException {
    try {
        return MAPPER.readValue(new FileInputStream(f), clazz);

    } catch (FileNotFoundException io) {
        throw new SherlokException()
                .setMessage(clazz.getSimpleName().replaceAll("Def$", "") + " does not exist.")
                .setObject(f.getName()).setDetails(io.toString());

    } catch (UnrecognizedPropertyException upe) {
        throw new SherlokException().setMessage("Unrecognized field \"" + upe.getPropertyName() + "\"")
                .setObject(clazz.getSimpleName().replaceAll("Def$", "") + " " + f.getName())
                .setDetails(upe.getMessageSuffix());

    } catch (JsonMappingException jme) {

        StringBuilder sb = new StringBuilder();
        sb.append("cannot read ");
        jme.getPathReference(sb);
        sb.append(" in '" + f.getName() + "': ");
        sb.append(" " + jme.getOriginalMessage());

        throw new SherlokException()
                .setMessage(sb.toString().replaceAll("org\\.sherlok\\.mappings\\.\\w+Def\\[", "["));

    } catch (JsonParseException jpe) {

        throw new SherlokException().setMessage("Could not parse JSON object of type " + clazz.getSimpleName())
                .setObject(f.getName().replaceAll("Def$", "")).setDetails(jpe.getMessage());

    } catch (Exception e) {
        throw new SherlokException().setMessage(e.getMessage()).setObject(f.getName().replaceAll("Def$", ""))
                .setDetails(e.getStackTrace());
    }
}