7

I have a class that has more than a dozen properties. For most of the properties of primitive type, I hope to use the default BeanSerializer and BeanDeserializer or whatever to reduce the cumbersome code I need to write. For other properties of custom and array types, I want to do some custom serializer/deserializer. Note that I am not able to change the underlying JSON string. But I have full access to the android code. I am using Jackson 1.7.9/Ektorp 1.1.1.

shall I subclass BeanDeserializer? I am having trouble with that. It expects a default constructor with no parameters but I don't know how to call the super constructor.

class MyType{
    // a dozen properties with primitive types String, Int, BigDecimal
    public Stirng getName();
    public void setName(String name);

    // properties that require custom deserializer/serializer
    public CustomType getCustom();
    public void setCustom(CustomType ct);
}

class MyDeserializer extends BeanDeserialzer{
    // an exception is throw if I don't have default constructor.
    // But BeanDeserializer doesn't have a default constructor
    // It has the below constructor that I don't know how to fill in the parameters
    public MyDeserializer(AnnotatedClass forClass, JavaType type,
        BeanProperty property, CreatorContainer creators,
        BeanPropertyMap properties,
        Map<String, SettableBeanProperty> backRefs,
        HashSet<String> ignorableProps, boolean ignoreAllUnknown,
        SettableAnyProperty anySetter) {
    super(forClass, type, property, creators, properties, backRefs, ignorableProps,
            ignoreAllUnknown, anySetter);
}
    @Override
    public Object deserialize(JsonParser jp, DeserializationContext dc, Object bean)
        throws IOException, JsonProcessingException {
    super.deserialize(jp, dc, bean);
        MyType c = (MyType)bean;        

            ObjectMapper mapper = new ObjectMapper();

            JsonNode rootNode = mapper.readValue(jp, JsonNode.class);
            // Use tree model to construct custom
            // Is it inefficient because it needs a second pass to the JSON string to construct the tree?
            c.setCustom(custom);
            return c;
}
}

I searched Google but couldn't find any helpful examples/tutorial. If anyone can send me some working examples that would be great! Thanks!

PokerIncome.com
  • 1,708
  • 2
  • 19
  • 30

1 Answers1

4

To sub-class BeanSerializer/-Deserializer, you would be better off using a more recent version of Jackson, since this area has been improved with explicit support via BeanSerializerModifier and BeanDeserializerModifier, which can alter configuration of instances.

But just to make sure, you can also specify custom serializer/deserializer to just be used on individual properties, like so:

class Foo {
   @JsonSerialize(using=MySerializer.class)
   public OddType getValue();
}
StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • Thanks for the idea! I will try it out to see if it works. I am using Ektorp for Android and they suggest 1.1.1 so that's why I am using Jackson 1.7.9. But it might work if I upgrade. – PokerIncome.com Oct 29 '11 at 00:43
  • the highest version I can go is 1.8.5. Any suggestion? I will try out your JsonSerializer idea on OddType. – PokerIncome.com Dec 04 '11 at 07:51
  • 1.8 has better overridability than 1.7 so it might actually work. Using BeanSerializerModifier you don't actually have to override BeanSerializer, but could also create custom instances, yet defer other props to default settings. Also, your custom serializers can look up default ones in 'resolve()' method (if you implement ResolvableSerializer, similar to how BeanSerializer does). Ideally you would avoid sub-classing BeanSerializers if possible, just for simplicity; but if you need to sub-class, that is a supported technique too. – StaxMan Dec 04 '11 at 17:51
  • thanks for the idea again! I basically want to implement JsonUnwrapped (1.9 feature) serialize/deserialize with 1.8.5. What is the simplest way to do? I have a hard time figure out how to use BeanSerializerModifier. Do you have any examples or documentations? Thanks a lot! – PokerIncome.com Dec 04 '11 at 21:46
  • Going to use this example and extends JsonDeserializer to deserialize every property myself. Seems cumbersome to write but that's the only way I know now! http://javasourcecode.org/html/open-source/jackson/jackson-1.8.5/org/codehaus/jackson/map/deser/TestCustomFactory.CustomBeanDeserializer.java.html – PokerIncome.com Dec 04 '11 at 23:09
  • Doing unwrapped manually is tricky; and Bean(De)SerializerModifier really is an advanced use case. Good luck! – StaxMan Dec 06 '11 at 01:14
  • Looks like this should be @JsonSerialize (no r at the end). – Frans Sep 12 '13 at 08:32