Skip to content

Commit

Permalink
feat: reproduce code based on comment in issue
Browse files Browse the repository at this point in the history
  • Loading branch information
arvgord committed Oct 26, 2024
1 parent 8ed6668 commit 56e40cd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 24 deletions.
17 changes: 12 additions & 5 deletions src/main/java/com/arvgord/SecondObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,18 +15,23 @@
)
public class SecondObject {

private final String transactionId;
@JsonAnySetter
@JsonAnyGetter
private Map<String, Object> data = new LinkedHashMap<>();
private String transactionId;
private final Map<String, Object> 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<String, Object> getData() {
return this.data;
}
}
25 changes: 15 additions & 10 deletions src/main/java/com/arvgord/ThirdObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -13,22 +14,26 @@
)
public class ThirdObject {

private final String transactionId;
@JsonAnySetter
@JsonAnyGetter
private Map<String, Object> data = new LinkedHashMap<>();
private String transactionId;
private final Map<String, Object> data;

ThirdObject() {}

public ThirdObject(String transactionId) {
@JsonCreator
public ThirdObject(
@JsonProperty("transactionId") String transactionId,
@JsonProperty("data") Map<String, Object> 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<String, Object> getData() {
return this.data;
}
}
40 changes: 31 additions & 9 deletions src/test/java/com/arvgord/JacksonSortingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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",
Expand All @@ -53,28 +53,50 @@ public class JacksonSortingTest {
}
""";

private <T> void testSerializationDeserialization(String jsonInput, Class<T> 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 <T> void testSerializationDeserialization(String resultOutput, Class<T> 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);
}

@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);
}
}

0 comments on commit 56e40cd

Please sign in to comment.