user2395365
user2395365

Reputation: 2149

jackson deserialize list of custom objects

I have the following JSON which I'm trying to deserialize using the Jackson API

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

I basically need an Attachment class which has a list of AttachmentFile objects which would look like this:

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

How can I achieve this using a custom deserializer?

Thanks!

Upvotes: 0

Views: 2484

Answers (2)

Frank Olschewski
Frank Olschewski

Reputation: 500

I use jackson 1.9.12 and there are no problems serialize and deserialize HashMap.

Attachments:

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

Test:

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

Upvotes: 1

John Smith
John Smith

Reputation: 2332

You do not need a custom deserializer.

Using jacksons @JsonAnySetter annotation, you can write a method in your attachment class that looks like this

class Attachment
{
     ArrayList files = new ArrayList();

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

You may have to tweak that code (using more annotations), to make sure that value is deserialized as AttachmentFile. But I think you get the basic idea.

Upvotes: 0

Related Questions