title: Looking for a good example of polymorphic serialization deserialization using jackson with scala
body: <p>Looking for a good example of polymorphic serialization deserialization  using jackson with scala</p>
  
  <p>got an exception : </p>
  
  <blockquote>
    <p>Exception in thread "main" 
    Blockquote
    org.codehaus.jackson.map.exc.UnrecognizedPropertyException:
    Unrecognized field "animals" (Class Zoo), not marked as ignorable</p>
  </blockquote>
  
  <p>after trying the following code :</p>
  
  <pre><code>    import org.codehaus.jackson.annotate.{ JsonTypeInfo, JsonSubTypes }
      import org.codehaus.jackson.annotate.JsonSubTypes.Type
  
      @JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include= JsonTypeInfo.As.PROPERTY,
        property = "type"
      )
      @JsonSubTypes(Array(
        new Type(value= classOf[Cat] , name = "cat"),
        new Type(value= classOf[Dog] , name = "dog")
      )
      )
      abstract class Animal {
        val name:String  = "NoName"
  
      }
  class Cat extends Animal{
     val favoriteToy = "edi"
  }
  class Dog extends Animal{
    val breed = "German Shepherd"
    val color = "brown"
  }
  class Zoo {
    val animals = new scala.collection.mutable.ListBuffer[Animal]
  }
  import org.codehaus.jackson.map.ObjectMapper
  
  object Foo {
    def main (args:Array[String]) {
      val mapper = new ObjectMapper()
       mapper.setPropertyNamingStrategy(CamelCaseNamingStrategy )
      val source = scala.io.Source.fromFile("input.json" )
      val input = source.mkString
      source.close
       val zoo = mapper.readValue(input,classOf[Zoo])
      println(mapper.writeValueAsString(zoo))
    }
  import org.codehaus.jackson.map.introspect.{AnnotatedField, AnnotatedMethod}
  import org.codehaus.jackson.map.{MapperConfig, PropertyNamingStrategy}
  
  object CamelCaseNamingStrategy extends PropertyNamingStrategy{
   override def nameForGetterMethod (config: MapperConfig[_], method: AnnotatedMethod, defaultName: String) =
   {
     translate(defaultName)
   }
  
   override def nameForSetterMethod (config: MapperConfig[_], method: AnnotatedMethod, defaultName: String) = {
     translate(defaultName)
   }
  
    override def nameForField  (config: MapperConfig[_], field: AnnotatedField, defaultName: String) = {
      translate(defaultName)
    }
  
    def translate(defaultName:String) = {
      val nameChars = defaultName.toCharArray
      val nameTranslated = new StringBuilder(nameChars.length*2)
      for ( c &lt;- nameChars){
        if (Character.isUpperCase(c)){
          nameTranslated.append("_")
        }
        nameTranslated.append( Character.toLowerCase(c))
      }
      nameTranslated.toString
    }
  
  }
  </code></pre>
  
  <p>file input.json</p>
  
  <pre><code>{
  "animals":
    [
      {"type":"dog","name":"Spike","breed":"mutt","color":"red"},
      {"type":"cat","name":"Fluffy","favoriteToy":"spider ring"}
    ]
  }
  </code></pre>