Skip to content

Commit

Permalink
JSON output with sorted map entries.
Browse files Browse the repository at this point in the history
  • Loading branch information
hs-lsong committed Oct 24, 2023
1 parent 23695ab commit 0a8202a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/hubspot/jinjava/JinjavaConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

import static com.hubspot.jinjava.lib.fn.Functions.DEFAULT_RANGE_LIMIT;

import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.google.common.collect.ImmutableSet;
import com.hubspot.jinjava.el.JinjavaInterpreterResolver;
import com.hubspot.jinjava.el.JinjavaObjectUnwrapper;
Expand Down Expand Up @@ -161,6 +163,10 @@ private ObjectMapper setupObjectMapper(@Nullable ObjectMapper objectMapper) {
if (legacyOverrides.isUseSnakeCasePropertyNaming()) {
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
}
return objectMapper
.copy()
.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
}
return objectMapper;
}
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/com/hubspot/jinjava/lib/filter/ToJsonFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

import static org.assertj.core.api.Java6Assertions.assertThat;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hubspot.jinjava.BaseInterpretingTest;
import com.hubspot.jinjava.Jinjava;
import com.hubspot.jinjava.JinjavaConfig;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Before;
Expand All @@ -27,4 +32,44 @@ public void itWritesObjectAsString() {
assertThat(filter.filter(testMap, interpreter))
.isEqualTo("{\"testArray\":[4,1,2],\"testString\":\"testString\"}");
}

@Test
public void itWritesObjectWithOrderedKeyAsString() {
int[] testArray = new int[] { 4, 1, 2 };
Map<String, Object> testMap = new HashMap<>();
testMap.put("testString", "testString");
testMap.put("testArray", testArray);
testMap.put("another", "ab");
Map<String, Object> nested = new HashMap<>();
nested.put("innerKey2", "v2");
nested.put("innerKey1", "v1");
testMap.put("nested", nested);
assertThat(filter.filter(testMap, interpreter))
.isEqualTo(
"{\"another\":\"ab\",\"nested\":{\"innerKey1\":\"v1\",\"innerKey2\":\"v2\"},\"testArray\":[4,1,2],\"testString\":\"testString\"}"
);
}

@Test
public void itWritesObjectWithoutOrderedKeyAsString() {
int[] testArray = new int[] { 4, 1, 2 };
Map<String, Object> testMap = new HashMap<>();
testMap.put("testString", "testString");
testMap.put("testArray", testArray);
testMap.put("another", "ab");
Map<String, Object> nested = new HashMap<>();
nested.put("innerKey2", "v2");
nested.put("innerKey1", "v1");
testMap.put("nested", nested);

Jinjava jinjava = new Jinjava(
JinjavaConfig.newBuilder().withObjectMapper(new ObjectMapper()).build()
);
JinjavaInterpreter interpreter = jinjava.newInterpreter();
assertThat(filter.filter(testMap, interpreter))
.isEqualTo(
"""
{"testArray":[4,1,2],"another":"ab","testString":"testString","nested":{"innerKey2":"v2","innerKey1":"v1"}}"""
);
}
}

0 comments on commit 0a8202a

Please sign in to comment.