Jackson for JSON serialization

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Jackson for JSON serialization

NoSuchLuke

I requested a couple of enhancements (#81 and #80) and I'd like to elaborate on what I'm thinking.

As background, my project already uses Jackson in several places (e.g., Ajax) to serialize beans, and many of those beans contain Joda DateTime fields, which Jackson has a plug-in module to handle. Thymeleaf's internal JSON serializer yields different and incompatible results. So, I want to switch that out for a Jackson equivalent, wherever I use th:inline="javascript".

The solution I found seemed needlessly complex. The obvious starting point is to replace StandardJavaScriptTextInliner with a new class overriding formatEvaluationResult accordingly. But StandardInlineAttrProcessor (and its base class) make it impossible to just plug this new inliner in, so I had to copy-paste the whole thing into a new class before adding my tweak. Then, I needed to get the dialect to use this new processor instead, so I had to subclass the Spring standard dialect. But I think with a little refactoring within Thymeleaf, it wouldn't be necessary to write three new classes.

In fact, I think such changes could be made almost entirely in Spring, something like:

<bean id="standardDialect" class="org.thymeleaf.spring3.dialect.SpringStandardDialect">
    <property name="removedProcessors" value="org.thymeleaf.standard.processor.attr.StandardInlineAttrProcessor" />
    <property name="additionalProcessors">
        <set>
            <bean class="org.thymeleaf.standard.processor.attr.StandardInlineAttrProcessor">
                <property name="additionalInliners">
                    <map>
                        <entry key="javascript">
                            <bean class="org.example.JacksonJavaScriptTextInliner" />
                        </entry>
                    </map>
                </property>
            </bean>
        </set>
    </property>
</bean>

So then I would only need to write org.example.JacksonJavaScriptTextInliner, which is just a few lines.

Better still would be if Thymeleaf had a more built-in solution. Maybe you can break off the Jackson-dependent part into a small thymeleaf-jackson project, which is accessed by, say, th:inline="jackson". Then the last piece is a hook somewhere to provide or configure the Jackson ObjectMapper.

Reply | Threaded
Open this post in threaded view
|

Re: Jackson for JSON serialization

danielfernandez
Administrator
Hi,

Being able to specify the text inlining artifacts as a part of the TemplateEngine or Dialect configuration steps is a nice idea, and will be considered for 2.1.

Thanks,
Daniel.