Merge pull request #15302 from DiegoMarti2/master
baeldung-articles: baeldung-articlesBAEL-7234
This commit is contained in:
commit
1bae3d7580
|
@ -0,0 +1,29 @@
|
||||||
|
package com.baeldung.jsonnodetojsonobject;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
public class JsonNodeToJsonObjectUnitTest {
|
||||||
|
|
||||||
|
public static String jsonString = "{\"name\": \"John\", \"gender\": \"male\", \"company\": \"Baeldung\", \"isEmployee\": true, \"age\": 30}";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJsonNode_whenConvertingToObjectNode_thenVerifyFieldsIntegrity() throws JsonProcessingException {
|
||||||
|
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
JsonNode jsonNode = objectMapper.readTree(jsonString);
|
||||||
|
ObjectNode objectNode = objectMapper.createObjectNode().setAll((ObjectNode) jsonNode);
|
||||||
|
|
||||||
|
assertEquals("John", objectNode.get("name").asText());
|
||||||
|
assertEquals("male", objectNode.get("gender").asText());
|
||||||
|
assertEquals("Baeldung", objectNode.get("company").asText());
|
||||||
|
assertTrue(objectNode.get("isEmployee").asBoolean());
|
||||||
|
assertEquals(30, objectNode.get("age").asInt());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue