0

我有以下 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;
}

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

谢谢!

4

2 回答 2

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;
   }
}

附件文件:

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());
      }
   }
}
于 2013-08-09T07:38:24.847 回答
0

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

使用 jacksons@JsonAnySetter注释,您可以在附件类中编写一个方法,如下所示

class Attachment
{
     ArrayList files = new ArrayList();

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

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

于 2013-08-09T05:33:32.900 回答