Example usage for com.fasterxml.jackson.databind ObjectMapper readValue

List of usage examples for com.fasterxml.jackson.databind ObjectMapper readValue

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper readValue.

Prototype

@SuppressWarnings("unchecked")
    public <T> T readValue(byte[] src, JavaType valueType)
            throws IOException, JsonParseException, JsonMappingException 

Source Link

Usage

From source file:com.github.braully.graph.DatabaseFacade.java

public synchronized static List<RecordResultGraph> getAllResults(String database) {
    List<RecordResultGraph> results = new ArrayList();
    ObjectMapper mapper = new ObjectMapper();
    try {/*from w w  w  .ja v  a2 s.co  m*/
        List<Map> tmp = mapper.readValue(new File(database), List.class);
        if (tmp != null) {
            try {
                Iterator<Map> iterator = tmp.iterator();
                while (iterator.hasNext()) {
                    RecordResultGraph t = new RecordResultGraph(iterator.next());
                    results.add(t);
                }
            } catch (ClassCastException e) {
                e.printStackTrace();
            }
        }
        Collections.reverse(results);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return results;
}

From source file:org.dbmfs.DbmfsUtil.java

/**
 * ?JsonObject.<br>/*from   w ww.j  av a  2 s . c om*/
 *
 * @param Json
 * @return ?Object
 */
public static List<Map> jsonDeserialize(String jsonString) throws IOException, JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.readValue(jsonString, List.class);
}

From source file:de.thingweb.desc.ThingDescriptionParser.java

public static Thing fromBytes(byte[] data) throws JsonParseException, IOException {
    try {/*from  w  w  w . j a va2 s  . co  m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        EXIFactory ef = DefaultEXIFactory.newInstance();
        ef.setSharedStrings(SHARED_STRINGS_EXI_FOR_JSON);

        EXIforJSONParser e4j = new EXIforJSONParser(ef);
        e4j.parse(new ByteArrayInputStream(data), baos);

        // push-back EXI-generated JSON
        data = baos.toByteArray();
    } catch (EXIException | IOException e) {
        // something went wrong with EXI --> use "plain-text" json
    }

    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readValue(new InputStreamReader(new ByteArrayInputStream(data), "UTF-8"),
            JsonNode.class);

    try {
        return parse(root);
    } catch (Exception e) {
        // try old parser if by chance it was an old TD
        return parseOld(root);
    }
}

From source file:jeplus.JEPlusConfig.java

/**
 * Read the configuration from the given JSON file. 
 * @param file The File object associated with the file
 * @return a new configuration instance from the file
 * @throws java.io.IOException//ww  w .j a v a2s  .  com
 */
public static JEPlusConfig loadFromJSON(File file) throws IOException {
    // Read JSON
    ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
    JEPlusConfig config = mapper.readValue(file, JEPlusConfig.class);
    // Set current file
    config.CurrentConfigFile = file.getAbsolutePath();
    // Return
    return config;
}

From source file:net.e2.bw.servicereg.ldap.ServiceInstanceLdapService.java

/** Extracts coverage from compressed json */
public static List<Area> decompressCoverage(byte[] bytes) {
    if (bytes != null && bytes.length > 0) {
        try {/* w w  w  .  j a  v  a2 s  .c  o  m*/
            InputStream in = new InflaterInputStream(new ByteArrayInputStream(bytes));
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[8192];
            int len;
            while ((len = in.read(buffer)) > 0) {
                baos.write(buffer, 0, len);
            }
            String json = new String(baos.toByteArray(), "UTF-8");

            ObjectMapper mapper = new ObjectMapper();
            CoverageWrapper coverage = mapper.readValue(new StringReader(json), CoverageWrapper.class);
            return coverage.getCoverage();
        } catch (Exception ignored) {
            ignored.printStackTrace();
        }
    }
    return new ArrayList<>();
}

From source file:org.dbmfs.DbmfsUtil.java

/**
 * ?JsonObject.<br>// w  ww  .  java  2 s .c om
 *
 * @param Json
 * @return ?Object
 */
public static Map<String, Object> jsonDeserializeSingleObject(String jsonString)
        throws IOException, JsonProcessingException {
    String[] strSplit = jsonString.split("\"__DBMFS_TABLE_META_INFOMATION\"");

    String convertDataString = strSplit[0].trim();
    convertDataString = convertDataString.substring(1, (convertDataString.length() - 1));

    ObjectMapper mapper = new ObjectMapper();
    return mapper.readValue(convertDataString + "}", Map.class);
}

From source file:eu.europa.ec.fisheries.uvms.spatial.service.util.MapConfigHelper.java

public static ConfigurationDto getConfiguration(String configString) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    return mapper.readValue(configString, ConfigurationDto.class);
}

From source file:org.dbmfs.DbmfsUtil.java

public static DDLFolder jsonDeserializeDDLObject(String jsonBody) throws IOException {

    ObjectMapper mapper = new ObjectMapper();
    List dataMapList = mapper.readValue(jsonBody, List.class);

    return DDLFolder.createDDLFolder((String) ((Map) dataMapList.get(0)).get("__DBMFS_TABLE_META_INFOMATION"));
}

From source file:org.hawkular.bus.common.AbstractMessage.java

/**
 * Convenience static method that converts a JSON string to a particular message object.
 *
 * @param json the JSON string/*from   w w w .j ava 2 s.co  m*/
 * @param clazz the class whose instance is represented by the JSON string
 *
 * @return the message object that was represented by the JSON string
 */
public static <T extends BasicMessage> T fromJSON(String json, Class<T> clazz) {
    try {
        Method buildObjectMapperForDeserializationMethod = findBuildObjectMapperForDeserializationMethod(clazz);

        final ObjectMapper mapper = (ObjectMapper) buildObjectMapperForDeserializationMethod.invoke(null);
        if (FailOnUnknownProperties.class.isAssignableFrom(clazz)) {
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
        }

        return mapper.readValue(json, clazz);
    } catch (Exception e) {
        throw new IllegalStateException("JSON message cannot be converted to object of type [" + clazz + "]",
                e);
    }
}

From source file:com.github.braully.graph.DatabaseFacade.java

public static synchronized void saveResult(UndirectedSparseGraphTO graph, IGraphOperation graphOperation,
        Map<String, Object> result, List<String> consoleOut) {
    if (result != null && graph != null && graphOperation != null) {
        try {// w  w w  .j a v  a2  s .  co m

            List<RecordResultGraph> results = null;
            ObjectMapper mapper = new ObjectMapper();
            try {
                results = mapper.readValue(new File(DATABASE_URL), List.class);
            } catch (Exception e) {

            }
            if (results == null) {
                results = new ArrayList<RecordResultGraph>();
            }
            RecordResultGraph record = new RecordResultGraph();
            String fileGraph = saveGraph(graph);
            String fileConsole = saveConsole(consoleOut, fileGraph);
            record.status = "ok";
            record.graph = fileGraph;
            record.name = graph.getName();
            record.edges = "" + graph.getEdgeCount();
            record.vertices = "" + graph.getVertexCount();
            record.operation = graphOperation.getName();
            record.type = graphOperation.getTypeProblem();
            record.date = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
            record.results = resultMapToString(result);
            record.id = "" + results.size();
            record.console = fileConsole;
            results.add(record);
            mapper.writeValue(new File(DATABASE_URL), results);
            removeBatchDiretoryIfExists(graph);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}