Java binary serialization fails due to Jackson

I am using jackson 2 to convert json to java object. So far, so good. But I also use hazelcast to distribute objects in the cluster. Therefore, all beans must be java.io.Serializable. When I read Object from json like this:

ObjectMapper mapper = new ObjectMapper();
mapper.addMixInAnnotations(AbstractBean.class, MongoIdMixIn.class);

// this is to prevent from failing on missing type class property: @JsonProperty("@class")
Object tgtObject = targetClass.newInstance();
mapper.readerForUpdating(tgtObject).readValue(dbo.toString());

// put into hazelcast map
target.put(dbo.get(keyColumn), tgtObject); 

I get an exception from hazelcast:

java.io.NotSerializableException: com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer

I am wondering where com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer comes from, since Object is a simple java bean (but uses inheritance).

My abstract class:

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@javaClass")
public abstract class AbstractBean implements Serializable {
    @JsonIgnore public static final transient IMarkupParser MARKUP_PARSER = new WikiMarkupParser();

    @JsonProperty("id")
    private String id;

    @JsonProperty("@class")
    private String clazz;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getClazz() {
        return this.getClass().getSimpleName();
    }
}

And my baby:

public class Posting extends AbstractBean {
    private String postingSource;
    private String languageCode;

    public String getPostingSource() {
        return postingSource;
    }

    public void setPostingSource(String postingSource) {
        this.postingSource = postingSource;
    }

    public String getLanguageCode() {
        return languageCode;
    }

    public void setLanguageCode(String languageCode) {
        this.languageCode = languageCode;
    }
}

I have no idea why the serailizer will even try to serialize mixins, as they are not part of the bean, but here they are (yes, I tried to make them serializable too, like a test, no luck)

public interface IdMixins extends Serializable {
}

public interface MongoIdMixIn extends IdMixins {
    @JsonProperty("_id")
    @JsonSerialize(using = MongoIdSerializer.class)
    public String getId();

    @JsonProperty("_id")
    @JsonDeserialize(using = MongoIdDeserializer.class)
    public void setId(String id);
}

public class MongoIdDeserializer extends JsonDeserializer<String> implements Serializable {
    private static final long serialVersionUID = -5404276857799190647L;

    @Override
    public String deserialize(JsonParser jp, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        String value = null;

        String tmp = jp.getText(); // {
        validate(jp, tmp,"{");
        int curly = 1;

        while (jp.nextToken() != null) {
            String v = jp.getText();

            if (v.equals("{")) curly++;

            if (v.equals("$oid")) {
                jp.nextToken();
                value = jp.getText();
            }

            if (v.equals("}")) curly--;
            if (curly<=0) return value;
        }

        return null;
    }

    private void validate(JsonParser jsonParser, String input, String expected) throws JsonProcessingException {
        if (!input.equals(expected)) {
            throw new JsonParseException("Unexpected token: " + input, jsonParser.getTokenLocation());
        }
    }
}

public class MongoIdSerializer extends JsonSerializer<String> implements Serializable {
    private static final long serialVersionUID = 3435689991839324194L;

    @Override
    public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeFieldName("$oid");
        jsonGenerator.writeString(s);
        jsonGenerator.writeEndObject();
    }
}
+4
source share
2

! - ObjectMapper. , Posting , . Stacktrace com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer Exception !... : -)

+1

- 1 . . ;-).

, TypeWrappedDeserializer AbstractBean. , .

?

for (Field field : tgtObject.getClass().getDeclaredFields() )
{
   // you can replace this by your logging method
   System.out.println("Field: " + field.getName() + ":" + field.getType());
}
for (Field field : tgtObject.getClass().getSuperclass().getDeclaredFields() )
{
   // you can replace this by your logging method
   System.out.println("Field: " + field.getName() + ":" + field.getType());
}

, .

0

All Articles