|
| 1 | +package json.jackson; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 6 | +import java.io.File; |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | +import org.apache.commons.io.IOUtils; |
| 9 | +import org.junit.Assert; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +/** |
| 13 | + * |
| 14 | + */ |
| 15 | +public class JsonNodeCopyTest { |
| 16 | + |
| 17 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 18 | + |
| 19 | + @Test |
| 20 | + public void cloneTest() throws Exception { |
| 21 | + String json = IOUtils.toString(new File("src/test/resources/json/jackson/fabric-sysconfig.json").toURI(), StandardCharsets.UTF_8); |
| 22 | + JsonNode originNode = objectMapper.readTree(json); |
| 23 | + long start = System.currentTimeMillis(); |
| 24 | + // deepCopy |
| 25 | + JsonNode copyNode = originNode.deepCopy(); |
| 26 | + long elapsed = System.currentTimeMillis() - start; |
| 27 | + System.out.println("Deep copy elapsed : " + elapsed + " [ms]"); |
| 28 | + |
| 29 | + // change origin value |
| 30 | + ((ObjectNode) originNode).put("sequence", "1"); |
| 31 | + |
| 32 | + // asserts |
| 33 | + Assert.assertFalse(originNode.equals(copyNode)); |
| 34 | + Assert.assertFalse(originNode.get("sequence").asText().equals(copyNode.get("sequence").asText())); |
| 35 | + |
| 36 | + Assert.assertTrue(originNode.get("channel_group").toString().equals(copyNode.get("channel_group").toString())); |
| 37 | + } |
| 38 | +} |
0 commit comments