List of usage examples for com.fasterxml.jackson.databind JsonNode toString
public abstract String toString();
From source file:org.apache.olingo.fit.utils.Commons.java
public static InputStream changeFormat(final InputStream is, final Accept target) { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); try {/* ww w . j a v a2 s .c o m*/ IOUtils.copy(is, bos); IOUtils.closeQuietly(is); final ObjectMapper mapper = new ObjectMapper( new JsonFactory().configure(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS, true)); final JsonNode node = changeFormat( (ObjectNode) mapper.readTree(new ByteArrayInputStream(bos.toByteArray())), target); return IOUtils.toInputStream(node.toString(), Constants.ENCODING); } catch (Exception e) { LOG.error("Error changing format", e); return new ByteArrayInputStream(bos.toByteArray()); } finally { IOUtils.closeQuietly(is); } }
From source file:io.confluent.kafkarest.unit.AvroConverterTest.java
private static void expectConversionException(JsonNode obj, Schema schema) { try {// w w w . ja v a 2 s . c o m AvroConverter.toAvro(obj, schema); fail("Expected conversion of " + (obj == null ? "null" : obj.toString()) + " to schema " + schema.toString() + " to fail"); } catch (ConversionException e) { // Expected } }
From source file:com.baasbox.service.storage.DocumentService.java
public static ODocument create(String collection, JsonNode bodyJson) throws Throwable, InvalidCollectionException, InvalidModelException { DocumentDao dao = DocumentDao.getInstance(collection); ODocument doc = dao.create();/* w w w . ja va 2s . c o m*/ dao.update(doc, (ODocument) (new ODocument()).fromJSON(bodyJson.toString())); dao.save(doc); return doc;//.toJSON("fetchPlan:*:0 _audit:1,rid"); }
From source file:com.servioticy.api.commons.data.CouchBase.java
/** * @param userId // TODO [David] params???? * @param data_id//from ww w . j a v a 2 s. c o m * @return */ public static Data getData(SO so, String streamId) { JsonNode stream = so.getStream(streamId); if (stream == null) return null; if (stream.path("data").isMissingNode()) return null; String dataId = stream.get("data").asText(); // String storedData = getJsonNode(dataId).toString(); JsonNode storedJsonData = getJsonNode(dataId); String storedData = (storedJsonData != null) ? storedJsonData.toString() : null; if (storedData != null) { return new Data(dataId, storedData); } return null; }
From source file:com.github.tomakehurst.wiremock.matching.StringValuePatternJsonDeserializer.java
private static Class<? extends StringValuePattern> findPatternClass(JsonNode rootNode) throws JsonMappingException { for (Map.Entry<String, JsonNode> node : ImmutableList.copyOf(rootNode.fields())) { Class<? extends StringValuePattern> patternClass = PATTERNS.get(node.getKey()); if (patternClass != null) { return patternClass; }//from ww w . ja v a 2 s . c o m } throw new JsonMappingException(rootNode.toString() + " is not a valid comparison"); }
From source file:com.github.arnebinder.hide.swagger.params.HiderMojo.java
public static JsonNode merge(JsonNode mainNode, JsonNode updateNode) throws MojoExecutionException { if (updateNode instanceof ArrayNode) { if (!(mainNode instanceof ArrayNode)) { // error throw new MojoExecutionException( "Could not merge nodes: " + mainNode.toString() + " is not an ArrayNode."); } else {//w w w . ja v a 2 s. co m Iterator<JsonNode> updateElements = updateNode.elements(); while (updateElements.hasNext()) { JsonNode updateElement = updateElements.next(); if (updateElement.has("name") && updateElement.get("name") != null && updateElement.get("name").isTextual()) { String updateName = updateElement.get("name").asText(); JsonNode mNode = mainNode.findValue(updateName); if (mNode == null) { // add updateElement to mainNode ((ArrayNode) mainNode).add(updateElement); } merge(mNode, updateElement); } else { throw new MojoExecutionException("Could not find key \"name\" in ArrayNode update element: " + updateElement.toString()); } } } } else { Iterator<String> fieldNames = updateNode.fieldNames(); while (fieldNames.hasNext()) { String fieldName = fieldNames.next(); JsonNode jsonNode = mainNode.get(fieldName); // if field exists and is an embedded object if (jsonNode != null && jsonNode.isObject()) { merge(jsonNode, updateNode.get(fieldName)); } else { if (mainNode instanceof ObjectNode) { // Overwrite field JsonNode value = updateNode.get(fieldName); ((ObjectNode) mainNode).replace(fieldName, value); } } } } return mainNode; }
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 . j a v a 2s.co 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:com.spoiledmilk.ibikecph.util.Util.java
public static List<JsonNode> JsonNodeToList(JsonNode arg) { List<JsonNode> result = null; try {//w w w .java 2 s. co m if (arg != null) result = getJsonObjectMapper().readValue(arg.toString(), new TypeReference<List<JsonNode>>() { }); } catch (JsonParseException e) { LOG.w("JsonParseException ", e); } catch (JsonMappingException e) { LOG.w("JsonMappingException ", e); } catch (IOException e) { LOG.w("IOException ", e); } return result; }
From source file:org.kiji.rest.util.RowResourceUtil.java
/** * Util method to write a rest row into Kiji. * * @param kijiTable is the table to write into. * @param entityId is the entity id of the row to write. * @param kijiRestRow is the row model to write to Kiji. * @param schemaTable is the handle to the schema table used to resolve the KijiRestCell's * writer schema if it was specified as a UID. * @throws IOException if there a failure writing the row. *//*w w w. ja v a 2s. co m*/ public static void writeRow(KijiTable kijiTable, EntityId entityId, KijiRestRow kijiRestRow, KijiSchemaTable schemaTable) throws IOException { final KijiTableWriter writer = kijiTable.openTableWriter(); // Default global timestamp. long globalTimestamp = System.currentTimeMillis(); try { for (Entry<String, NavigableMap<String, List<KijiRestCell>>> familyEntry : kijiRestRow.getCells() .entrySet()) { String columnFamily = familyEntry.getKey(); NavigableMap<String, List<KijiRestCell>> qualifiedCells = familyEntry.getValue(); for (Entry<String, List<KijiRestCell>> qualifiedCell : qualifiedCells.entrySet()) { final KijiColumnName column = new KijiColumnName(columnFamily, qualifiedCell.getKey()); if (!kijiTable.getLayout().exists(column)) { throw new WebApplicationException( new IllegalArgumentException("Specified column does not exist: " + column), Response.Status.BAD_REQUEST); } for (KijiRestCell restCell : qualifiedCell.getValue()) { final long timestamp; if (null != restCell.getTimestamp()) { timestamp = restCell.getTimestamp(); } else { timestamp = globalTimestamp; } if (timestamp >= 0) { // Put to either a counter or a regular cell. if (SchemaType.COUNTER == kijiTable.getLayout().getCellSchema(column).getType()) { JsonNode parsedCounterValue = BASIC_MAPPER.valueToTree(restCell.getValue()); if (parsedCounterValue.isIntegralNumber()) { // Write the counter cell. writer.put(entityId, column.getFamily(), column.getQualifier(), timestamp, parsedCounterValue.asLong()); } else if (parsedCounterValue.isContainerNode()) { if (null != parsedCounterValue.get(COUNTER_INCREMENT_KEY) && parsedCounterValue.get(COUNTER_INCREMENT_KEY).isIntegralNumber()) { // Counter incrementation does not support timestamp. if (null != restCell.getTimestamp()) { throw new WebApplicationException(new IllegalArgumentException( "Counter incrementation does not support " + "timestamp. Do not specify timestamp in request.")); } // Increment counter cell. writer.increment(entityId, column.getFamily(), column.getQualifier(), parsedCounterValue.get(COUNTER_INCREMENT_KEY).asLong()); } else { throw new WebApplicationException(new IllegalArgumentException( "Counter increment could not be parsed " + "as long: " + parsedCounterValue + ". Provide a json node such as {\"incr\" : 123}."), Response.Status.BAD_REQUEST); } } else { // Could not parse parameter to a long. throw new WebApplicationException(new IllegalArgumentException( "Counter value could not be parsed as long: " + parsedCounterValue + ". Provide a long value to set the counter."), Response.Status.BAD_REQUEST); } } else { // Write the cell. String jsonValue = restCell.getValue().toString(); // TODO: This is ugly. Converting from Map to JSON to String. if (restCell.getValue() instanceof Map<?, ?>) { JsonNode node = BASIC_MAPPER.valueToTree(restCell.getValue()); jsonValue = node.toString(); } Schema actualWriter = restCell.getWriterSchema(schemaTable); if (actualWriter == null) { throw new IOException("Unrecognized schema " + restCell.getValue()); } putCell(writer, entityId, jsonValue, column, timestamp, actualWriter); } } } } } } finally { ResourceUtils.closeOrLog(writer); } }
From source file:com.redhat.lightblue.util.JsonUtils.java
private static boolean valueToString(StringBuilder bld, JsonNode node, int depth, boolean newLine) { if (newLine) { indent(bld, depth);//w w w. j a v a 2 s. com newLine = false; } bld.append(node.toString()); return newLine; }