List of usage examples for com.fasterxml.jackson.databind ObjectMapper readValue
@SuppressWarnings("unchecked") public <T> T readValue(byte[] src, JavaType valueType) throws IOException, JsonParseException, JsonMappingException
From source file:eu.europa.ec.fisheries.uvms.spatial.service.util.MapConfigHelper.java
public static Map<String, ReferenceDataPropertiesDto> getReferenceDataSettings(String referenceData) throws ServiceException { if (referenceData == null) { return null; }/*www .j av a 2s.co m*/ try { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); Object obj = mapper.readValue(referenceData, Map.class); String jsonString = mapper.writeValueAsString(obj); return mapper.readValue(jsonString, TypeFactory.defaultInstance().constructMapType(Map.class, String.class, ReferenceDataPropertiesDto.class)); } catch (IOException e) { throw new ServiceException("Parse Exception from Json to Object", e); } }
From source file:eu.trentorise.opendata.commons.test.jackson.TodJacksonTester.java
/** * Tests that the provided object can be converted to json and reconstructed * as type T. Also logs the json with the provided logger at FINE level. * * @return the reconstructed object// w w w. j a v a 2 s . co m */ public static <T> T testJsonConv(ObjectMapper om, Logger logger, @Nullable Object obj, Class<T> targetClass) { checkNotNull(om); checkNotNull(logger); T recObj; String json; try { json = om.writeValueAsString(obj); logger.log(Level.FINE, "json = {0}", json); } catch (Exception ex) { throw new RuntimeException("FAILED SERIALIZING!", ex); } try { Object ret = om.readValue(json, targetClass); recObj = (T) ret; } catch (Exception ex) { throw new RuntimeException("FAILED DESERIALIZING!", ex); } assertEquals(obj, recObj); return recObj; }
From source file:com.spotify.heroic.shell.Tasks.java
public static Series parseSeries(final ObjectMapper mapper, final Optional<String> series) { return series.<Series>map(s -> { try {//from w ww .ja v a 2 s . c o m return mapper.readValue(s, Series.class); } catch (IOException e) { throw new RuntimeException("Bad series: " + s, e); } }).orElseGet(Series::empty); }
From source file:de.undercouch.actson.JsonParserTest.java
/** * Assert that two JSON objects are equal * @param expected the expected JSON object * @param actual the actual JSON object/*from ww w .jav a2 s .c om*/ */ private static void assertJsonObjectEquals(String expected, String actual) { ObjectMapper mapper = new ObjectMapper(); TypeReference<Map<String, Object>> ref = new TypeReference<Map<String, Object>>() { }; try { Map<String, Object> em = mapper.readValue(expected, ref); Map<String, Object> am = mapper.readValue(actual, ref); assertEquals(em, am); } catch (IOException e) { throw new IllegalStateException(e); } }
From source file:com.twosigma.beaker.table.serializer.TableDisplayDeSerializer.java
public static Pair<String, Object> getDeserializeObject(BeakerObjectConverter parent, JsonNode n, ObjectMapper mapper) { Object o = null;/*from w w w . j a va 2 s . co m*/ String subtype = null; try { List<List<?>> values = TableDisplayDeSerializer.getValues(parent, n, mapper); List<String> columns = TableDisplayDeSerializer.getColumns(n, mapper); List<String> classes = TableDisplayDeSerializer.getClasses(n, mapper); if (n.has("subtype")) subtype = mapper.readValue(n.get("subtype").asText(), String.class); if (subtype != null && subtype.equals(TableDisplay.DICTIONARY_SUBTYPE)) { o = getValuesAsDictionary(parent, n, mapper); } else if (subtype != null && subtype.equals(TableDisplay.LIST_OF_MAPS_SUBTYPE) && columns != null && values != null) { o = getValuesAsRows(parent, n, mapper); } else if (subtype != null && subtype.equals(TableDisplay.MATRIX_SUBTYPE)) { o = getValuesAsMatrix(parent, n, mapper); } if (o == null) { if (n.has("hasIndex") && mapper.readValue(n.get("hasIndex").asText(), String.class).equals("true") && columns != null && values != null) { columns.remove(0); classes.remove(0); for (List<?> v : values) { v.remove(0); } o = new TableDisplay(values, columns, classes); } else { o = new TableDisplay(values, columns, classes); } } } catch (Exception e) { logger.error("exception deserializing TableDisplay ", e); } return new ImmutablePair<String, Object>(subtype, o); }
From source file:it.uniroma2.sag.kelp.linearization.nystrom.NystromMethod.java
/** * Load a Nystrom-based projection function from a file * //from www . ja v a 2 s. com * @param inputFilePath * the input file * @return Nystrom-based projection function * @throws FileNotFoundException * @throws IOException */ public static NystromMethod load(String inputFilePath) throws FileNotFoundException, IOException { ObjectMapper mapper = new ObjectMapper(); InputStreamReader inS = new InputStreamReader(FileUtils.createInputStream(inputFilePath), "utf8"); NystromMethod readValue = mapper.readValue(inS, NystromMethod.class); inS.close(); return readValue; }
From source file:edu.usu.sdl.openstorefront.common.util.StringProcessor.java
/** * Remove all json fields not in the list to keep. * * @param json/*from w w w. ja v a 2s. c o m*/ * @param fieldsToKeep * @return */ public static String stripeFieldJSON(String json, Set<String> fieldsToKeep) { ObjectMapper mapper = defaultObjectMapper(); try { JsonNode rootNode = mapper.readTree(json); processNode(rootNode, fieldsToKeep); Object jsonString = mapper.readValue(rootNode.toString(), Object.class); return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonString); } catch (IOException ex) { throw new OpenStorefrontRuntimeException(ex); } }
From source file:io.coala.json.JsonUtil.java
/** * @param om the {@link ObjectMapper} used to parse/deserialize/unmarshal * @param json the JSON formatted {@link String} value * @param resultType the type of result {@link Object} * @param imports the {@link Properties} instances for default values, etc. * @return the parsed/deserialized/unmarshalled {@link Object} *//*from ww w . j av a 2s . c o m*/ public static <T> T valueOf(final ObjectMapper om, final String json, final Class<T> resultType, final Properties... imports) { if (json == null || json.equalsIgnoreCase("null")) return null; try { return (T) om.readValue( !json.startsWith("\"") && resultType == String.class ? "\"" + json + "\"" : json, checkRegistered(om, resultType, imports)); } catch (final Throwable e) { return Thrower.rethrowUnchecked(e); } }
From source file:org.forgerock.openig.util.Json.java
private static <T> T parse(ObjectMapper mapper, Reader reader) throws IOException { if (reader == null) { return null; }// w w w .j ava2 s .c o m final JsonParser jp = mapper.getFactory().createParser(reader); final JsonToken jToken = jp.nextToken(); if (jToken != null) { switch (jToken) { case START_ARRAY: return mapper.readValue(jp, new TypeReference<LinkedList<?>>() { }); case START_OBJECT: return mapper.readValue(jp, new TypeReference<LinkedHashMap<String, ?>>() { }); case VALUE_FALSE: case VALUE_TRUE: return mapper.readValue(jp, new TypeReference<Boolean>() { }); case VALUE_NUMBER_INT: return mapper.readValue(jp, new TypeReference<Integer>() { }); case VALUE_NUMBER_FLOAT: return mapper.readValue(jp, new TypeReference<Float>() { }); case VALUE_NULL: return null; default: // This is very unlikely to happen. throw new IOException("Invalid JSON content"); } } return null; }
From source file:com.google.openrtb.json.OpenRtbJsonTest.java
private static String cleanupIdField(final String jsonString) throws IOException { ObjectMapper mapper = new ObjectMapper(); ObjectNode json = mapper.readValue(jsonString, ObjectNode.class); json.put("id", "1"); return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json); }