|
| 1 | +package com.baeldung.jsonnodetoarraynode; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import java.util.Iterator; |
| 8 | +import java.util.List; |
| 9 | +import java.util.stream.Collectors; |
| 10 | +import java.util.stream.StreamSupport; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 15 | +import com.fasterxml.jackson.databind.JsonNode; |
| 16 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 17 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 18 | + |
| 19 | +public class JsonNodeToArrayNodeConverterUnitTest { |
| 20 | + @Test |
| 21 | + void givenJsonNode_whenUsingJsonNodeMethods_thenConvertToArrayNode() throws JsonProcessingException { |
| 22 | + int count = 0; |
| 23 | + String json = "{\"objects\": [\"One\", \"Two\", \"Three\"]}"; |
| 24 | + final JsonNode arrayNode = new ObjectMapper().readTree(json).get("objects"); |
| 25 | + if (arrayNode.isArray()) { |
| 26 | + for (final JsonNode objNode : arrayNode) { |
| 27 | + assertNotNull(objNode, "Array element should not be null"); |
| 28 | + count++; |
| 29 | + } |
| 30 | + } |
| 31 | + assertNotNull(arrayNode, "The 'objects' array should not be null"); |
| 32 | + assertTrue(arrayNode.isArray(), "The 'objects' should be an array"); |
| 33 | + assertEquals(3, count, "The 'objects' array should have 3 elements"); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void givenJsonNode_whenUsingCreateArrayNode_thenConvertToArrayNode() throws Exception { |
| 38 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 39 | + JsonNode originalJsonNode = objectMapper.readTree("{\"objects\": [\"One\", \"Two\", \"Three\"]}"); |
| 40 | + ArrayNode arrayNode = objectMapper.createArrayNode(); |
| 41 | + originalJsonNode.get("objects").elements().forEachRemaining(arrayNode::add); |
| 42 | + assertEquals("[\"One\",\"Two\",\"Three\"]", arrayNode.toString()); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void givenJsonNode_whenUsingStreamSupport_thenConvertToArrayNode() throws Exception { |
| 47 | + String json = "{\"objects\": [\"One\", \"Two\", \"Three\"]}"; |
| 48 | + JsonNode obj = new ObjectMapper().readTree(json); |
| 49 | + List<JsonNode> objects = StreamSupport |
| 50 | + .stream(obj.get("objects").spliterator(), false) |
| 51 | + .collect(Collectors.toList()); |
| 52 | + |
| 53 | + assertEquals(3, objects.size(), "The 'objects' list should contain 3 elements"); |
| 54 | + |
| 55 | + JsonNode firstObject = objects.get(0); |
| 56 | + assertEquals("One", firstObject.asText(), "The first element should be One"); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void givenJsonNode_whenUsingIterator_thenConvertToArrayNode() throws Exception { |
| 61 | + String json = "{\"objects\": [\"One\", \"Two\", \"Three\"]}"; |
| 62 | + JsonNode datasets = new ObjectMapper().readTree(json); |
| 63 | + Iterator<JsonNode> iterator = datasets.withArray("objects").elements(); |
| 64 | + |
| 65 | + int count = 0; |
| 66 | + while (iterator.hasNext()) { |
| 67 | + JsonNode dataset = iterator.next(); |
| 68 | + System.out.print(dataset.toString() + " "); |
| 69 | + count++; |
| 70 | + } |
| 71 | + assertEquals(3, count, "The 'objects' list should contain 3 elements"); |
| 72 | + } |
| 73 | +} |
0 commit comments