List of usage examples for com.fasterxml.jackson.databind.node ObjectNode put
public ObjectNode put(String paramString1, String paramString2)
From source file:com.infinities.skyport.util.JsonUtil.java
public static String toJson(boolean insertResponseCode, String msg, Object object, Class<? extends Views.Short> view) { ObjectMapper mapper = getObjectMapper(); ObjectWriter writer = mapper.writerWithView(view).withDefaultPrettyPrinter(); JsonNode rootNode = mapper.createObjectNode(); try {//from w ww . j av a2 s. com if (object == null || "".equals(object)) { object = mapper.createObjectNode(); } String temp = writer.writeValueAsString(object); rootNode = mapper.readTree(temp); } catch (Exception e) { logger.error("json parsing failed", e); } ObjectNode root = getObjectMapper().createObjectNode(); root.put(JsonConstants.STATUS, insertResponseCode ? 1 : 0).put(JsonConstants.MSG, msg) .put(JsonConstants._DATA, rootNode); try { return getObjectMapper().configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, false) .writeValueAsString(root); } catch (Exception e) { logger.error("json parsing failed", e); throw new RuntimeException(e); } }
From source file:com.infinities.skyport.util.JsonUtil.java
public static String toLegendJson(Throwable e) { StringWriter wtr = new StringWriter(); try {/* w w w.j a v a 2 s .co m*/ JsonGenerator g = new JsonFactory().createGenerator(wtr); g.writeStartArray(); g.writeStartObject(); g.writeStringField("RES", "FALSE"); g.writeStringField("REASON", e.toString()); // g.writeStringField("REASON", Objects.firstNonNull(e.getMessage(), // e.toString())); g.writeEndObject(); g.writeEndArray(); g.close(); } catch (Exception ee) { ArrayNode array = getObjectMapper().createArrayNode(); ObjectNode reason = getObjectMapper().createObjectNode(); ObjectNode status = getObjectMapper().createObjectNode(); status.put(JsonConstants.STATUS, String.valueOf("FALSE")); reason.put(JsonConstants.REASON, "an unexpected error occurred"); array.add(status).add(reason); // "[{\"RES\":\"FALSE\"}, {\"REASON\":\"an unexpected error occurred\"}]"; return array.toString(); } return wtr.toString(); }
From source file:com.pros.jsontransform.expression.FunctionAbstract.java
static JsonNode transformArgument(final JsonNode argumentNode, final ObjectTransformer transformer) throws ObjectTransformerException { ObjectNode resultNode = transformer.mapper.createObjectNode(); if (argumentNode.isContainerNode()) { // transform argument node JsonNode sourceNode = transformer.getSourceNode(); JsonNode valueNode = transformer.transformExpression(sourceNode, argumentNode); resultNode.put("result", valueNode); } else if (argumentNode.isTextual()) { // transform $i modifier String textValue = argumentNode.textValue(); if (textValue.contains($I)) { int arrayIndex = transformer.getIndexOfSourceArray(); textValue = textValue.replace($I, String.valueOf(arrayIndex)); }/*from w ww. j av a 2 s . c om*/ resultNode.put("result", textValue); } else { resultNode.put("result", argumentNode); } return resultNode.get("result"); }
From source file:models.DataSet.java
public static List<DataSet> queryDataSet(String dataSetName, String agency, String instrument, String physicalVariable, String gridDimension, Date dataSetStartTime, Date dataSetEndTime) { List<DataSet> dataset = new ArrayList<DataSet>(); ObjectMapper mapper = new ObjectMapper(); ObjectNode queryJson = mapper.createObjectNode(); queryJson.put("name", dataSetName); queryJson.put("agencyId", agency); queryJson.put("instrument", instrument); queryJson.put("physicalVariable", physicalVariable); queryJson.put("gridDimension", gridDimension); if (dataSetEndTime != null) { queryJson.put("dataSetEndTime", dataSetEndTime.getTime()); }//w w w . j a v a 2s.c o m if (dataSetStartTime != null) { queryJson.put("dataSetStartTime", dataSetStartTime.getTime()); } JsonNode dataSetNode = APICall.postAPI(DATASET_QUERY, queryJson); if (dataSetNode == null || dataSetNode.has("error") || !dataSetNode.isArray()) { return dataset; } for (int i = 0; i < dataSetNode.size(); i++) { JsonNode json = dataSetNode.path(i); DataSet newDataSet = deserializeJsonToDataSet(json); dataset.add(newDataSet); } return dataset; }
From source file:com.nirmata.workflow.details.JsonSerializer.java
public static JsonNode newRunnableTask(RunnableTask runnableTask) { ArrayNode taskDags = newArrayNode(); runnableTask.getTaskDags().forEach(taskDag -> taskDags.add(newRunnableTaskDag(taskDag))); ObjectNode tasks = newNode();// w ww . ja va2 s . c o m runnableTask.getTasks().entrySet() .forEach(entry -> tasks.set(entry.getKey().getId(), newExecutableTask(entry.getValue()))); ObjectNode node = newNode(); node.set("taskDags", taskDags); node.set("tasks", tasks); node.put("startTimeUtc", runnableTask.getStartTimeUtc().format(DateTimeFormatter.ISO_DATE_TIME)); node.put("completionTimeUtc", runnableTask.getCompletionTimeUtc().isPresent() ? runnableTask.getCompletionTimeUtc().get().format(DateTimeFormatter.ISO_DATE_TIME) : null); node.put("parentRunId", runnableTask.getParentRunId().isPresent() ? runnableTask.getParentRunId().get().getId() : null); return node; }
From source file:org.apache.streams.data.util.JsonUtil.java
/** * Creates an empty array if missing//from w w w . j a va2 s.c o m * @param node object to create the array within * @param field location to create the array * @return the Map representing the extensions property */ public static ArrayNode ensureArray(ObjectNode node, String field) { String[] path = Lists.newArrayList(Splitter.on('.').split(field)).toArray(new String[0]); ObjectNode current = node; ArrayNode result = null; for (int i = 0; i < path.length; i++) { current = ensureObject((ObjectNode) node.get(path[i]), path[i]); } if (current.get(field) == null) current.put(field, mapper.createArrayNode()); result = (ArrayNode) node.get(field); return result; }
From source file:com.ikanow.aleph2.aleph2_rest_utils.RestUtils.java
public static JsonNode convertSingleObjectToJson(final Boolean value, final String field_name) { final ObjectNode json = mapper.createObjectNode(); json.put(field_name, value); return json;//from www. jav a 2 s . c om }
From source file:com.ikanow.aleph2.aleph2_rest_utils.RestUtils.java
public static JsonNode convertSingleObjectToJson(final String value, final String field_name) { final ObjectNode json = mapper.createObjectNode(); json.put(field_name, value); return json;/*from ww w .jav a2s. c o m*/ }
From source file:com.ikanow.aleph2.aleph2_rest_utils.RestUtils.java
public static JsonNode convertSingleObjectToJson(final Long value, final String field_name) { final ObjectNode json = mapper.createObjectNode(); json.put(field_name, value); return json;/*from www . j av a 2 s . c o m*/ }
From source file:com.github.reinert.jjschema.HyperSchemaGeneratorV4.java
private static <T> ObjectNode transformJsonToHyperSchema(Class<T> type, ObjectNode jsonSchema) { ObjectNode properties = (ObjectNode) jsonSchema.get("properties"); Iterator<String> namesIterator = properties.fieldNames(); while (namesIterator.hasNext()) { String prop = namesIterator.next(); try {// w w w . j a va 2 s .co m Field field = type.getDeclaredField(prop); com.github.reinert.jjschema.Media media = field .getAnnotation(com.github.reinert.jjschema.Media.class); if (media != null) { ObjectNode hyperProp = (ObjectNode) properties.get(prop); hyperProp.put(MEDIA_TYPE, media.type()); hyperProp.put(BINARY_ENCODING, media.binaryEncoding()); if (hyperProp.get("type").isArray()) { TextNode typeString = new TextNode("string"); ((ArrayNode) hyperProp.get("type")).set(0, typeString); } else { hyperProp.put("type", "string"); } properties.put(prop, hyperProp); } } catch (NoSuchFieldException e) { //e.printStackTrace(); } catch (SecurityException e) { //e.printStackTrace(); } } return jsonSchema; }