2015-02-05 96 views
2

我有以下POJO類:傑克遜系列化/樹形結構的反序列化

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "Folder", propOrder = { 
    "id", 
    "parent" 
}) 
@XmlSeeAlso({ 
    FolderTree.class 
}) 
public class Folder 
{  
    protected int id; 
    protected int parent; 

    getters, setters... 
} 

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "FolderTree", propOrder = { 
    "subFolders" 
}) 
@XmlRootElement(name = "folderTree") 
public class FolderTree extends Folder 
{ 
    @XmlElement(name = "subFolder", type = Folder.class) 
    protected List<Folder> subFolders; 

    getter... 
} 

和包裝:

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "Folders", propOrder = { 
    "folders" 
}) 
@XmlRootElement(name = "folders") 
public class Folders 
{ 
    @XmlElement(name = "folder", type = Folder.class) 
    protected List<Folder> folders; 

    getter... 
} 

我需要序列化和反序列化他們在XML和JSON中。 XML工作正常,但在JSON以下結構串行化:

{ 
    "folder": [ 
     { 
      "subFolder": [ 
       { 
        "subFolder": [], 
        "id": 2, 
        "parent": 1 

       }, 
       { 
        "subFolder": [], 
        "id": 3, 
        "parent": 1 
       } 
      ], 
      "id": 1, 
      "parent": null    
     } 
    ] 
} 

解串器不能識別該子文件夾對象(「子文件夾」:[])內的第二場的子文件夾。有沒有辦法讓它只在父文件夾中包含子文件夾字段?或者可以做些什麼來使傑克遜反序列化這個對象?

我無法更改POJO類,因爲它們是由jaxb創建的,並且所有工作都可以正確使用XML。

回答

1

解決方案。

爲了在沒有孩子的情況下襬脫空的subFiolder數組,我禁用了序列化功能中的WRITE_EMPTY_JSON_ARRAYS。我使用Spring和bean看起來是這樣的:

<bean id="objectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"> 
    <property name="annotationIntrospector" ref="jaxbAnnotationIntrospector" /> 
    <property name="featuresToDisable"> 
     <list> 
      <value type="com.fasterxml.jackson.databind.SerializationFeature">WRITE_EMPTY_JSON_ARRAYS</value> 
     </list> 
    </property> 
</bean> 

反序列化,因爲我無法修改POJO類,我添加了一個混入了文件夾類:

@JsonTypeInfo( 
     use = JsonTypeInfo.Id.NAME, 
     include = JsonTypeInfo.As.PROPERTY, 
     property = "folder", 
     defaultImpl = FolderTree.class) 
public class FolderMixin { 

} 

最後,我伸出ObjectMapper設置MixInAnnotations:

public class CustomObjectMapper extends ObjectMapper 
{ 
    public CustomObjectMapper() 
    { 
     super(); 
     customize(); 
    } 

    protected void customize() 
    { 
     registerModule(new SimpleModule() { 
      private static final long serialVersionUID = 1L; 

      public void setupModule(SetupContext context) { 
       super.setupModule(context); 
       context.setMixInAnnotations(Folder.class, FolderMixin.class); 
      }   
     }); 
    } 
} 

我增加了對反序列化都AnnotationIntrospectors:

<bean id="customObjectMapper" class="path.to.my.CustomObjectMapper"> 
    <property name="annotationIntrospector" ref="introspectors" /> 
</bean> 

<!-- introspectors --> 
<bean id="xmlAnnotation" class="com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector"> 
    <constructor-arg value="#{customObjectMapper.getTypeFactory()}" /> 
</bean> 

<bean id="jsonAnnotation" class="com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector" /> 

<bean id="introspectors" class="com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair"> 
    <constructor-arg ref="xmlAnnotation" /> 
    <constructor-arg ref="jsonAnnotation" /> 
</bean> 
<!-- end of introspectors --> 

<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 
    <property name="objectMapper" ref="customObjectMapper"/> 
</bean> 

我使用傑克遜2.4.0。