From 56e40cd2a958a38e3372ad3ecf0595eb07af5895 Mon Sep 17 00:00:00 2001 From: Artem Gordienko Date: Sat, 26 Oct 2024 14:39:26 +0500 Subject: [PATCH] feat: reproduce code based on comment in issue * https://github.com/FasterXML/jackson-module-kotlin/issues/843#issuecomment-2439400995 --- src/main/java/com/arvgord/SecondObject.java | 17 +++++--- src/main/java/com/arvgord/ThirdObject.java | 25 +++++++----- .../java/com/arvgord/JacksonSortingTest.java | 40 ++++++++++++++----- 3 files changed, 58 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/arvgord/SecondObject.java b/src/main/java/com/arvgord/SecondObject.java index 7c28a56..2d19f4b 100644 --- a/src/main/java/com/arvgord/SecondObject.java +++ b/src/main/java/com/arvgord/SecondObject.java @@ -3,6 +3,8 @@ import com.fasterxml.jackson.annotation.JsonAnyGetter; import com.fasterxml.jackson.annotation.JsonAnySetter; import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; import java.util.LinkedHashMap; import java.util.Map; @@ -13,18 +15,23 @@ ) public class SecondObject { + private final String transactionId; @JsonAnySetter @JsonAnyGetter - private Map data = new LinkedHashMap<>(); - private String transactionId; + private final Map data; - public SecondObject() {} - SecondObject(String transactionId) { + @JsonCreator + public SecondObject(@JsonProperty("transactionId") String transactionId) { this.transactionId = transactionId; + this.data = new LinkedHashMap<>(); } public String getTransactionId() { - return transactionId; + return this.transactionId; + } + + public Map getData() { + return this.data; } } \ No newline at end of file diff --git a/src/main/java/com/arvgord/ThirdObject.java b/src/main/java/com/arvgord/ThirdObject.java index c410c58..33ac594 100644 --- a/src/main/java/com/arvgord/ThirdObject.java +++ b/src/main/java/com/arvgord/ThirdObject.java @@ -3,8 +3,9 @@ import com.fasterxml.jackson.annotation.JsonAnyGetter; import com.fasterxml.jackson.annotation.JsonAnySetter; import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; -import java.util.LinkedHashMap; import java.util.Map; @JsonAutoDetect( @@ -13,22 +14,26 @@ ) public class ThirdObject { + private final String transactionId; + @JsonAnySetter @JsonAnyGetter - private Map data = new LinkedHashMap<>(); - private String transactionId; + private final Map data; - ThirdObject() {} - public ThirdObject(String transactionId) { + @JsonCreator + public ThirdObject( + @JsonProperty("transactionId") String transactionId, + @JsonProperty("data") Map data + ) { this.transactionId = transactionId; + this.data = data; } - @JsonAnySetter - public void setData(String key, Object value) { - this.data.put(key, value); + public String getTransactionId() { + return this.transactionId; } - public String getTransactionId() { - return transactionId; + public Map getData() { + return this.data; } } diff --git a/src/test/java/com/arvgord/JacksonSortingTest.java b/src/test/java/com/arvgord/JacksonSortingTest.java index d3d5514..593a15e 100644 --- a/src/test/java/com/arvgord/JacksonSortingTest.java +++ b/src/test/java/com/arvgord/JacksonSortingTest.java @@ -9,7 +9,7 @@ public class JacksonSortingTest { private final ObjectMapper objectMapper = new ObjectMapper(); - private final String JSON_INPUT_FIRST = """ + private final String JSON_INPUT = """ { "b": 2, "a": 1, @@ -31,11 +31,11 @@ public class JacksonSortingTest { } """; - private final String JSON_INPUT_SECOND_AND_THIRD = """ + private final String JSON_OUTPUT_SECOND = """ { "transactionId": "test", - "b": 2, "a": 1, + "b": 2, "c": [ { "id": "3", @@ -53,11 +53,33 @@ public class JacksonSortingTest { } """; - private void testSerializationDeserialization(String jsonInput, Class clazz) throws Exception { - T deserializedObject = objectMapper.readValue(jsonInput, clazz); + private final String JSON_OUTPUT_THIRD = """ + { + "transactionId": "test", + "c": [ + { + "id": "3", + "value": "c" + }, + { + "id": "1", + "value": "a" + }, + { + "id": "2", + "value": "b" + } + ], + "a": 1, + "b": 2 + } + """; + + private void testSerializationDeserialization(String resultOutput, Class clazz) throws Exception { + T deserializedObject = objectMapper.readValue(JSON_INPUT, clazz); String serializedJson = objectMapper.writeValueAsString(deserializedObject); - String expectedJson = objectMapper.readTree(jsonInput).toPrettyString(); + String expectedJson = objectMapper.readTree(resultOutput).toPrettyString(); String actualJson = objectMapper.readTree(serializedJson).toPrettyString(); assertEquals(expectedJson, actualJson); @@ -65,16 +87,16 @@ private void testSerializationDeserialization(String jsonInput, Class cla @Test public void testSerializationAndDeserializationForFirstObject() throws Exception { - testSerializationDeserialization(JSON_INPUT_FIRST, FirstObject.class); + testSerializationDeserialization(JSON_INPUT, FirstObject.class); } @Test public void testSerializationAndDeserializationForSecondObject() throws Exception { - testSerializationDeserialization(JSON_INPUT_SECOND_AND_THIRD, SecondObject.class); + testSerializationDeserialization(JSON_OUTPUT_SECOND, SecondObject.class); } @Test public void testSerializationAndDeserializationForThirdObject() throws Exception { - testSerializationDeserialization(JSON_INPUT_SECOND_AND_THIRD, ThirdObject.class); + testSerializationDeserialization(JSON_OUTPUT_THIRD, ThirdObject.class); } }