Data.Aeson.Generic.decode unable to parse Data.Aeson.Generic.encode output (aeson-0.6.0.0) #75

Closed
chrisdone opened this Issue Mar 29, 2012 · 3 comments

Projects

None yet

4 participants

@chrisdone

The following is OK:

data Foo = Foo deriving (Typeable,Data,Show)

λ> encode Foo
Chunk "[]" Empty
λ> decode (encode Foo) :: Maybe Foo
Just Foo

But there is a problem for more than one nullary constructor:

data Foo = Foo | Bar deriving (Typeable,Data,Show)

λ> encode Foo
Chunk "\"Foo\"" Empty    
λ> decode (encode Foo) :: Maybe Foo
Nothing

I didn't yet look at the code, but thought I'd make a note of it somewhere, as it's kind of a glaring issue.

@nurpax
nurpax commented Jun 5, 2012

I think I was able to reproduce this with Maybe types:

-- We enable the DeriveDataTypeable language extension so that GHC can
-- automatically derive the Typeable and Data classes for us.

{-# LANGUAGE DeriveDataTypeable #-}

{-# LANGUAGE OverloadedStrings #-}

import Data.Data (Typeable, Data)
import Data.Aeson.Generic (decode, encode)
import qualified Data.ByteString.Lazy.Char8 as BL

data ListenMask =
  ListenMask {
    lmChannel :: String
  , lmChannel2 :: Maybe String
  } deriving (Show, Eq, Data, Typeable)

main :: IO ()
main = do
  let req1 = decode "[{\"lmChannel\":\"#s\", \"lmChannel2\":\"Nothing\" }]" :: Maybe [ListenMask]
  let req2 = decode "[{\"lmChannel\":\"#s\", \"lmChannel2\":{\"Just\":\"foo\"}}]" :: Maybe [ListenMask]
  print req1
  print req2
  let reply1 = ListenMask "#s" (Nothing)
  let reply2 = ListenMask "#s" (Just "foo")
  BL.putStrLn (encode reply1)
  BL.putStrLn (encode reply2)

this prints

Nothing
Just [ListenMask {lmChannel = "#s", lmChannel2 = Just "foo"}]
{"lmChannel2":"Nothing","lmChannel":"#s"}
{"lmChannel2":{"Just":"foo"},"lmChannel":"#s"}

when I expected the first line of output to be successfully decoded. Encoded output certainly matches req1.

Spent quite a while debugging this - didn't get any sort of a type error or anything. I'd expect many people to use Generic decode/encode with Aeson since that is the one that needs a lot more typing compared to the HP Text.Json package.

@basvandijk basvandijk was assigned Jun 16, 2012
@bos
Owner
bos commented Jun 16, 2012

This looks similar to #72 to me. @basvandijk ?

@basvandijk basvandijk added a commit that referenced this issue Apr 22, 2013
@basvandijk basvandijk 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.
12ed274
@bos
Owner
bos commented Sep 4, 2013

Won't fix, sorry.

@bos bos closed this Sep 4, 2013
@tolysz tolysz pushed a commit to tolysz/aeson that referenced this issue May 18, 2015
@basvandijk basvandijk 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.
e14c983
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment