List of usage examples for com.fasterxml.jackson.databind.node ObjectNode get
public JsonNode get(String paramString)
From source file:com.almende.eve.agent.google.GoogleTaskAgent.java
/** * Retrieve a list with all task lists in this google tasks. * /*w w w. j a v a 2s . com*/ * @return the task list * @throws Exception * the exception */ @Override public ArrayNode getTaskList() throws Exception { final String url = CALENDAR_URI + "users/@me/lists"; final String resp = HttpUtil.get(url, getAuthorizationHeaders()); final ObjectNode calendars = JOM.getInstance().readValue(resp, ObjectNode.class); // check for errors if (calendars.has("error")) { final ObjectNode error = (ObjectNode) calendars.get("error"); throw new JSONRPCException(error); } // get items from response ArrayNode items = null; if (calendars.has("items")) { items = (ArrayNode) calendars.get("items"); } else { items = JOM.createArrayNode(); } return items; }
From source file:eu.bittrade.libs.steemj.communication.jrpc.JsonRPCResponse.java
/** * This method checks if the JSON response wrapped by this * {@link JsonRPCResponse} instance has the expected <code>id</code> and * will try to transform the JSON into the given <code>type</code>. * //from w w w.j a v a 2s .c o m * @param type * The type to transform the JSON to. * @param id * The expected id of the response. * @return A list of of <code>type</code> instances. * @throws SteemCommunicationException * If the response does not contain the expected <code>id</code> * or if the response could not be transformed into the expected * <code>type</code>. */ public <T> List<T> handleResult(JavaType type, long id) throws SteemCommunicationException { if (isResponseValid()) { if (!isResult()) { throw new SteemCommunicationException( "The result does not contain the required " + RESULT_FIELD_NAME + " field."); } else { ObjectNode responseAsObject = ObjectNode.class.cast(rawJsonResponse); if (!hasExpectedId(id, responseAsObject)) { throw new SteemCommunicationException( "The id of this response does not match the expected id. This can cause an unexpected behavior."); } if (!isResultEmpty()) return CommunicationHandler.getObjectMapper() .convertValue(responseAsObject.get(RESULT_FIELD_NAME), type); } } return new ArrayList<>(); }
From source file:com.almende.eve.agent.google.GoogleTaskAgent.java
/** * Create a task list.//w ww .j a v a2s . co m * * @param taskList * JSON structure containing a taskList * @return JSON sturcture with created tasklist * @throws Exception * the exception */ public ObjectNode createTaskList(@Name("taskList") final ObjectNode taskList) throws Exception { final String url = CALENDAR_URI + "users/@me/lists"; // perform POST request final ObjectMapper mapper = JOM.getInstance(); final String body = mapper.writeValueAsString(taskList); final Map<String, String> headers = getAuthorizationHeaders(); headers.put("Content-Type", "application/json"); final String resp = HttpUtil.post(url, body, headers); final ObjectNode createdTaskList = mapper.readValue(resp, ObjectNode.class); // check for errors if (createdTaskList.has("error")) { final ObjectNode error = (ObjectNode) createdTaskList.get("error"); throw new JSONRPCException(error); } LOG.info("createTaskList=" + JOM.getInstance().writeValueAsString(createdTaskList)); return createdTaskList; }
From source file:org.eel.kitchen.jsonschema.validator.JsonResolverTest.java
@Test public void multipleRefLoopIsDetected() { JsonNode node;// w w w.jav a2 s .c om final ObjectNode schema = factory.objectNode(); final ArrayNode path = factory.arrayNode(); node = factory.objectNode().put("$ref", "#/b"); schema.put("a", node); path.add("#/b"); node = factory.objectNode().put("$ref", "#/c"); schema.put("b", node); path.add("#/c"); node = factory.objectNode().put("$ref", "#/a"); schema.put("c", node); path.add("#/a"); container = new SchemaContainer(schema); schemaNode = new SchemaNode(container, schema.get("a")); try { resolver.resolve(schemaNode); } catch (JsonSchemaException e) { msg = e.getValidationMessage(); verifyMessageParams(msg, Domain.REF_RESOLVING, "$ref"); assertEquals(msg.getMessage(), "ref loop detected"); assertEquals(msg.getInfo("path"), path); } }
From source file:com.redhat.lightblue.metadata.parser.JSONMetadataParserTest.java
License:asdf
@Test public void testParseConvertValueGenerator() throws IOException { JsonNode object = loadJsonNode("JSONMetadataParserTest-valuegenerator.json"); EntitySchema em = parser.parseEntitySchema(object); SimpleField field = (SimpleField) em.resolve(new Path("name")); Assert.assertNotNull(field);/*from w ww. j av a2 s .c om*/ ValueGenerator vg = field.getValueGenerator(); Assert.assertNotNull(vg); Assert.assertEquals(ValueGenerator.ValueGeneratorType.IntSequence, vg.getValueGeneratorType()); Assert.assertEquals("seq", vg.getProperties().get("name")); Assert.assertEquals("1000", vg.getProperties().get("initialValue").toString()); ObjectNode obj = JsonNodeFactory.instance.objectNode(); parser.convertValueGenerator(vg, obj); obj = (ObjectNode) obj.get("valueGenerator"); System.out.println(obj); Assert.assertEquals(ValueGenerator.ValueGeneratorType.IntSequence.toString(), obj.get("type").asText()); ObjectNode props = (ObjectNode) obj.get("configuration"); Assert.assertEquals("seq", props.get("name").asText()); Assert.assertEquals("1000", props.get("initialValue").asText()); }
From source file:com.almende.eve.agent.google.GoogleTaskAgent.java
/** * Retrieve a list of task on a certain task list. * /*from w ww . j a va 2 s. com*/ * @param dueMin * Minimal due time (optional) * @param dueMax * Maximal due time (optional) * @param taskListId * the task list id * @return the tasks * @throws Exception * the exception */ @Override public ArrayNode getTasks(@Optional @Name("dueMin") final String dueMin, @Optional @Name("dueMax") final String dueMax, @Optional @Name("taskListId") String taskListId) throws Exception { if (taskListId == null) { taskListId = getDefaultTaskList(); } if (taskListId == null) { throw new Exception("No tasklist given and no default list found"); } // built url with query parameters String url = CALENDAR_URI + "lists/" + taskListId + "/tasks"; final Map<String, String> params = new HashMap<String, String>(); if (dueMin != null) { params.put("dueMin", new DateTime(dueMin).toString()); } if (dueMax != null) { params.put("dueMax", new DateTime(dueMax).toString()); } // Set singleEvents=true to expand recurring events into instances // params.put("singleEvents", "true"); url = HttpUtil.appendQueryParams(url, params); // perform GET request final Map<String, String> headers = getAuthorizationHeaders(); final String resp = HttpUtil.get(url, headers); final ObjectMapper mapper = JOM.getInstance(); final ObjectNode json = mapper.readValue(resp, ObjectNode.class); // check for errors if (json.has("error")) { final ObjectNode error = (ObjectNode) json.get("error"); throw new JSONRPCException(error); } // get items from the response ArrayNode items = null; if (json.has("items")) { items = (ArrayNode) json.get("items"); /* * TODO: cleanup? * // convert from Google to Eve event * for (int i = 0; i < items.size(); i++) { * ObjectNode item = (ObjectNode) items.get(i); * toEveEvent(item); * } */ } else { items = JOM.createArrayNode(); } return items; }
From source file:com.marklogic.samplestack.database.DatabaseQnADocumentSearchIT.java
@Test public void testStructuredTagSearch() { JsonNode query;/* www . jav a 2 s . c o m*/ ObjectNode results = null; try { query = mapper.readValue( "{\"query\":{\"value-constraint-query\":{\"constraint-name\":\"tag\",\"text\":\"monotouch\"}}}", JsonNode.class); results = operations.qnaSearch(ClientRole.SAMPLESTACK_CONTRIBUTOR, query, 1, QueryView.FACETS); logger.debug("Query Results:" + mapper.writeValueAsString(results)); } catch (IOException e) { throw new SamplestackIOException(e); } assertNotNull("JSON has facet results", results.get("facets").get("tag")); }
From source file:com.googlecode.jsonrpc4j.JsonRpcClient.java
/** * Reads a JSON-PRC response from the server. This blocks until * a response is received.// w ww . jav a 2 s. c o m * * @param returnType the expected return type * @param ips the {@link InputStream} to read from * @return the object returned by the JSON-RPC response * @throws Throwable on error */ public Object readResponse(Type returnType, InputStream ips) throws Throwable { // get node iterator ReadContext ctx = ReadContext.getReadContext(ips, mapper); // read the response ctx.assertReadable(); JsonNode response = ctx.nextValue(); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "JSON-PRC Response: " + response.toString()); } // bail on invalid response if (!response.isObject()) { throw new JsonRpcClientException(0, "Invalid JSON-RPC response", response); } ObjectNode jsonObject = ObjectNode.class.cast(response); // show to listener if (this.requestListener != null) { this.requestListener.onBeforeResponseProcessed(this, jsonObject); } // detect errors if (jsonObject.has("error") && jsonObject.get("error") != null && !jsonObject.get("error").isNull()) { // resolve and throw the exception if (exceptionResolver == null) { throw DefaultExceptionResolver.INSTANCE.resolveException(jsonObject); } else { throw exceptionResolver.resolveException(jsonObject); } } // convert it to a return object if (jsonObject.has("result") && !jsonObject.get("result").isNull() && jsonObject.get("result") != null) { if (returnType == null) { LOGGER.warning("Server returned result but returnType is null"); return null; } JsonParser returnJsonParser = mapper.treeAsTokens(jsonObject.get("result")); JavaType returnJavaType = TypeFactory.defaultInstance().constructType(returnType); return mapper.readValue(returnJsonParser, returnJavaType); } // no return type return null; }
From source file:org.dswarm.graph.resources.MaintainResource.java
private Collection<String> getRecordURIs(final ObjectNode json) { final JsonNode recordsNode = json.get(DMPStatics.RECORDS_IDENTIFIER); final ArrayList<String> recordURIs = new ArrayList<>(); for (final JsonNode recordNode : recordsNode) { final String recordURI = recordNode.asText(); recordURIs.add(recordURI);/*from www .j a v a2 s . c o m*/ } return recordURIs; }
From source file:com.almende.eve.rpc.jsonrpc.JSONRPCException.java
/** * Instantiates a new jSONRPC exception. * // w ww . j ava2s .co m * @param exception * the exception */ public JSONRPCException(final ObjectNode exception) { super(); JSONRPCException cause = null; if (exception != null && !exception.isNull()) { cause = JOM.getInstance().convertValue(exception, JSONRPCException.class); cause.setRemote(true); TypeUtil<List<StackTraceElement>> injector = new TypeUtil<List<StackTraceElement>>() { }; List<StackTraceElement> trace = injector.inject(exception.get("stackTrace")); cause.setStackTrace(trace.toArray(new StackTraceElement[0])); } init(CODE.REMOTE_EXCEPTION, JSONRPCException.class.getSimpleName() + " received!", cause); }