简体   繁体   中英

Jackson pretty print string

At some places we are logging JSON strings. For better readability we want to pretty print them. Therefore we are using:

v_JsonString = v_ObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(v_Json);

But after passing the result to the logger the string is only single line, the line breaks etc are gone. The used logging pattern is:

<pattern>%d{HH:mm:ss.SSS} %-5level %logger{35} - %msg%n</pattern>

We also tried to do it manually by setting some placeholders and replace them in the pattern:

%replace(%msg){'PLACEHOLDER', '\\n'}

Without any success, every log entry is converted to single line. What is the correct way to preserve formatted string information?

EDIT The problem is not the logger, Jackson does not pretty print strings without serialization, see https://stackoverflow.com/a/39119883/1924298

The problem is solved. The problem was not the logger / settings. The real cause is described here: https://stackoverflow.com/a/20871355/1924298

EDIT

The problem is not solved as getting serialization error when trying with type "Object". I will edit the original question.

Object json = mapper.readValue(input, Object.class);

String indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM