List of usage examples for com.fasterxml.jackson.databind ObjectMapper readTree
public JsonNode readTree(URL source) throws IOException, JsonProcessingException
From source file:com.basistech.rosette.dm.json.plain.NameTest.java
@Test public void name() throws Exception { List<Name> names = Lists.newArrayList(); Name.Builder builder = new Name.Builder("Fred"); names.add(builder.build());/*from w w w. j a va2 s . c o m*/ builder = new Name.Builder("George"); builder.languageOfOrigin(LanguageCode.ENGLISH).script(ISO15924.Latn).languageOfUse(LanguageCode.FRENCH); names.add(builder.build()); ObjectMapper mapper = objectMapper(); String json = mapper.writeValueAsString(names); // one way to inspect the works is to read it back in _without_ our customized mapper. ObjectMapper plainMapper = new ObjectMapper(); JsonNode tree = plainMapper.readTree(json); assertTrue(tree.isArray()); assertEquals(2, tree.size()); JsonNode node = tree.get(0); assertTrue(node.has("text")); assertEquals("Fred", node.get("text").asText()); assertFalse(node.has("script")); assertFalse(node.has("languageOfOrigin")); assertFalse(node.has("languageOfUse")); List<Name> readBack = mapper.readValue(json, new TypeReference<List<Name>>() { }); assertEquals(names, readBack); }
From source file:com.evrythng.java.wrapper.mapping.TypeMapDeserializer.java
@Override public T deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException { ObjectCodec codec = jp.getCodec();// w ww . j a va 2 s.c om ObjectMapper mapper = (ObjectMapper) codec; ObjectNode root = mapper.readTree(jp); JsonNode type = root.get(typeFieldName); final String sType = type == null ? null : type.textValue(); Class<? extends T> clazz = resolveClass(sType); return codec.treeToValue(root, clazz); }
From source file:com.flipkart.zjsonpatch.ApiTest.java
@Test(expected = InvalidJsonPatchException.class) public void applyingNonArrayPatchShouldThrowAnException() throws IOException { ObjectMapper objectMapper = new ObjectMapper(); JsonNode invalid = objectMapper.readTree("{\"not\": \"a patch\"}"); JsonNode to = objectMapper.readTree("{\"a\":1}"); JsonPatch.apply(invalid, to);/*w ww .j a va 2s .c o m*/ }
From source file:com.flipkart.zjsonpatch.ApiTest.java
@Test(expected = InvalidJsonPatchException.class) public void applyingAnInvalidArrayShouldThrowAnException() throws IOException { ObjectMapper objectMapper = new ObjectMapper(); JsonNode invalid = objectMapper.readTree("[1, 2, 3, 4, 5]"); JsonNode to = objectMapper.readTree("{\"a\":1}"); JsonPatch.apply(invalid, to);//from ww w . ja va 2s . c om }
From source file:com.flipkart.zjsonpatch.ApiTest.java
@Test(expected = InvalidJsonPatchException.class) public void applyingAPatchWithAnInvalidOperationShouldThrowAnException() throws IOException { ObjectMapper objectMapper = new ObjectMapper(); JsonNode invalid = objectMapper.readTree("[{\"op\": \"what\"}]"); JsonNode to = objectMapper.readTree("{\"a\":1}"); JsonPatch.apply(invalid, to);//from w w w . jav a2 s . c o m }
From source file:com.yahoo.elide.jsonapi.models.PatchTest.java
@Test public void testSerialization() throws Exception { ObjectMapper mapper = new ObjectMapper(); JsonNode valueNode = mapper.readTree("\"stringValue\""); Patch patch = new Patch(Patch.Operation.ADD, "/foo/bar", valueNode); String expected = "{\"op\":\"add\",\"path\":\"/foo/bar\",\"value\":\"stringValue\"}"; String actual = mapper.writeValueAsString(patch); Assert.assertEquals(expected, actual, "A patch object should serialize correctly as a string."); }
From source file:com.yahoo.elide.jsonapi.models.PatchTest.java
@Test public void testListSerialization() throws Exception { ObjectMapper mapper = new ObjectMapper(); JsonNode valueNode = mapper.readTree("\"stringValue\""); List<Patch> patches = Lists.newArrayList(new Patch(Patch.Operation.ADD, "/foo/bar", valueNode)); String expected = "[{\"op\":\"add\",\"path\":\"/foo/bar\",\"value\":\"stringValue\"}]"; String actual = mapper.writeValueAsString(patches); Assert.assertEquals(expected, actual, "A list of patch object should serialized correctly as a string."); }
From source file:org.springframework.security.jackson2.UserDeserializer.java
/** * This method will create {@link User} object. It will ensure successful object creation even if password key is null in * serialized json, because credentials may be removed from the {@link User} by invoking {@link User#eraseCredentials()}. * In that case there won't be any password key in serialized json. *///from w w w . j a v a 2 s . c om @Override public User deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectMapper mapper = (ObjectMapper) jp.getCodec(); JsonNode jsonNode = mapper.readTree(jp); Set<GrantedAuthority> authorities = mapper.convertValue(jsonNode.get("authorities"), new TypeReference<Set<SimpleGrantedAuthority>>() { }); return new User(readJsonNode(jsonNode, "username").asText(), readJsonNode(jsonNode, "password").asText(), readJsonNode(jsonNode, "enabled").asBoolean(), readJsonNode(jsonNode, "accountNonExpired").asBoolean(), readJsonNode(jsonNode, "credentialsNonExpired").asBoolean(), readJsonNode(jsonNode, "accountNonLocked").asBoolean(), authorities); }
From source file:cat.calidos.morfeu.utils.Tezt.java
protected JsonNode parseJson(InputStream content) throws IOException, JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); JsonNode doc = mapper.readTree(content); return doc;/*from ww w .j a va 2 s . c o m*/ }
From source file:eu.mondo.driver.mongo.util.StatementDeserializer.java
@Override public Statement deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { String objectString = null;//from www.ja v a 2 s. co m String predicateString = null; String subjectString = null; ObjectMapper mapper = new ObjectMapper(); JsonNode root = mapper.readTree(jp); subjectString = root.path("subject").textValue(); predicateString = root.path("predicate").textValue(); objectString = root.path("object").textValue(); URI subject = new URIImpl(subjectString); URI predicate = new URIImpl(predicateString); Value object; try { object = new URIImpl(objectString); } catch (Exception e) { object = ValueFactoryImpl.getInstance().createLiteral(objectString); } jp.close(); Statement statement = new StatementImpl(subject, predicate, object); return statement; }