mongodbscalasalat

Does salat support de/serialization of type Either in scala?


I have the following case classes:

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

As far as I can see, serialization (writing to MongoDB) goes fine, but it fails in deserializing:

com.novus.salat.util.ToObjectGlitch: 

  argument type mismatch

  $anon$2(class catalog.DataEntity @ com.novus.salat.global.package$$anon$1@3fa48431) 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" ...
------------------------------------------------------------

The output above is manually matched to the simplified case classes above. I am using salatDAO for reading and writing to DB.


Solution

  • Either is not currently supported. I've updated the "Supported Types" wiki accordingly.

    Note that in Salat 1.10.0-SNAPSHOT, you will get an error like the following (when attempting to deserialize from JSON strings):

    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

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