amnaredo / test

0 stars 0 forks source link

Could not find implicit value for evidence parameter / Not enough arguments for method write #228

Open amnaredo opened 3 years ago

amnaredo commented 3 years ago

This simple mixed-content Map fails during compilation:

write(Map(
            "foo" -> "bar",
            "message" -> Map("hello" -> "world")
        ))

With this error:

Error:(21, 16) could not find implicit value for evidence parameter of type upickle.default.Writer[scala.collection.immutable.Map[String,Object]]
Error occurred in an application involving default arguments.
        val x = write(Map(

Error:(21, 16) not enough arguments for method write: (implicit evidence$4: upickle.default.Writer[scala.collection.immutable.Map[String,Object]])String.
Unspecified value parameter evidence$4.
Error occurred in an application involving default arguments.
        val x = write(Map(

ID: 231 Original Author: wiradikusuma

amnaredo commented 3 years ago

upickle does not know how to serialize Object.

I think you'll need to give a more specific type to it, for example by creating a sealed trait with subtypes that wrap your String and Map[String, String], and then give upickle a Map[String, YourSealedTreait] instead of Map[String, Object].

ETA: Of course, you will need to define serializers for the sealed trait and its subtypes as explained in upickle docs. Original Author: raquo

amnaredo commented 3 years ago

Thank you, I've read about that in the doc, but my use case is actually to serialize arbitrary maps. Do you have recommendation for that? Original Author: wiradikusuma

amnaredo commented 3 years ago

I don't think what you're looking for is possible with upickle. upickle does not usually store type information in the serialized JSON string (except for sealed traits / enums). So when deserializing, you need to know which type you're deserializing into, and have a deserializer available for that type.

If this really is a requirement, try to find a library that encodes the types in serialized strings. I'm not sure which ones do that. That seems complicated to pull off. Original Author: raquo

amnaredo commented 3 years ago

You can also construct a Js.Value yourself out of Js.Objs if you want a heterogenous JSON tree

On Thu, 17 May 2018 at 11:08 PM, Nikita Gazarov notifications@github.com wrote:

I don't think what you're looking for is possible with upickle. upickle does not usually store type information in the serialized JSON string (except for sealed traits / enums). So when deserializing, you need to know which type you're deserializing into, and have a deserializer available for that type.

If this really is a requirement, try to find a library that encodes the types in serialized strings. I'm not sure which ones do that.

— You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub https://github.com/lihaoyi/upickle/issues/231#issuecomment-390104408, or mute the thread https://github.com/notifications/unsubscribe-auth/AA5A_L8nhRTCy3u9BNCRMY-GqHf5jQSqks5tzmVYgaJpZM4UEJP2 .

Original Author: lihaoyi