json4s/json4s — pull request #52 by tlazaro (2013-06-24 15:15:20+00:00)
Title: Added support for Either

Hi,

We have a use case in our team when parsing a Json that we need to use Either. That is, in some scenarios we receive one type for a given field, in other scenarios we receive a completely different type in that field. Given that scenario we have a `case class OurStructure(value: Either[List[Something], SomethingElse])`. Changing the Json API we consume is not an option.

We were using Jerkson with Scala 2.9 and moved to 2.10 and Json4s. Now all the Eithers that were working before broke.

There is no support for Either in the library so we tried creating a custom serializer... and failed :(.

We were able to get it working for Eithers of basic types. It doesn't work though for Eithers of types with type arguments of their own (i.e Either[List[Int], Int] fails on the List). IMHO the CustomSerializer simply can't work in that case. It provides a TypeInfo instead of a ScalaType and thus loses all the information it should have carried along with the Manifest from the original 'extract' at call site. There is no way to determine the argument type of the List.

After concluding that CustomSerializer can not possibly work we decided to add direct support for it. Here is a working draft of how to work with Eithers. There are some tests included showing that it works. Please let me know what you think and feel free to change it or ask for me for changes.

Thanks,
Tomás Lázaro