Reputation: 1298
If I have a un-formatted JSON as a String object , can I use a spring integration JSON transformer to pretty print it properly? I didn't see any attributes in reference documentation for pretty printing.
Upvotes: 0
Views: 514
Reputation: 174564
Not out of the box, but you can do it easy enough with Jackson in a POJO transformer - note that you have to convert the JSON to an object and back again...
private final ObjectMapper mapper = new ObjectMapper();
@Transformer(inputChannel = "foo", outputChannel = "bar")
public String transform(String in) throws Exception {
// System.out.println(in);
String out = new String(
mapper.writerWithDefaultPrettyPrinter().writeValueAsBytes(mapper.readValue(in, Object.class)));
// System.out.println(out);
return out;
}
or...
<int:transformer input-channel="foo" output=channel="bar" ref="myJsonPrettyfier" />
...if you are using XML.
You could probably even use an expression
; something like...
<int:transformer ...
expression="new String(@mapper.writerWithDefaultPrettyPrinter().writeValueAsBytes(@mapper.readValue(in, Object.class)))" />
Where mapper
is a <bean/>
for the ObjectMapper
.
Upvotes: 1