Category Archives: Uncategorized

Jackson polymorphic serialization with Autovalue

One of the most powerful features in the Java JSON library Jackson, is support for polymorphic serialization. That is, when deserializaing a JSON (or XML) blob to a supertype, with proper configuration, an object of the correct subtype will be instantiated. Likewise, type information will be included in serialization.

This allows for Java clients and servers to communicate using JSON and use a common interface to share types.

Typically this can be achieved using the JsonTypeInfo and JsonSubTypes annotation above. The JsonTypeInfo annotation allows for considerable flexibility in how the type information is included in the serialized object.

However, I did find one situation where using the @JsonSubTypes annotation directly on the interface is insufficient. This is when using <>AutoValue.

Adding @JsonSubTypes.Type(name="horse", value=Horse.class) to Animal will not provide the symmetric serialization we may expect here. Instead we’ll find the value for type in our JSON markup will be the generated type AutoValue_Horse and as a result we will not be able to deserialize the JSON blob to an Animal as that type isn’t known to the Animal interface.

The workaround here is adding additional type information to Horse.

While it requires an additional annotation, adding JsonTypeName to Horse ensures that even the generated type AutoValue_Horse will include the correct type name in the JSON markup so polymorphic serialization behaves as intended!

From Zero to Ruby – Update 1

I’ll start off by saying that  Ruby has taken a bit of a back seat to putting the finishing touches on this site over the least couple week.  Because the blog is built on top of WordPress most of stylistic touches end up being PHP on the server side. It’s been ages since I’ve used PHP, so it’s never bad to brush up, but as I’ve been going back and forth between it and Ruby, I will say that it is much less… friendly?  In either case I’ve been able to put together a couple cool pieces of code that, once I clean up, I’ll share.

So as far as progress on the Ruby front:

 Books & Lessons

Rails Best Practices (Code School) – I’ve had a Code School account for awhile, and I really enjoyed Rails for Zombies (1 & 2) so I more less picked up where I left off.

 Meetups

Arlington RUG – “Ruby on Audio” – A couple really cool presentations on using Ruby to create music.  Both presenters were great, but I will say Joel Straight’s Ruby Drum Machine may have stolen the show.

Relevant Links

100 Vim Commands Every Programmer Should Know – While not expressly Ruby related, I’ve been using Vim for the last 6 months or so as my primary text editor.  I’m decent with the commands, but I want to start working in a larger set of them for day-to-day use.

I hope to ramp up my Ruby intake over the next couple weeks now that the bones of this blog are in place.  More updates to come!