简体   繁体   English

杰克逊漂亮的印花绳子

[英]Jackson pretty print string

At some places we are logging JSON strings. 在某些地方,我们正在记录JSON字符串。 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 编辑问题不是记录器,杰克逊在没有序列化的情况下无法打印字符串,请参阅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 真正的原因在这里描述: 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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