Deserializing a List of ObjectId's?

53 views
Skip to first unread message

Nav

unread,
Apr 25, 2017, 3:46:18 AM4/25/17
to Jongo
I'm able to deserialize ObjectId's into strings using this:

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.jongo.marshall.jackson.oid.ObjectId;
import com.myproject.custompackage.NoObjectIdSerializer;

public class MyClass {
   
@ObjectId
   
@JsonSerialize(using = NoObjectIdSerializer.class)
   
private String _id;
}


where the NoObjectIdSerializer is:

import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

public class NoObjectIdSerializer extends JsonSerializer<String> {
   
@Override
   
public void serialize(String value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
        jgen
.writeString(value);
   
}    
}


However, this doesn't work when I have a List of Strings.
private List<String> vis;

where each String in the above list is an ObjectId.

The error is:
Unable to unmarshall result to class Caused by: com.fasterxml.jackson.databind.JsonMappingException: Problem deserializing property 'vis' (expected type: [collection type; class java.util.List, contains [simple type, class org.jongo.marshall.jackson.oid.ObjectId]]; actual type: java.lang.String), problem: Can not set java.util.List field com.myproject.custompackage.MyClass.vis to java.lang.String
 at [Source: de.undercouch.bson4jackson.io.LittleEndianInputStream@7d9f7daa; pos: 519] (through reference chain: com.myproject.custompackage.MyClass["vis"])

On trying to understand it, I feel I'm supposed to create a new NoObjectIdSerializer class that'll process a List of ObjectId's, but that I'll have to figure out.

If you know what the issue is and could help with a solution, it would be much appreciated.
Reply all
Reply to author
Forward
0 new messages