jackson反序列化自定义对象列表

时间:2013-08-09 05:16:17

标签: java json jackson

我有以下JSON,我正在尝试使用Jackson API反序列化

"attachments": {
            "file1": {
                "content": "",
                "name": "sample.json",
                "type": "application/json"
            },
            "file2": {
                "content": ""
                "name": "myspreadsheet.xlsx",
                "type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            }
        },

我基本上需要一个Attachment类,它有一个AttachmentFile对象列表,如下所示:

public static AttachmentFile {
    String content;
    String name;
    String type;
}

如何使用自定义反序列化器实现此目的?

谢谢!

2 个答案:

答案 0 :(得分:1)

我使用jackson 1.9.12并且没有问题序列化和反序列化HashMap。

附件:

import java.util.Map;

public class Attachments
{
   //@JsonDeserialize(as=HashMap.class) // use this if you want a HashMap
   public Map<String, AttachmentFile> attachments;
   public Attachments() {
   }
   public Attachments(
      final Map<String, AttachmentFile> attachments
   ) {
      this.attachments = attachments;
   }
}

AttachmentFile:

public class AttachmentFile
{
   public String content;
   public String name;
   public String type;
   public AttachmentFile() {
   }
   public AttachmentFile(
      final String content,
      final String name,
      final String type
   ) {
      this.content = content;
      this.name = name;
      this.type = type;
   }
}

测试:

import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.junit.Assert;
import org.junit.Test;

public class AttachmentsTest
{
   @Test
   public void test()
   {
      try {

         final Map<String, AttachmentFile> attachments = new HashMap<String, AttachmentFile>();
         attachments.put(
            "file1",
            new AttachmentFile(
               "",
               "sample.json",
               "application/json"
            )
         );
         attachments.put(
            "file2",
            new AttachmentFile(
               "",
               "myspreadsheet.xlsx",
               "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            )
         );
         final Attachments inputData = new Attachments();
         inputData.attachments = attachments;
         final ObjectMapper jsonMapper = new ObjectMapper();
         jsonMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
         final String jsonString = jsonMapper.writeValueAsString(inputData);
         //System.out.println(jsonString);
         final Attachments outputData = jsonMapper.readValue(jsonString, inputData.getClass());
         Assert.assertNotNull(outputData);
         Assert.assertEquals(inputData.attachments.size(), outputData.attachments.size());
         Assert.assertEquals(inputData.attachments.get("file1").name, outputData.attachments.get("file1").name);
         Assert.assertEquals(inputData.attachments.get("file2").name, outputData.attachments.get("file2").name);

      } catch (final Exception e) {
         Assert.fail(e.getMessage());
      }
   }
}

答案 1 :(得分:0)

您不需要自定义反序列化程序。

使用jacksons @JsonAnySetter注释,您可以在附件类中编写一个类似于此的方法

class Attachment
{
     ArrayList files = new ArrayList();

     @JsonAnySetter
     public void setFile(String name, Object value)
     {
          files.add(value);
     }
}

您可能需要调整该代码(使用更多注释),以确保将value反序列化为AttachmentFile。但我认为你得到了基本的想法。