Skip to content
This repository has been archived by the owner on May 21, 2018. It is now read-only.

Map extractor typeclass for JSON is missing #41

Closed
propensive opened this issue Jan 6, 2016 · 5 comments
Closed

Map extractor typeclass for JSON is missing #41

propensive opened this issue Jan 6, 2016 · 5 comments
Assignees

Comments

@propensive
Copy link
Owner

This code,

json"{}".as[Map[String, String]]

does not compile, because the typeclass instance Extractor[Map[String, String], Json] can't be found. An implementation exists in rapture.data, but it doesn't get resolved.

@sscarduzio
Copy link
Contributor

You're right: the extractor was there, but not resolved. I also eventually made it work defining the implicit manually:

import rapture.json._
import rapture.json.jsonBackends.argonaut._
implicit def me: Extractor[Map[String, Json], Json] = GeneralExtractors.mapExtractor
json"""{"bool" : true, "int": 3, "string": "bam", "float":0.33, "obj": {"x": 1} }""".as[Map[String, Json]]

res0: scala.collection.immutable.Map[String,rapture.json.Json] = Map(float -> 0.33, obj -> {"x":1}, string -> "bam", bool -> true, int -> 3)

I noticed that my implicit def does not work if I omit the type declaration:

implicit def me = GeneralExtractors.mapExtractor  // won't resolve!

Seems like the return type of the mapExtractor [T, Data <: DataType[Data, _ <: DataAst ]] is not spontaneously matching the required Extractor[Map[String, Json], Json]. But it does match if I provide some guidance to the compiler, explicitly annotating the return type.

Is this one of those type-inference fuck-up bug you talk about at conferences? :)

One more lead: IntelliJ would infer the type: Extractor[Map[String, Map[String, Json]], Json]
Which obviously won't work either.

@propensive propensive self-assigned this Jan 11, 2016
@propensive propensive added this to the 2.0.0-M4 Toltec milestone Jan 14, 2016
@propensive
Copy link
Owner Author

Sorry @sscarduzio - I only just saw your response!

Thanks for the fix! I've seen that sort of type inference issue several times before, though I don't think it's a bug. In your example me is not polymorphic, so it won't "inherit" the polymorphism of mapExtractor (i.e. its two type parameters). I think this is actually a case of type-inference working quite well! It's using the return type (i.e. the explicit type you provide) to infer the type parameters of the invocation of mapExtractor! :)

@propensive
Copy link
Owner Author

I've just fixed this, and added another nice feature:

If your JSON object keys are all some non-string type, e.g. integers:

val j = json"""{ "1": true, "2": false }"""

and a string parser for those types exists in Rapture, then it's possible to extract directly into that kind of map, i.e.

j.as[Map[Int, Boolean]]

@sscarduzio
Copy link
Contributor

Thanks Jon for the explanation (makes sense now). Although I made it work
playing Jenga with types, it's nice to have a proper fix :)

On Fri, Jan 29, 2016 at 12:25 AM, Jon Pretty notifications@github.com
wrote:

Closed #41 #41.


Reply to this email directly or view it on GitHub
#41 (comment).

@propensive
Copy link
Owner Author

Yep, it should never have gone away!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants