bos/aeson — pull request #116 by basvandijk (2013-04-12 08:36:49+00:00)
Title: Parameterize generic encoding with options

Hi Bryan,

The associated patches add the following features:

* Users can now configure the automatic JSON encoding using the
following Options record:

```haskell
data Options = Options
{ fieldNameModifier :: String -> String
, constructorNameModifier :: String -> String
, nullaryToString :: Bool
, omitNothingFields :: Bool
, sumEncoding :: SumEncoding
}

data SumEncoding =
TwoElemArray
| ObjectWithType { typeFieldName :: String
, valueFieldName :: String
}
| ObjectWithSingleField
```

* Convenient defaults are provided:

```haskell
defaultOptions :: Options
defaultOptions = Options
{ fieldNameModifier = id
, constructorNameModifier = id
, nullaryToString = True
, omitNothingFields = True
, sumEncoding = TwoElemArray
}

defaultObjectWithType :: SumEncoding
defaultObjectWithType = ObjectWithType
{ typeFieldName = "type"
, valueFieldName = "value"
}
```
* The Options are supported in both the TH as well as the GHC Generics
encoders.

The Options are not supported in the SYB encoder but there's no
technical reason they can't be supported there to.

* The GFromJSON and GToJSON type classes are exported so that users
can specify their own encoding Options using:

```haskell
instance FromJSON MyType where
parseJSON = fmap to . gParseJSON myOptions

instance ToJSON MyType where
toJSON = gToJSON myOptions . from
```

* Some tests for the automatic JSON encoding are added.

I realize this is a lot of code to review. Let me know if you have any
questions or comments. Also, I don't mind bikeshedding over naming and
such ;-)

Cheers,

Bas