寻找使用带有scala的jackson进行多态序列化反序列化的一个很好的例子

时间:2013-06-16 16:04:55

标签: scala jackson

使用带scala的jackson

寻找多态序列化反序列化的一个很好的例子

有一个例外:

  

线程“main”中的异常   大段引用   org.codehaus.jackson.map.exc.UnrecognizedPropertyException:   无法识别的字段“动物”(类动物园),未标记为可忽略的

尝试以下代码后

    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 <- nameChars){
      if (Character.isUpperCase(c)){
        nameTranslated.append("_")
      }
      nameTranslated.append( Character.toLowerCase(c))
    }
    nameTranslated.toString
  }

}

file input.json

{
"animals":
  [
    {"type":"dog","name":"Spike","breed":"mutt","color":"red"},
    {"type":"cat","name":"Fluffy","favoriteToy":"spider ring"}
  ]
}

2 个答案:

答案 0 :(得分:7)

如果您在Scala中进行多态反序列化,我强烈建议您使用案例类和Jackson的scala模块。

object Test {
  import com.fasterxml.jackson.annotation.JsonSubTypes.Type
  import com.fasterxml.jackson.annotation.{JsonSubTypes, JsonTypeInfo}
  import com.fasterxml.jackson.databind.ObjectMapper
  import com.fasterxml.jackson.module.scala.DefaultScalaModule
  import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper

  @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")
  ))
  trait Animal

  case class Dog(name: String, breed: String, leash_color: String) extends Animal
  case class Cat(name: String, favorite_toy: String) extends Animal
  case class Zoo(animals: Iterable[Animal])

  def main(args: Array[String]): Unit = {
    val objectMapper = new ObjectMapper with ScalaObjectMapper
    objectMapper.registerModule(DefaultScalaModule)

    val dogStr = """{"type": "dog", "name": "Spike", "breed": "mutt",  "leash_color": "red"}"""
    val catStr = """{"type": "cat", "name": "Fluffy", "favorite_toy": "spider ring"}"""
    val zooStr = s"""{"animals":[$dogStr, $catStr]}"""

    val zoo = objectMapper.readValue[Zoo](zooStr)

    println(zoo)
    // Prints: Zoo(List(Dog(Spike,mutt,red), Cat(Fluffy,spider ring)))
  }
}

答案 1 :(得分:5)

好的,这里有一个基于Deserialize JSON with Jackson into Polymorphic by Programmer Bruce的scala的工作示例:

import org.codehaus.jackson.annotate.JsonSubTypes.Type
import org.codehaus.jackson.annotate.{JsonSubTypes, JsonTypeInfo}

@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 {
  var name:String =""

}

class Dog extends Animal{
  var breed= "German Shepherd"
  var color = "brown"
}

class Cat extends Animal{
   var favoriteToy:String = "nothing"
}


class Zoo {
  var animals = new Array[Animal](5)
}


import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility
import org.codehaus.jackson.annotate.JsonMethod
import org.codehaus.jackson.map.{DeserializationConfig, ObjectMapper}

object Foo {
   def main (args:Array[String]) {
    val mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD,Visibility.ANY)
     mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,false)
    val source =  scala.io.Source.fromFile("/input.json" )
    val input = source.mkString
     println("input " + input)
    source.close
     val zoo = mapper.readValue(input,classOf[Zoo])

    println(mapper.writeValueAsString(zoo))
  }

}
  

file:input.json {“animals”:[       { “类型”: “狗”, “名”: “秒杀”, “品种”: “笨蛋”, “色”: “红色”},         {“type”:“cat”,“name”:“Fluffy”,“favoriteToy”:“蜘蛛戒指”}        ]}