incorrect "fromJSON" parsing of Data.Set.Set #107

Closed
mf59816 opened this Issue Jan 4, 2013 · 6 comments

Comments

Projects
None yet
4 participants

mf59816 commented Jan 4, 2013

sets are rendered as lists, but not parsed back into sets. this may be related to #101, #102. i just pulled the head, but problem persists. to reproduce:

{-# LANGUAGE DeriveDataTypeable          #-}

import Control.Applicative ((<$>))
import Data.Aeson.Generic as A
import Data.Aeson.Parser (value)
import Data.Aeson.Types
import Data.Data
import Data.Set
import Data.Typeable

import Data.Attoparsec.Lazy as Parser

data Document =
    Document
      { fromDocument :: Set String
      }
  deriving (Eq, Ord, Show, Read, Data, Typeable)

x1 = A.encode $ Document empty
y1 = A.decode x1 :: Maybe Document

y2 :: Parser.Result (Data.Aeson.Types.Result Document)
y2 = fmap A.fromJSON $ Parser.parse value x1
*Main> x1
Chunk "{\"fromDocument\":[]}" Empty
*Main> y1
Nothing
*Main> y2
Done Empty Error "Data.Aeson.Generic.parseJSON: NoRep(DataType {tycon = \"Data.Set.Set\", datarep = NoRep})"
Owner

bos commented Jan 15, 2013

This looks like a job for @basvandijk! :-)

Contributor

sol commented Mar 22, 2013

This bug only occurs with GHC 7.6.1/7.6.2, 7.4.2 works fine. (edit: I just realized that I had containers-0.5.2.1 installed on GHC 7.4.2).

Here is a minimal way to reproduce it:

import Data.Set
import Data.Aeson.Generic

-- expected: Just (fromList [23, 42])
-- actual: Nothing
foo = decode (encode $ fromList [23 :: Int, 42]) :: Maybe (Set Int)
Contributor

sol commented Mar 23, 2013

Hmm, works with containers-0.5.2.1. Maybe a bug in the Typeable instance of Data.Set from containers-0.5.0.0, @tibbe?

Contributor

sol commented Mar 23, 2013

It works since haskell/containers@f1f58da, containers-0.5.2.0 is the first version that includes this commit.

Collaborator

basvandijk commented Apr 21, 2013

The way to fix this is to give Data.Aeson.Generic.parseJSON an extension for parsing Sets just like it has extensions for parsing lists, Vectors, Maps, HashMaps, etc. It appears this is done like:

parseJSON :: (Data a) => Value -> Parser a
parseJSON j = parseJSON_generic j
              ...
              `ext1R` setAny
              ...
  where
    vector :: (Data a) => Parser (V.Vector a)
    vector = case j of
               Array js -> V.mapM parseJSON js
               _        -> myFail

    setAny :: Parser (Set.Set a)
    setAny = fmap (Set.fromList . V.toList) vector

The problem is that this gives:

Could not deduce (Ord a1) arising from a use of `Set.fromList'

Other than giving parseJSON an (Ord a) constraint which we shouldn't do, I have no idea how to fix this.

basvandijk added a commit that referenced this issue Apr 22, 2013

Deprecated Data.Aeson.Generic
The module is deprecated for the following reasons:

- It has issues: #75, #84, #101, #107.

- It's slow compared to GHC Generics and very, very, very slow
  compared to TH. See some recent benchmark results in #116.

- The encoding/decoding can't currently be configured using the
  encoding Options.

- Related to the previous point: GHC Generics and TH use compatible
  encoders/decoders (if the same Options have been
  used). Data.Aeson.Generic does things slightly differently.

- The original author is not around to work on it.
Owner

bos commented Sep 4, 2013

Won't fix, sorry.

@bos bos closed this Sep 4, 2013

tolysz pushed a commit to tolysz/aeson that referenced this issue May 18, 2015

Deprecated Data.Aeson.Generic
The module is deprecated for the following reasons:

- It has issues: #75, #84, #101, #107.

- It's slow compared to GHC Generics and very, very, very slow
  compared to TH. See some recent benchmark results in #116.

- The encoding/decoding can't currently be configured using the
  encoding Options.

- Related to the previous point: GHC Generics and TH use compatible
  encoders/decoders (if the same Options have been
  used). Data.Aeson.Generic does things slightly differently.

- The original author is not around to work on it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment