Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom pretty printer using Jackson library

I have data in a Map object and I want to print it in a json format. I tried using DefaultPrettyPrinter

mapper.writerWithDefaultPrettyPrinter().writeValue(filePath, mapObject);

but the format is not what I expected. I am getting output like this:

{
  "arrVals" : ["value-1","value-2"]
}

I want output like this:

{
  "arrVals" : [
    "value-1",
    "value-2"
  ]
}
like image 455
Manisha Avatar asked Aug 07 '13 08:08

Manisha


1 Answers

You need indentation before Array Values. You can use indentArraysWith method to set the Lf2SpacesIdenter object which will basically add a line feed followed by 2 spaces. This might solve your problem.

DefaultPrettyPrinter pp = new DefaultPrettyPrinter();
pp.indentArraysWith(new Lf2SpacesIndenter());
mapper.writer(pp).writeValue(filePath, mapObject);
like image 106
Mady Avatar answered Nov 15 '22 10:11

Mady