-
Notifications
You must be signed in to change notification settings - Fork 532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot convert from Map[_, Any] #216
Comments
@ChetanBhasin circe is built on the idea of having types drive serialization—you ask for a JSON representation of a value of type This is by design, and has two goals: to keep the library simple and safe by avoiding any use of runtime reflection, and to promote the use of types. In many ways Of course you could write your own I'd recommend trying to avoid |
@travisbrown Thanks for the reply. I can totally understand why we might not want to do conversions between Any and JSON value and why custom Encoders are a bad idea. However, I think, it makes sense to have a data type which deals only with JSON primitives. For example, while the only way to represent a JSON object of key-value pairs in vanilla Scala would be to use a
This approach would be type-safe. Now, my only question is weather something like already exists in the library? |
I'm sorry. I've been fiddling around with the library and understand this better. Will close the issue. |
@travisbrown I tested import io.circe._, io.circe.generic.auto._, io.circe.parser._, io.circe.syntax._
import io.circe._
import io.circe.generic.auto._
import io.circe.parser._
import io.circe.syntax._
import cats.data.Xor
type Message = Map[String, Either[Int, String]]
object Message {
def apply(elems: (String, Either[Int, String])*): Map[String, Either[Int, String]] = Map(elems: _*)
}
val m1 = Message("name" -> Right("John"), "age" -> Left(50))
println(m1.asJson)
// Exiting paste mode, now interpreting.
{
"name" : {
"Right" : {
"b" : "John"
}
},
"age" : {
"Left" : {
"a" : 50
}
}
} And when I to convert a json without this keys the decode do not work: // ...
val json: String = """
{
"id": 1,
"name": "Foo",
"city": "NYC",
"age": 10,
"height": 175
}
"""
val m2 = decode[Message](json)
// Exiting paste mode, now interpreting.
m2: cats.data.Xor[io.circe.Error,Message] = Left(DecodingFailure(CNil, List(El(DownField(id),true,false)))) Is that the expected behave? |
@robsonpeixoto The If you want an untagged representation, you can do something like this: import io.circe._, io.circe.generic.auto._, io.circe.jawn._, io.circe.syntax._
implicit val encodeIntOrString: Encoder[Either[Int, String]] =
Encoder.instance(_.fold(_.asJson, _.asJson))
implicit val decodeIntOrString: Decoder[Either[Int, String]] =
Decoder[Int].map(Left(_)).or(Decoder[String].map(Right(_))) And then: scala> println(m1.asJson)
{
"name" : "John",
"age" : 50
}
scala> decode[Message](m1.asJson.noSpaces)
res1: cats.data.Xor[io.circe.Error,Message] = Right(Map(name -> Right(John), age -> Left(50))) Does that work for you? |
Very well, @travisbrown. Thanks |
Update play-json to 2.8.1
While trying out the library, I have realized that there are no implicit conversions for Any or AnyVal.
For example, consider the following code:
This will not work because there is no implicit mapping for a AnyVal (or for Any, for that matter).
I've been trying to understand how one may write their own implicit conversion guidelines in such cases for converting from objects to JSON and vice versa.
The end goal here is something like being able to convert between any arbitrary JSON to a Map[String, Any], and vice versa.
Note: If such a functionality already exists, please point me to the right direction. I was not able to find this in the documentation or otherwise.
The text was updated successfully, but these errors were encountered: