Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Make Data.Aeson.Generic encode Maybe the same way as Data.Aeson.Encode #84

Closed
singpolyma opened this Issue Aug 19, 2012 · 3 comments

Comments

Projects
None yet
3 participants

I want to use the generics encoder to encode my custom records as JSON, but I have Maybe fields, and the current implementation encodes those as "Nothing" and {"Just": 12} instead of as null and 12. Numbers and booleans are both special cased in the Generic encoder, so it seems like this should be possible.

I'm new to the whole Generics thing, so this may not be the best way, but it works:

diff --git a/Data/Aeson/Generic.hs b/Data/Aeson/Generic.hs
index 0c312b0..cad2329 100644
--- a/Data/Aeson/Generic.hs
+++ b/Data/Aeson/Generic.hs
@@ -87,6 +87,7 @@ toJSON = toJSON_generic
          `ext1Q` list
          `ext1Q` vector
          `ext1Q` set
+         `ext1Q` maybeData
          `ext2Q'` mapAny
          `ext2Q'` hashMapAny
          -- Use the standard encoding for all base types.
@@ -122,6 +123,8 @@ toJSON = toJSON_generic
     list xs = Array . V.fromList . map toJSON $ xs
     vector v = Array . V.map toJSON $ v
     set s = Array . V.fromList . map toJSON . Set.toList $ s
+    maybeData (Just m) = toJSON m
+    maybeData Nothing = T.toJSON (Nothing :: Maybe ())

     mapAny m
       | tyrep == typeOf DT.empty = remap id

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. Your code is correct as far as it goes, but it doesn't handle the decoding side, and we've deprecated this module anyway because it's a pain to maintain.

@bos bos closed this Sep 4, 2013

Is there an alternative for easily parsing JSON into Haskell data types, including Maybe?

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