[Haskell-cafe] aeson: instance FromJSON IntMap

Justin Bailey jgbailey at gmail.com
Wed Mar 3 19:06:37 UTC 2021


I recently ran into a similar issue, where the `parseJason` instance
for a map with custom keys (not Text but a sum type) was expected to
be a JSON array rather than an object.

I just ended up writing my own instance, assuming the built-in one
would be too hard to change (breaking backwards compatibility).

On Wed, Mar 3, 2021 at 8:17 AM Oleg Grenrus <oleg.grenrus at iki.fi> wrote:
>
> History digging points to a commit from 2013:
> https://github.com/haskell/aeson/commit/260a7e839bfda660bb7e0b715567b35d76cc4693
> I'm too lazy to dig further, but I'd expect that the answer "that
> instance have always been that way".
>
> Back then there weren't machinery to parse keys of maps (we had e.g.
> `FromJSON (Map Text a)`). Now we have. I'm open to change (and have
> other small-ish yet breaking changes in mind for aeson), please open an
> issue, so we can discuss it there.
>
> Cheers, Oleg
>
> On 3.3.2021 17.27, Olaf Klinke wrote:
> > Dear Cafe,
> >
> > why is Aeson's instance FromJSON IntMap so different from the instance
> > of Map? Apparently the Map instance expects an Object (the dictionary)
> > while the IntMap instance [1] is implemented using IntMap.fromList, so
> > it expects an array.
> > I can of course decode the JSON as (Map Int) and then use
> > IntMap.fromAscList . Map.assocs
> > but how efficient is that? And it also relies on Map.assocs producing
> > the list in the order expected by fromAscList which (unlikely) could be
> > broken in a future version of containers.
> >
> > Thanks
> > Olaf
> >
> > [1]
> > https://hackage.haskell.org/package/aeson-1.5.6.0/docs/src/Data.Aeson.Types.FromJSON.html#line-1891
> >
> > _______________________________________________
> > Haskell-Cafe mailing list
> > To (un)subscribe, modify options or view archives go to:
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> > Only members subscribed via the mailman list are allowed to post.
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.


More information about the Haskell-Cafe mailing list