Cannot convert from Map[_, Any]
See original GitHub issueWhile trying out the library, I have realized that there are no implicit conversions for Any or AnyVal.
For example, consider the following code:
case class Container(m: Map[String, AnyVal])
val container = Container(Map("name" -> "John".asInstanceOf[AnyVal], "age" -> 50))
container.asJson
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.
Issue Analytics
- State:
- Created 8 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Cannot convert object to map of any single type - Terraform
I suppose because tomap of an empty object . I know that removing empty “b” object completely will avoid this error but for...
Read more >Type mismatch: cannot convert from Map<Object,List ...
The following code does the same thing as your code but splits up the collect in to multiple map steps, this may work...
Read more >cannot convert HashMap to class exception
I get the following exception when trying to deserialize a map java.lang.ClassCastException: Cannot convert class java.util.HashMap to class ...
Read more >Type mismatch: cannot convert from Object to Map<String ...
This line gives me an error: private static final Map<String, IndexType> INDEX_TYPE_MAP = Arrays ...
Read more >Type mismatch: cannot convert from element type Object to ...
Type mismatch: cannot convert from element type Object to Map. ... Any good reason to instantiate HashMap<Object, Object>?
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@robsonpeixoto The
Either
constructors are case classes, sogeneric
will produce instances for them just like any other case classes (there are no automatically-providedEither
instances, and it’s not special-cased).If you want an untagged representation, you can do something like this:
And then:
Does that work for you?
@ChetanBhasin circe is built on the idea of having types drive serialization—you ask for a JSON representation of a value of type
Map[String, User]
, for example, and the appropriate encoders are selected at compile-time—there’s no cost or danger of reflection at runtime. circe is entirely helpless when it comes toAny
orAnyVal
, where you can’t do anything reasonable with a value except reflect on it at runtime (if that counts as reasonable).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
Map[String, Any]
is the opposite of type-safe functional programming, and circe aims to make it both possible and desirable to avoid having types likeMap[String, Any]
show up anywhere in your program.Of course you could write your own
Encoder
andDecoder
instances for types likeMap[String, Any]
, but they’d be unsafe, unidiomatic, and possibly less performant (because of the necessity of runtime reflection), so circe itself will almost definitely never provide anything like that off the shelf.I’d recommend trying to avoid
Map[String, Any]
entirely, but if you’re stuck with it and you really want to use circe (as opposed to a JSON library that embraces runtime reflection, which is most of them 😄), you could try to cast and convert yourMap[String, Any]
values into something more type-full, like e.g.Map[String, Either[Int, String]]
, which circe will happily encode and decode.