Skip to content

Commit

Permalink
feat: implement java code and tests for jackson 2.17.2
Browse files Browse the repository at this point in the history
  • Loading branch information
arvgord committed Oct 15, 2024
1 parent efcd742 commit 8ed6668
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 20 deletions.
53 changes: 34 additions & 19 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
# Compiled class file
*.class
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

# Log file
*.log
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
.idea/
*.iws
*.iml
*.ipr

# BlueJ files
*.ctxt
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

# Mobile Tools for Java (J2ME)
.mtj.tmp/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
### VS Code ###
.vscode/

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*
### Mac OS ###
.DS_Store
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# jackson-databind-sorting-issue-java
# jackson-databind-sorting-issue-java

Related issues:
- [Jackson Databind Issue #4751](https://github.com/FasterXML/jackson-databind/issues/4751)
- [Jackson Databind Issue #4752](https://github.com/FasterXML/jackson-databind/issues/4752)
62 changes: 62 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.arvgord</groupId>
<artifactId>jackson-databind-sorting-issue-java</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<jackson.version>2.17.2</jackson.version>
<junit.version>5.8.1</junit.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.1</version>
</plugin>
</plugins>
</build>
</project>
23 changes: 23 additions & 0 deletions src/main/java/com/arvgord/FirstObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.arvgord;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonAutoDetect;

import java.util.LinkedHashMap;
import java.util.Map;

@JsonAutoDetect(
fieldVisibility = JsonAutoDetect.Visibility.ANY,
getterVisibility = JsonAutoDetect.Visibility.NONE
)
public class FirstObject {

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

public String getTransactionId() {
return (String) data.get("transactionId");
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/arvgord/SecondObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.arvgord;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonAutoDetect;

import java.util.LinkedHashMap;
import java.util.Map;

@JsonAutoDetect(
fieldVisibility = JsonAutoDetect.Visibility.ANY,
getterVisibility = JsonAutoDetect.Visibility.NONE
)
public class SecondObject {

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

public SecondObject() {}

SecondObject(String transactionId) {
this.transactionId = transactionId;
}

public String getTransactionId() {
return transactionId;
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/arvgord/ThirdObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.arvgord;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonAutoDetect;

import java.util.LinkedHashMap;
import java.util.Map;

@JsonAutoDetect(
fieldVisibility = JsonAutoDetect.Visibility.ANY,
getterVisibility = JsonAutoDetect.Visibility.NONE
)
public class ThirdObject {

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

ThirdObject() {}

public ThirdObject(String transactionId) {
this.transactionId = transactionId;
}

@JsonAnySetter
public void setData(String key, Object value) {
this.data.put(key, value);
}

public String getTransactionId() {
return transactionId;
}
}
80 changes: 80 additions & 0 deletions src/test/java/com/arvgord/JacksonSortingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.arvgord;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class JacksonSortingTest {

private final ObjectMapper objectMapper = new ObjectMapper();

private final String JSON_INPUT_FIRST = """
{
"b": 2,
"a": 1,
"transactionId": "test",
"c": [
{
"id": "3",
"value": "c"
},
{
"id": "1",
"value": "a"
},
{
"id": "2",
"value": "b"
}
]
}
""";

private final String JSON_INPUT_SECOND_AND_THIRD = """
{
"transactionId": "test",
"b": 2,
"a": 1,
"c": [
{
"id": "3",
"value": "c"
},
{
"id": "1",
"value": "a"
},
{
"id": "2",
"value": "b"
}
]
}
""";

private <T> void testSerializationDeserialization(String jsonInput, Class<T> clazz) throws Exception {
T deserializedObject = objectMapper.readValue(jsonInput, clazz);
String serializedJson = objectMapper.writeValueAsString(deserializedObject);

String expectedJson = objectMapper.readTree(jsonInput).toPrettyString();
String actualJson = objectMapper.readTree(serializedJson).toPrettyString();

assertEquals(expectedJson, actualJson);
}

@Test
public void testSerializationAndDeserializationForFirstObject() throws Exception {
testSerializationDeserialization(JSON_INPUT_FIRST, FirstObject.class);
}

@Test
public void testSerializationAndDeserializationForSecondObject() throws Exception {
testSerializationDeserialization(JSON_INPUT_SECOND_AND_THIRD, SecondObject.class);
}

@Test
public void testSerializationAndDeserializationForThirdObject() throws Exception {
testSerializationDeserialization(JSON_INPUT_SECOND_AND_THIRD, ThirdObject.class);
}
}

0 comments on commit 8ed6668

Please sign in to comment.