Looks like Play Json Library is unable to serialize Scala Collections. Is there any alternative. I just need to dump the data from a map.
import scala.concurrent._
import play.api.libs.ws._
import scala.concurrent.ExecutionContext.Implicits.global
import play.libs.Json
object temp {
// Correct Serialization
val javaMap = new java.util.HashMap[String, String]()
javaMap.put("Abc", "Def")
// Outputs: res1: String = {"Abc":"Def"}
Json.stringify(Json.toJson(javaMap))
// Incorrect Serialization
val scalaMap = Map("Abc" -> "Def") //> scalaMap : scala.collection.immutable.Map[String,String] = Map(Abc -> Def)
// Output: res2: String = {"empty":false,"traversableAgain":true}
Json.stringify(Json.toJson(scalaMap)) //> res2: String = {"empty":false,"traversableAgain":true}
}
You use the JSValue class to convert basic values, such as numbers and strings, between JavaScript and Objective-C or Swift representations to pass data between native code and JavaScript code.
The Play JSON API provides implicit Writes for most basic types, such as Int , Double , String , and Boolean . It also supports Writes for collections of any type T that a Writes[T] exists. import play. json.
You're importing the wrong JSON library.
import play.api.libs.json._
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With