I have a case class as :
case class Cart(cart_id :UUID, cart_entries :Map[String,CartEntry]){
//override def toString: String = "Saini CartId = " + cart_id.toString
}
I am using net.liftweb.json._ to convert this into json :
implicit val formats = Serialization.formats(NoTypeHints) + new UUIDserializer
val json = write(cart)
println(json)
the Json comes out as
{"cart_id":"68eb787f-746c-4320-9ef4-8b5c7f0d7e21","cart_entries":[{"_1":"ABC","_2":{"sku_id":"ABC","quantity":12,"price":213.0}}]}
While converting back my json to a cart object :
val jValue = read[Cart](json)
I am getting this exception .
[ERROR] [12/22/2016 17:11:23.784] [System-akka.actor.default-dispatcher-3] [akka://System/user/68eb787f-746c-4320-9ef4-8b5c7f0d7e21] No usable value for cart_entries No constructor for type interface scala.collection.mutable.Map, JArray(List(JObject(List(JField(_1,JString(ABC)), JField(_2,JObject(List(JField(sku_id,JString(ABC)), JField(quantity,JInt(12)), JField(price,JDouble(213.0))))))))) net.liftweb.json.MappingException: No usable value for cart_entries No constructor for type interface scala.collection.mutable.Map, JArray(List(JObject(List(JField(_1,JString(ABC)), JField(_2,JObject(List(JField(sku_id,JString(ABC)), JField(quantity,JInt(12)), JField(price,JDouble(213.0))))))))) at net.liftweb.json.Meta$.fail(Meta.scala:191) at net.liftweb.json.Extraction$.mkValue$1(Extraction.scala:357) at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$$build$1(Extraction.scala:317) at net.liftweb.json.Extraction$$anonfun$14.apply(Extraction.scala:253) at net.liftweb.json.Extraction$$anonfun$14.apply(Extraction.scala:253) at scala.collection.immutable.List.map(List.scala:277) at net.liftweb.json.Extraction$.instantiate$1(Extraction.scala:253) at net.liftweb.json.Extraction$.newInstance$1(Extraction.scala:286) at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$$build$1(Extraction.scala:315) at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$$extract0(Extraction.scala:366) at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$$extract0(Extraction.scala:199) at net.liftweb.json.Extraction$.extract(Extraction.scala:43) at net.liftweb.json.JsonAST$JValue.extract(JsonAST.scala:312) at net.liftweb.json.Serialization$.read(Serialization.scala:58) at What should i do ?
Your cart_entries field is defined as scala.collection.mutable.Map
. Lift's JSON serialization knows how to serialize a Map (mutable or immutable), but it doesn't know how to deserialize into a mutable Map. To fix it, either define your map to be scala.collection.immutable.Map
or add TypeHints
to your formats to support the mutable type.