|
| 1 | +package com.baeldung.jackson.deserialization.nested; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | + |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.databind.JsonNode; |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
| 12 | + |
| 13 | +public class DeserializeWithNestedPropertiesUnitTest { |
| 14 | + |
| 15 | + private String SOURCE_JSON = "{\"id\":\"957c43f2-fa2e-42f9-bf75-6e3d5bb6960a\",\"name\":\"The Best Product\",\"brand\":{\"id\":\"9bcd817d-0141-42e6-8f04-e5aaab0980b6\",\"name\":\"ACME Products\",\"owner\":{\"id\":\"b21a80b1-0c09-4be3-9ebd-ea3653511c13\",\"name\":\"Ultimate Corp, Inc.\"}}}"; |
| 16 | + |
| 17 | + @Test |
| 18 | + public void whenUsingJacksonAnnotations_thenOk() throws IOException { |
| 19 | + Product product = new ObjectMapper().readerFor(Product.class) |
| 20 | + .readValue(SOURCE_JSON); |
| 21 | + |
| 22 | + assertEquals(product.getName(), "The Best Product"); |
| 23 | + assertEquals(product.getBrandName(), "ACME Products"); |
| 24 | + assertEquals(product.getOwnerName(), "Ultimate Corp, Inc."); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void whenUsingJacksonJsonNode_thenOk() throws IOException { |
| 29 | + JsonNode productNode = new ObjectMapper().readTree(SOURCE_JSON); |
| 30 | + |
| 31 | + Product product = new Product(); |
| 32 | + product.setId(productNode.get("id") |
| 33 | + .textValue()); |
| 34 | + product.setName(productNode.get("name") |
| 35 | + .textValue()); |
| 36 | + product.setBrandName(productNode.get("brand") |
| 37 | + .get("name") |
| 38 | + .textValue()); |
| 39 | + product.setOwnerName(productNode.get("brand") |
| 40 | + .get("owner") |
| 41 | + .get("name") |
| 42 | + .textValue()); |
| 43 | + |
| 44 | + assertEquals(product.getName(), "The Best Product"); |
| 45 | + assertEquals(product.getBrandName(), "ACME Products"); |
| 46 | + assertEquals(product.getOwnerName(), "Ultimate Corp, Inc."); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void whenUsingJacksonDeserializerManuallyRegistered_thenOk() throws IOException { |
| 51 | + ObjectMapper mapper = new ObjectMapper(); |
| 52 | + SimpleModule module = new SimpleModule(); |
| 53 | + module.addDeserializer(Product.class, new ProductDeserializer()); |
| 54 | + mapper.registerModule(module); |
| 55 | + |
| 56 | + Product product = mapper.readValue(SOURCE_JSON, Product.class); |
| 57 | + assertEquals(product.getName(), "The Best Product"); |
| 58 | + assertEquals(product.getBrandName(), "ACME Products"); |
| 59 | + assertEquals(product.getOwnerName(), "Ultimate Corp, Inc."); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void whenUsingJacksonDeserializerAutoRegistered_thenOk() throws IOException { |
| 64 | + ObjectMapper mapper = new ObjectMapper(); |
| 65 | + Product product = mapper.readValue(SOURCE_JSON, Product.class); |
| 66 | + assertEquals(product.getName(), "The Best Product"); |
| 67 | + assertEquals(product.getBrandName(), "ACME Products"); |
| 68 | + assertEquals(product.getOwnerName(), "Ultimate Corp, Inc."); |
| 69 | + } |
| 70 | +} |
0 commit comments