baeldung-articles: baeldung-articlesBAEL-7234

Commit to a new article (How to convert JsonNode to ObjectNode). Adding a test class (JsonNodeToJsonObjectUnitTest).
This commit is contained in:
DiegoMarti2 2023-11-26 11:55:41 +02:00 committed by GitHub
parent e5e8ecf27d
commit 2892810240
1 changed files with 28 additions and 0 deletions

View File

@ -0,0 +1,28 @@
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());
}
}