Merge pull request #15302 from DiegoMarti2/master

baeldung-articles: baeldung-articlesBAEL-7234
This commit is contained in:
Andrea Giulio Cerasoni 2023-12-03 11:28:22 +00:00 committed by GitHub
commit 1bae3d7580
1 changed files with 29 additions and 0 deletions

View File

@ -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());
}
}