Skip to content
This repository

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

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

4 participants

Chris Done Bas van Dijk Bryan O'Sullivan Janne Hellsten
Chris Done

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.

Janne Hellsten
nurpax commented June 05, 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.

Bryan O'Sullivan
Owner
bos commented June 15, 2012

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

Bas van Dijk basvandijk referenced this issue from a commit April 22, 2013
Bas van Dijk 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
Bryan O'Sullivan bos closed this September 04, 2013
Bryan O'Sullivan
Owner

Won't fix, sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.