diff --git a/pom.xml b/pom.xml index 4e14ee1f2..d3749e93a 100644 --- a/pom.xml +++ b/pom.xml @@ -151,6 +151,11 @@ ch.obermuhlner big-math + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + 2.14.0 + ch.qos.logback diff --git a/src/main/java/com/hubspot/jinjava/JinjavaConfig.java b/src/main/java/com/hubspot/jinjava/JinjavaConfig.java index 99a54a784..33e468734 100644 --- a/src/main/java/com/hubspot/jinjava/JinjavaConfig.java +++ b/src/main/java/com/hubspot/jinjava/JinjavaConfig.java @@ -19,6 +19,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.google.common.collect.ImmutableSet; import com.hubspot.jinjava.el.JinjavaInterpreterResolver; import com.hubspot.jinjava.el.JinjavaObjectUnwrapper; @@ -157,7 +158,8 @@ private JinjavaConfig(Builder builder) { private ObjectMapper setupObjectMapper(@Nullable ObjectMapper objectMapper) { if (objectMapper == null) { - objectMapper = new ObjectMapper(); + objectMapper = new ObjectMapper().registerModule(new JavaTimeModule()); + if (legacyOverrides.isUseSnakeCasePropertyNaming()) { objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilter.java index 0e7844ee8..41440d367 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilter.java @@ -1,9 +1,11 @@ package com.hubspot.jinjava.lib.filter; -import com.fasterxml.jackson.databind.node.POJONode; +import com.fasterxml.jackson.core.JsonProcessingException; import com.hubspot.jinjava.doc.annotations.JinjavaDoc; import com.hubspot.jinjava.doc.annotations.JinjavaParam; import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; +import com.hubspot.jinjava.interpret.InvalidInputException; +import com.hubspot.jinjava.interpret.InvalidReason; import com.hubspot.jinjava.interpret.JinjavaInterpreter; import com.hubspot.jinjava.objects.date.PyishDate; import java.util.Map; @@ -48,7 +50,16 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args) ) { varStr = Objects.toString(var); } else { - varStr = new POJONode(var).toPrettyString(); + try { + varStr = + interpreter + .getConfig() + .getObjectMapper() + .writerWithDefaultPrettyPrinter() + .writeValueAsString(var); + } catch (JsonProcessingException e) { + throw new InvalidInputException(interpreter, this, InvalidReason.JSON_WRITE); + } } return EscapeFilter.escapeHtmlEntities( diff --git a/src/test/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilterTest.java b/src/test/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilterTest.java index 822a83c97..286a2771e 100644 --- a/src/test/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilterTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilterTest.java @@ -64,6 +64,7 @@ public void ppList() { @Test public void ppObject() { MyClass myClass = new MyClass(); + assertThat(f.filter(myClass, i)) .isEqualTo( String.format( @@ -73,7 +74,8 @@ public void ppObject() { " "nestedClass" : {\n" + " "fooField" : "%s",\n" + " "barField" : %d\n" + - " }\n" + + " },\n" + + " "date" : 1702252800.000000000\n" + "}){%% endraw %%}", myClass.getFoo(), myClass.getBar(), @@ -94,6 +96,10 @@ public int getBar() { return 123; } + public ZonedDateTime getDate() { + return ZonedDateTime.of(2023, 12, 11, 0, 0, 0, 0, ZonedDateTime.now().getZone()); + } + public MyNestedClass getNestedClass() { return new MyNestedClass(); }