List of usage examples for com.fasterxml.jackson.databind JsonNode toString
public abstract String toString();
From source file:com.enitalk.controllers.youtube.BotAware.java
public void sendLastCommand(JsonNode dest) throws ExecutionException, IOException { HttpPost post = new HttpPost(env.getProperty("bot.sendLast")); String auth = botAuth();//from w w w . j a v a2 s .c om post.addHeader("Authorization", "Bearer " + auth); post.setEntity(new StringEntity(dest.toString(), ContentType.APPLICATION_JSON)); CloseableHttpResponse resp = client.execute(post); IOUtils.closeQuietly(resp); }
From source file:com.redhat.lightblue.util.ErrorTest.java
@Test public void toJson_full() throws JSONException { Error e = Error.get("a", "b", "c"); JsonNode node = e.toJson(); JSONAssert.assertEquals("{objectType:error,context:a,errorCode:b,msg:c}", node.toString(), false); }
From source file:controllers.ExpressionController.java
private models.view.Expression buildViewExpression(final Http.RequestBody body) throws IOException { final JsonNode jsonBody = body.asJson(); if (jsonBody == null) { throw new IOException(); }//from w w w. j a va 2 s . c o m return OBJECT_MAPPER.readValue(jsonBody.toString(), models.view.Expression.class); }
From source file:com.infinities.keystone4j.admin.v3.project.ProjectResourceTest.java
@Test public void testUpdateProject() throws ClientProtocolException, IOException { project.setName("project1"); project.setDescription("desc"); project.setEnabled(false);/*w w w . j a v a 2s .co m*/ ProjectWrapper wrapper = new ProjectWrapper(project); PatchClient client = new PatchClient("http://localhost:9998/v3/projects/" + project.getId()); JsonNode node = client.connect(wrapper); System.err.println(node.toString()); JsonNode projectJ = node.get("project"); assertEquals(project.getId(), projectJ.get("id").asText()); assertEquals(project.getName(), projectJ.get("name").asText()); assertEquals(project.getDescription(), projectJ.get("description").asText()); assertEquals(project.getDomainId(), projectJ.get("domain_id").asText()); assertFalse(projectJ.get("enabled").asBoolean()); assertNotNull(projectJ.get("links")); assertNotNull(projectJ.get("links").get("self").asText()); }
From source file:com.ikanow.aleph2.search_service.elasticsearch.utils.TestJsonNodeWritableUtils.java
@SuppressWarnings("deprecation") @Test/* www .j a va2 s . co m*/ public void test_mapWritableWrapper() { final ObjectMapper mapper = BeanTemplateUtils.configureMapper(Optional.empty()); final MapWritable m1 = new MapWritable(); m1.put(new Text("test1"), new BooleanWritable(true)); final MapWritable m2 = new MapWritable(); m2.put(new Text("nested"), m1); m2.put(new Text("test2"), new Text("test2")); final ArrayWritable a1 = new ArrayWritable(IntWritable.class); a1.set(new Writable[] { new IntWritable(4), new IntWritable(5) }); final ArrayWritable a2 = new ArrayWritable(MapWritable.class); a2.set(new Writable[] { m1, m1 }); m2.put(new Text("array"), a2); m1.put(new Text("array"), a1); final JsonNode j2 = JsonNodeWritableUtils.from(m2); assertEquals(3, j2.size()); // Check j's contents assertEquals(Stream.of("nested", "test2", "array").sorted().collect(Collectors.toList()), Optionals.streamOf(j2.fieldNames(), false).sorted().collect(Collectors.toList())); assertEquals("test2", j2.get("test2").asText()); final JsonNode j1 = j2.get("nested"); assertEquals(2, j1.size()); final JsonNode j1b = JsonNodeWritableUtils.from(m1); assertTrue("{\"test1\":true,\"array\":[4,5]}".equals(j1b.toString()) || "{\"array\":[4,5],\"test1\":true}".equals(j1b.toString())); //(tests entrySet) final ArrayNode an = mapper.createArrayNode(); an.add(mapper.convertValue(4, JsonNode.class)); an.add(mapper.convertValue(5, JsonNode.class)); assertEquals(Arrays.asList(mapper.convertValue(true, JsonNode.class), an), Optionals.streamOf(((ObjectNode) j1).elements(), false).collect(Collectors.toList())); // OK, now test adding: assertEquals(2, j1.size()); final ObjectNode o1 = (ObjectNode) j1; o1.put("added", "added_this"); final ObjectNodeWrapper o1c = (ObjectNodeWrapper) o1; assertFalse(o1c.containsKey("not_present")); assertTrue(o1c.containsKey("added")); assertTrue(o1c.containsKey("test1")); assertEquals(Stream.of("test1", "array", "added").sorted().collect(Collectors.toList()), Optionals.streamOf(j1.fieldNames(), false).sorted().collect(Collectors.toList())); assertEquals( Arrays.asList(mapper.convertValue(true, JsonNode.class), an, mapper.convertValue("added_this", JsonNode.class)), Optionals.streamOf(((ObjectNode) j1).elements(), false).collect(Collectors.toList())); assertTrue(j1.toString().contains("added_this")); assertTrue(j1.toString().contains("4,5")); assertEquals(mapper.convertValue("added_this", JsonNode.class), j1.get("added")); assertEquals(3, j1.size()); // OK now test removing: assertEquals(null, o1.remove("not_present")); assertEquals(mapper.convertValue(true, JsonNode.class), o1.remove("test1")); assertEquals(2, o1.size()); ObjectNode o1b = o1.remove(Arrays.asList("added", "array")); assertEquals(0, o1.size()); assertEquals(0, o1b.size()); o1.putAll(JsonNodeWritableUtils.from(m1)); // will be minus one object assertEquals(2, o1.size()); assertTrue(o1c.containsValue(mapper.convertValue(true, JsonNode.class))); assertFalse(o1c.containsValue("banana")); final ObjectNodeWrapper o2 = (ObjectNodeWrapper) JsonNodeWritableUtils.from(m2); assertFalse(o2.isEmpty()); assertTrue(o2.containsKey("array")); assertFalse(o2.containsValue("array")); assertTrue(o2.containsValue(mapper.convertValue("test2", JsonNode.class))); assertEquals(TextNode.class, o2.remove("test2").getClass()); assertEquals(2, o2.size()); o2.removeAll(); assertEquals(0, o2.size()); }
From source file:org.pentaho.metaverse.impl.model.kettle.json.TransMetaJsonDeserializer.java
protected void deserializeVariables(TransMeta transMeta, JsonNode node, ObjectMapper mapper) throws IOException { ArrayNode varsArrayNode = (ArrayNode) node.get(TransMetaJsonSerializer.JSON_PROPERTY_VARIABLES); for (int i = 0; i < varsArrayNode.size(); i++) { JsonNode paramNode = varsArrayNode.get(i); ParamInfo param = mapper.readValue(paramNode.toString(), ParamInfo.class); transMeta.setVariable(param.getName(), param.getValue()); }// w ww. ja va 2 s .c o m }
From source file:org.dspace.app.rest.repository.MetadataFieldRestRepository.java
@Override @PreAuthorize("hasAuthority('ADMIN')") protected MetadataFieldRest put(Context context, HttpServletRequest request, String apiCategory, String model, Integer id, JsonNode jsonNode) throws SQLException, AuthorizeException { MetadataFieldRest metadataFieldRest = new Gson().fromJson(jsonNode.toString(), MetadataFieldRest.class); if (isBlank(metadataFieldRest.getElement())) { throw new UnprocessableEntityException("metadata element (in request body) cannot be blank"); }/*w w w . j a va2s .c om*/ if (!Objects.equals(id, metadataFieldRest.getId())) { throw new UnprocessableEntityException("ID in request body doesn't match path ID"); } MetadataField metadataField = metadataFieldService.find(context, id); if (metadataField == null) { throw new ResourceNotFoundException("metadata field with id: " + id + " not found"); } metadataField.setElement(metadataFieldRest.getElement()); metadataField.setQualifier(metadataFieldRest.getQualifier()); metadataField.setScopeNote(metadataFieldRest.getScopeNote()); try { metadataFieldService.update(context, metadataField); context.commit(); } catch (NonUniqueMetadataException e) { throw new UnprocessableEntityException("metadata field " + metadataField.getMetadataSchema().getName() + "." + metadataFieldRest.getElement() + (metadataFieldRest.getQualifier() != null ? "." + metadataFieldRest.getQualifier() : "") + " already exists"); } catch (IOException e) { throw new RuntimeException(e); } return converter.fromModel(metadataField); }
From source file:com.baasbox.service.user.UserService.java
public static ODocument updateProfile(ODocument profile, JsonNode nonAppUserAttributes, JsonNode privateAttributes, JsonNode friendsAttributes, JsonNode appUsersAttributes) throws Exception { if (nonAppUserAttributes != null) { ODocument attrObj = profile.field(UserDao.ATTRIBUTES_VISIBLE_BY_ANONYMOUS_USER); if (attrObj == null) attrObj = new ODocument(UserDao.USER_ATTRIBUTES_CLASS); attrObj.fromJSON(nonAppUserAttributes.toString()); PermissionsHelper.grantRead(attrObj, RoleDao.getRole(DefaultRoles.REGISTERED_USER.toString())); PermissionsHelper.grantRead(attrObj, RoleDao.getRole(DefaultRoles.ANONYMOUS_USER.toString())); PermissionsHelper.grantRead(attrObj, RoleDao.getFriendRole()); profile.field(UserDao.ATTRIBUTES_VISIBLE_BY_ANONYMOUS_USER, attrObj); attrObj.save();/*from www . java 2s . c om*/ } if (privateAttributes != null) { ODocument attrObj = profile.field(UserDao.ATTRIBUTES_VISIBLE_ONLY_BY_THE_USER); if (attrObj == null) attrObj = new ODocument(UserDao.USER_ATTRIBUTES_CLASS); attrObj.fromJSON(privateAttributes.toString()); PermissionsHelper.grant(attrObj, Permissions.ALLOW, getOUserByUsername(getUsernameByProfile(profile))); profile.field(UserDao.ATTRIBUTES_VISIBLE_ONLY_BY_THE_USER, attrObj); attrObj.save(); } if (friendsAttributes != null) { ODocument attrObj = profile.field(UserDao.ATTRIBUTES_VISIBLE_BY_FRIENDS_USER); if (attrObj == null) attrObj = new ODocument(UserDao.USER_ATTRIBUTES_CLASS); attrObj.fromJSON(friendsAttributes.toString()); PermissionsHelper.grantRead(attrObj, RoleDao.getFriendRole()); profile.field(UserDao.ATTRIBUTES_VISIBLE_BY_FRIENDS_USER, attrObj); attrObj.save(); } if (appUsersAttributes != null) { ODocument attrObj = profile.field(UserDao.ATTRIBUTES_VISIBLE_BY_REGISTERED_USER); if (attrObj == null) attrObj = new ODocument(UserDao.USER_ATTRIBUTES_CLASS); //preserve the _social field OTrackedMap oldSocial = (OTrackedMap) attrObj.field("_social"); ((ObjectNode) (appUsersAttributes)).remove("_social"); attrObj.fromJSON(appUsersAttributes.toString()); if (oldSocial != null) attrObj.field("_social", oldSocial); PermissionsHelper.grantRead(attrObj, RoleDao.getRole(DefaultRoles.REGISTERED_USER.toString())); PermissionsHelper.grantRead(attrObj, RoleDao.getFriendRole()); profile.field(UserDao.ATTRIBUTES_VISIBLE_BY_REGISTERED_USER, attrObj); attrObj.save(); } profile.save(); return profile; }
From source file:org.pentaho.metaverse.impl.model.kettle.json.TransMetaJsonDeserializer.java
protected void deserializeParameters(TransMeta transMeta, JsonNode node, ObjectMapper mapper) throws IOException { ArrayNode paramsArrayNode = (ArrayNode) node.get(TransMetaJsonSerializer.JSON_PROPERTY_PARAMETERS); for (int i = 0; i < paramsArrayNode.size(); i++) { JsonNode paramNode = paramsArrayNode.get(i); ParamInfo param = mapper.readValue(paramNode.toString(), ParamInfo.class); try {//from w w w .j a v a2 s .co m transMeta.addParameterDefinition(param.getName(), param.getDefaultValue(), param.getDescription()); } catch (DuplicateParamException e) { LOGGER.warn(Messages.getString("WARNING.Deserialization.Trans.DuplicateParam", param.getName()), e); } } }
From source file:com.redhat.lightblue.metadata.types.DateType.java
@Override public Object fromJson(JsonNode node) { if (node.isValueNode()) { try {//from www. ja va2 s . c o m return getDateFormat().parse(node.asText()); } catch (ParseException e) { throw Error.get(NAME, MetadataConstants.ERR_INCOMPATIBLE_VALUE, node.toString()); } } else { throw Error.get(NAME, MetadataConstants.ERR_INCOMPATIBLE_VALUE, node.toString()); } }