2016-09-16 107 views
1

我正在使用Jackson v2.8.2将JSON序列化为文件。Jackson - 将自定义PrettyPrinter与自定义JsonSerializer一起使用

我已经创建了一个自定义序列化器并实现了serialize方法来根据需要自定义JSON输出。

我调用串行如下:

// myClass is the object I want to serialize 

SimpleModule module = new SimpleModule(); 
module.addSerializer(MyClass.class, new MySerializer()); 

ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT); 
mapper.registerModule(module); 

try 
{ 
    mapper.writeValue(new File("json.txt"), myClass); 
} 

catch (JsonProcessingException e) 
{ 
    ... 
} 

创建的JSON文件和内容看起来不错。

该文件格式根据DefaultPrettyPrinter,但我想用我自己定制PrettyPrinter,我已经实现。

我该怎么做?

我已经试过如下:

MyPrettyPrinter myPrettyPrinter = new MyPrettyPrinter(); 
mapper.writer(myPrettyPrinter); 
mapper.writeValue(new File("json.txt"), myClass); 

但那不是我的调用自定义打印机。

回答

2

原因是编写器的调用返回ObjectWriter的新实例。事实上,ObjectMapper有很多工厂方法可以构建新的对象供您使用。

从ObjectMapper的源代码:

/** 
    * Factory method for constructing {@link ObjectWriter} that will 
    * serialize objects using specified pretty printer for indentation 
    * (or if null, no pretty printer) 
    */ 
    public ObjectWriter writer(PrettyPrinter pp) { 
     if (pp == null) { // need to use a marker to indicate explicit disabling of pp 
      pp = ObjectWriter.NULL_PRETTY_PRINTER; 
     } 
     return _newWriter(getSerializationConfig(), /*root type*/ null, pp); 
    } 

因此,对于你的手段,你应该更改您的代码:让您使用的是正确的作家分配

MyPrettyPrinter myPrettyPrinter = new MyPrettyPrinter(); 
ObjectWriter myWriter = mapper.writer(myPrettyPrinter); 
myWriter.writeValue(new File("json.txt"), myClass); 

注到myWriter当致电writeValue

以下是使用ObjectMapper和默认漂亮打印机的示例:

public class OMTest { 
    public static void main(String[] args) throws IOException { 
     // test string 
     String json = " {\"a\" : \"b\", \"c\" : \"d\" } "; 
     // mapper 
     ObjectMapper mapper = new ObjectMapper(); 
     // json tree 
     JsonNode tree = mapper.readTree(json); 
     // the objectWriter assigned with a pretty printer 
     ObjectWriter myWriter = mapper.writer(new DefaultPrettyPrinter()); 
     // print without pretty printer (using mapper) 
     System.out.println(mapper.writeValueAsString(tree)); 
     System.out.println(); 
     // print with writer (using the pretty printer) 
     System.out.println(myWriter.writeValueAsString(tree)); 
    } 
} 

此打印:

{"a":"b","c":"d"} 

{ 
    "a" : "b", 
    "c" : "d" 
} 

其中第一行使用映射器,而第二打印使用作家。

干杯,

阿图尔

+0

如果我按照您的建议更改代码,它不会编译 - 类型不匹配:cann不要从ObjectWriter转换为ObjectMapper'。 – ksl

+0

@ksl你是对的。我道歉,我更新了我的答案。您必须将作者分配给一个变量并使用该变量 – pandaadb

+1

谢谢,这很有用。还要注意''mapper.setDefaultPrettyPrinter(new MyPrettyPrinter())'也适用。我偶然发现了这里 - https://github.com/FasterXML/jackson-databind/issues/689。 – ksl

1

有时候,这取决于你想要达到的目标,你可以使用DefaultPrettyPrinter,只是定制Indenter,如下:

DefaultPrettyPrinter printer = new DefaultPrettyPrinter(); 
Indenter indenter = new CustomSpaceIndenter(); 
printer.indentObjectsWith(indenter); // Indent JSON objects 
printer.indentArraysWith(indenter); // Indent JSON arrays 

有一个相关的问题关于它:Serialize JsonNode to a very specific JSON format in Jackson

相关问题