2015-12-10 85 views
3

我有以下情況下的類:salat是否支持de /序列化類型在scala中?

case class DataEntity 
(
    name: String, 
    value: Either[Complex, String] 
) 
case class Complex (x: String, y: String) 

據我所看到的,序列化(寫入的MongoDB)變細,但在反串行化失敗:

com.novus.salat.util.ToObjectGlitch: 

    argument type mismatch 

    $anon$2(class catalog.DataEntity @ [email protected]) toObject failed on: 
    SYM: catalog.DataEntity 
    CONSTRUCTOR 
public catalog.DataEntity(java.lang.String,scala.util.Either<catalog.Complex, java.lang.String>) 

---------- CONSTRUCTOR EXPECTS FOR PARAM [0] -------------- 
NAME:   name 
TYPE:   java.lang.String 
DEFAULT ARG [Missing, but unnecessary because input value was supplied] 
@Ignore  false 
---------- CONSTRUCTOR INPUT ------------------------ 
TYPE: java.lang.String 
VALUE: 
TESTTEXT 
------------------------------------------------------------ 


---------- CONSTRUCTOR EXPECTS FOR PARAM [1] -------------- 
NAME:   value 
TYPE:   scala.util.Either<catalog.Complex, java.lang.String> 
DEFAULT ARG [Missing, but unnecessary because input value was supplied] 
@Ignore  false 
---------- CONSTRUCTOR INPUT ------------------------ 
TYPE: com.mongodb.BasicDBList 
VALUE: 
[ [ "File" , "/bin/ls" ... 
------------------------------------------------------------ 

上面的輸出是手動與上面的簡化案例類匹配。 我正在使用salatDAO讀取和寫入數據庫。

回答

0

Either目前不支持。我相應地更新了「支持的類型」wiki。

注意,在薩拉特1.10.0-快照時,你會得到像下面這樣(當試圖從JSON字符串反序列化)的錯誤:

scala> SalatExamples.eitherHolder() 
serialized EitherHolder(Left(data)) to { "_typeHint" : "com.novus.salat.examples.EitherHolder" , "either" : [ "data"]} for storage in mongo 

com.novus.salat.json.UnsupportedJsonTransformationException: serialize: Unsupported JSON transformation for class='scala.util.Left', value='Left(data)' 

NOTE: salat has certain limitations. It cannot deserialize JSON into case classes having fields such as: 
- Arrays such as Array[String] 
- Nested collections such as Map[String, List[String]] 
- Optional collections such as Option[List[String]] 

For more information, please visit: https://github.com/salat/salat/wiki/SupportedTypes 

https://github.com/salat/salat/wiki/SupportedTypes

參見:https://github.com/noahlz/salat-examples/blob/master/src/main/scala/com/novus/salat/examples/SalatExamples.scala