Skip to content

Commit cfdece6

Browse files
Keep Order of Fields like in the given Java Bean (#76)
* Adding Fields Order flag tot he object mapper * add test * do not use for deserz. * fix merge conflict * remove unused imports
1 parent 20bbc94 commit cfdece6

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

src/main/java/dev/toonformat/jtoon/util/ObjectMapperSingleton.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.toonformat.jtoon.util;
22

33
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import tools.jackson.databind.MapperFeature;
45
import tools.jackson.databind.ObjectMapper;
56
import tools.jackson.databind.json.JsonMapper;
67
import tools.jackson.module.afterburner.AfterburnerModule;
@@ -30,10 +31,8 @@ public static ObjectMapper getInstance() {
3031
INSTANCE = JsonMapper.builder()
3132
.changeDefaultPropertyInclusion(incl -> incl.withValueInclusion(JsonInclude.Include.ALWAYS))
3233
.addModule(new AfterburnerModule()) // Speeds up Jackson by 20–40% in most real-world cases
33-
// .disable(MapperFeature.DEFAULT_VIEW_INCLUSION) in Jackson 3 this is default disabled
34-
// .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) in Jackson 3 this is default disabled
35-
// .configure(SerializationFeature.INDENT_OUTPUT, false) in Jackson 3 this is default false
3634
.defaultTimeZone(TimeZone.getTimeZone("UTC")) // set a default timezone for dates
35+
.disable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
3736
.build();
3837
}
3938
return INSTANCE;

src/test/java/dev/toonformat/jtoon/JToonTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.lang.reflect.InvocationTargetException;
1010
import java.math.BigInteger;
1111
import java.time.Instant;
12+
import java.util.ArrayList;
1213
import java.util.LinkedHashMap;
1314
import java.util.List;
1415
import java.util.Map;
@@ -1200,6 +1201,24 @@ void encodesListOfAnnotatedPOJOs() {
12001201
102,Monitor,299.99""",
12011202
encode(obj));
12021203
}
1204+
1205+
@Test
1206+
@DisplayName("encodes nested POJO with keeping the order")
1207+
void encodesNestedWithKeepingTheOrder() {
1208+
List<HotelInfoLlmRerankDTO> hotelList = new ArrayList<>();
1209+
for (int i = 0; i < 5; i++) {
1210+
hotelList.add(new HotelInfoLlmRerankDTO("A" + (i + 1),
1211+
"hotelId " + i,
1212+
"hotelName",
1213+
"hotelBrand",
1214+
"hotelCategory",
1215+
"hotelPrice",
1216+
"hotelAddressDistance"
1217+
));
1218+
}
1219+
1220+
assertTrue(encode(hotelList).startsWith("[5]{no,hotelId,hotelName,hotelBrand,hotelCategory,hotelPrice,hotelAddressDistance}:"));
1221+
}
12031222
}
12041223
}
12051224
@Test

src/test/java/dev/toonformat/jtoon/TestPojos.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,16 @@ public record AnnotatedEmployee(
124124
Address address,
125125
@JsonIgnore String ssn) {
126126
}
127+
128+
/**
129+
* Record for checking the field order in the output.
130+
*/
131+
public record HotelInfoLlmRerankDTO(String no,
132+
String hotelId,
133+
String hotelName,
134+
String hotelBrand,
135+
String hotelCategory,
136+
String hotelPrice,
137+
String hotelAddressDistance) {}
127138
}
128139

0 commit comments

Comments
 (0)