Description
This might be a documentation issue. The documentation for ObjectWithSingleField says:
A constructor will be encoded to an object with a single field named after the constructor tag (modified by the constructorTagModifier) which maps to the encoded contents of the constructor.
Here's my test program:
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE CPP #-}
module Main where
import Language.Haskell.Exts.Annotated
import Data.Aeson
import Data.Aeson.TH
$(deriveToJSON defaultOptions { sumEncoding = ObjectWithSingleField } ''ExportSpecList)
$(deriveToJSON defaultOptions { sumEncoding = ObjectWithSingleField } ''SrcSpanInfo)
$(deriveToJSON defaultOptions { sumEncoding = ObjectWithSingleField } ''SrcSpan)
Running ghc -ddump-splices --make Main.hs gives (1/3):
Main.hs:1:1: Splicing declarations
deriveToJSON
(defaultOptions {sumEncoding = ObjectWithSingleField}) ''SrcSpan
======>
Main.hs:25:3-79
instance ToJSON SrcSpan where
toJSON
= \ value_a81J
-> case value_a81J of {
SrcSpan arg1_a81K arg2_a81L arg3_a81M arg4_a81N arg5_a81O
-> object
[((Data.Text.pack "srcSpanFilename") .= arg1_a81K),
((Data.Text.pack "srcSpanStartLine") .= arg2_a81L),
((Data.Text.pack "srcSpanStartColumn") .= arg3_a81M),
((Data.Text.pack "srcSpanEndLine") .= arg4_a81N),
((Data.Text.pack "srcSpanEndColumn") .= arg5_a81O)] }
An object, and a set of values that are of primitive types so no need to toJSON them. Ok, SrcSpanInfo next:
Main.hs:1:1: Splicing declarations
deriveToJSON
(defaultOptions {sumEncoding = ObjectWithSingleField})
''SrcSpanInfo
======>
Main.hs:24:3-83
instance ToJSON SrcSpanInfo where
toJSON
= \ value_a80U
-> case value_a80U of {
SrcSpanInfo arg1_a80V arg2_a80W
-> object
[((Data.Text.pack "srcInfoSpan") .= arg1_a80V),
((Data.Text.pack "srcInfoPoints") .= arg2_a80W)] }
An object, and a set of values that are not toJSON'd. The values are of type SrcSpan and [SrcSpan]. Ok, ExportSpecList next:
Main.hs:1:1: Splicing declarations
deriveToJSON
(defaultOptions {sumEncoding = ObjectWithSingleField})
''ExportSpecList
======>
Main.hs:12:3-86
instance ToJSON l_a6O3 => ToJSON (ExportSpecList l_a6O3) where
toJSON
= \ value_a7Us
-> case value_a7Us of {
ExportSpecList arg1_a7Ut arg2_a7Uu
-> Array
(Data.Vector.create
(do { mv_a7Uv <- Data.Vector.Mutable.unsafeNew 2;
Data.Vector.Mutable.unsafeWrite mv_a7Uv 0 (toJSON arg1_a7Ut);
Data.Vector.Mutable.unsafeWrite mv_a7Uv 1 (toJSON arg2_a7Uu);
return mv_a7Uv })) }
An array and a set of values that are toJSON'd.
Here are the datatypes cut and pasted from the Source-link on Hackage:
-- | A portion of the source, spanning one or more lines and zero or more columns.
data SrcSpan = SrcSpan
{ srcSpanFilename :: String
, srcSpanStartLine :: Int
, srcSpanStartColumn :: Int
, srcSpanEndLine :: Int
, srcSpanEndColumn :: Int
}
deriving (Eq,Ord,Show,Typeable,Data)
data SrcSpanInfo = SrcSpanInfo
{ srcInfoSpan :: SrcSpan
-- , explLayout :: Bool
, srcInfoPoints :: [SrcSpan] -- Marks the location of specific entities inside the span
}
deriving (Eq,Ord,Show,Typeable,Data)
-- | An explicit export specification.
data ExportSpecList l
= ExportSpecList l [ExportSpec l]
deriving (Eq,Ord,Show,Typeable,Data,Foldable,Traversable,Functor,Generic)
I might be doing unsupported things, but in that case it would be nice to get a warning that I can't expect to deserialize my values in a regular way on the receiving end.
I'm using ghc 7.6.3, haskell-src-exts 1.15.0.1, and aeson 0.7.0.6 if that makes any difference.