List of usage examples for com.fasterxml.jackson.databind ObjectMapper reader
public ObjectReader reader()
From source file:com.jayway.jsonpath.internal.spi.json.JacksonJsonProvider.java
/** * Initialize the JacksonProvider with a custom ObjectMapper. * @param objectMapper the ObjectMapper to use *//*from w w w. java 2s. c o m*/ public JacksonJsonProvider(ObjectMapper objectMapper) { this(objectMapper, objectMapper.reader().withType(Object.class)); }
From source file:com.github.shredder121.gh_event_api.handler.AbstractHandlerTest.java
public String minimize(final InputStream stream) throws IOException { ObjectMapper mapper = Jackson2ObjectMapperBuilder.json().build(); Map<String, Object> content = mapper.reader().forType(Map.class).readValue(stream); return mapper.writer(new MinimalPrettyPrinter(null/*minimizes*/)).writeValueAsString(content); }
From source file:io.mesosphere.mesos.frameworks.cassandra.scheduler.api.AbstractApiControllerTest.java
@NotNull protected Tuple2<Integer, JsonNode> fetchJson(final String rel, final boolean post) throws Exception { final JsonFactory factory = new JsonFactory(); final HttpURLConnection conn = (HttpURLConnection) resolve(rel).toURL().openConnection(); try {//from w ww.jav a 2 s. co m conn.setRequestMethod(post ? "POST" : "GET"); conn.setRequestProperty("Accept", "application/json"); conn.connect(); final int responseCode = conn.getResponseCode(); InputStream in; try { in = conn.getInputStream(); } catch (final IOException e) { in = conn.getErrorStream(); } if (in == null) { return Tuple2.tuple2(responseCode, (JsonNode) MissingNode.getInstance()); } assertEquals("application/json", conn.getHeaderField("Content-Type")); try { final ObjectMapper om = new ObjectMapper(); return Tuple2.tuple2(responseCode, om.reader().with(factory).readTree(in)); } finally { in.close(); } } finally { conn.disconnect(); } }
From source file:org.onosproject.drivers.ciena.waveserver.rest.CienaRestDevice.java
private JsonNode get(String uri) throws IOException { InputStream response = controller.get(deviceId, uri, MediaType.valueOf(MediaType.APPLICATION_JSON)); ObjectMapper om = new ObjectMapper(); final ObjectReader reader = om.reader(); // all waveserver responses contain data node, which contains the requested data return reader.readTree(response).get(DATA); }
From source file:org.talend.dataprep.api.filter.SimpleFilterService.java
@Override public Predicate<DataSetRow> build(String filterAsString, RowMetadata rowMetadata) { if (isEmpty(filterAsString)) { return r -> true; }//ww w.j a v a 2 s.c o m try { ObjectMapper mapper = new ObjectMapper(); final JsonNode root = mapper.reader().readTree(filterAsString); final Iterator<JsonNode> elements = root.elements(); if (!elements.hasNext()) { throw new IllegalArgumentException("Malformed filter: " + filterAsString); } else { return buildFilter(root, rowMetadata); } } catch (Exception e) { throw new TalendRuntimeException(BaseErrorCodes.UNABLE_TO_PARSE_FILTER, e); } }
From source file:net.opentsdb.tools.ConfigArgP.java
/** * Creates a new ConfigArgP/* w ww .j a va 2 s.co m*/ * @param args The command line arguments */ public ConfigArgP(String... args) { InputStream is = null; try { final Config loadConfig = new NoLoadConfig(); is = ConfigArgP.class.getClassLoader().getResourceAsStream("opentsdb.conf.json"); ObjectMapper jsonMapper = new ObjectMapper(); JsonNode root = jsonMapper.reader().readTree(is); JsonNode configRoot = root.get("config-items"); scriptEngine.eval("var config = " + configRoot.toString() + ";"); processBindings(jsonMapper, root); final ConfigurationItem[] loadedItems = jsonMapper.reader(ConfigurationItem[].class) .readValue(configRoot); final TreeSet<ConfigurationItem> items = new TreeSet<ConfigurationItem>(Arrays.asList(loadedItems)); LOG.info("Loaded [{}] Configuration Items from opentsdb.conf.json", items.size()); // if(LOG.isDebugEnabled()) { StringBuilder b = new StringBuilder("Configs:"); for (ConfigurationItem ci : items) { b.append("\n\t").append(ci.toString()); } b.append("\n"); LOG.info(b.toString()); // } for (ConfigurationItem ci : items) { LOG.debug("Processing CI [{}]", ci.getKey()); if (ci.meta != null) { argp.addOption(ci.clOption, ci.meta, ci.description); if ("default".equals(ci.help)) dargp.addOption(ci.clOption, ci.meta, ci.description); } else { argp.addOption(ci.clOption, ci.description); if ("default".equals(ci.help)) dargp.addOption(ci.clOption, ci.description); } if (!configItemsByKey.add(ci)) { throw new RuntimeException("Duplicate configuration key [" + ci.key + "] in opentsdb.conf.json. Programmer Error."); } if (!configItemsByCl.add(ci)) { throw new RuntimeException("Duplicate configuration command line option [" + ci.clOption + "] in opentsdb.conf.json. Programmer Error."); } if (ci.getDefaultValue() != null) { ci.setValue(processConfigValue(ci.getDefaultValue())); loadConfig.overrideConfig(ci.key, processConfigValue(ci.getValue())); } } //loadConfig.loadStaticVariables(); // find --config and --include-config in argp and load into config // validate //argp.parse(args); this.config = new Config(loadConfig); nonOptionArgs = applyArgs(args); } catch (Exception ex) { if (ex instanceof IllegalArgumentException) { throw (IllegalArgumentException) ex; } throw new RuntimeException("Failed to read opentsdb.conf.json", ex); } finally { if (is != null) try { is.close(); } catch (Exception x) { /* No Op */ } } }
From source file:io.fabric8.devops.connector.DevOpsConnector.java
protected JsonNode parseLastBuildJson(String urlText) { HttpURLConnection connection = null; String message = null;/* w w w . j ava2s. c o m*/ try { URL url = new URL(urlText); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Content-Type", "application/json"); int status = connection.getResponseCode(); message = connection.getResponseMessage(); getLog().info("Got response code from URL: " + url + " " + status + " message: " + message); if (status != 200 || Strings.isNullOrBlank(message)) { getLog().debug("Failed to load URL " + url + ". Status: " + status + " message: " + message); } else { ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.reader().readTree(message); } } catch (Exception e) { getLog().debug("Failed to load URL " + urlText + ". " + e, e); } finally { if (connection != null) { connection.disconnect(); } } return null; }