forked from haskell/aeson
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start working on release notes and expanded examples.
- Loading branch information
Showing
6 changed files
with
153 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
-- This example is basically the same as in Simplest.hs, only it uses | ||
-- GHC's builtin generics instead of explicit instances of ToJSON and | ||
-- FromJSON. | ||
|
||
-- This example only works with GHC 7.2 or newer, as it uses the | ||
-- datatype-generic programming machinery introduced in 7.2. | ||
|
||
-- We enable the DeriveGeneric language extension so that GHC can | ||
-- automatically derive the Generic class for us. | ||
|
||
{-# LANGUAGE DeriveGeneric #-} | ||
|
||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
import Data.Aeson (FromJSON, ToJSON, decode, encode) | ||
import qualified Data.ByteString.Lazy.Char8 as BL | ||
|
||
-- To decode or encode a value using the generic machinery, we must | ||
-- make the type an instance of the Generic class. | ||
|
||
import GHC.Generics (Generic) | ||
|
||
data Coord = Coord { x :: Double, y :: Double } | ||
deriving (Show, Generic) | ||
|
||
-- While we still have to declare our type as instances of FromJSON | ||
-- and ToJSON, we do *not* need to provide bodies for the instances. | ||
-- Default versions will be supplied for us. | ||
|
||
instance FromJSON Coord | ||
instance ToJSON Coord | ||
|
||
main ::IO () | ||
main = do | ||
let req = decode "{\"x\":3.0,\"y\":-1.0}" :: Maybe Coord | ||
print req | ||
let reply = Coord 123.4 20 | ||
BL.putStrLn (encode reply) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
-- This example is basically the same as in Simplest.hs, only it uses | ||
-- SYB generics instead of explicit instances of ToJSON and FromJSON. | ||
|
||
-- This mechanism is much slower than the newer generics mechanism | ||
-- demonstrated in Generic.hs, but it works on versions of GHC older | ||
-- than 7.2. | ||
|
||
-- 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 | ||
|
||
-- To decode or encode a value using the generic machinery, we must | ||
-- make the type an instance of the Typeable and Data classes. | ||
|
||
data Coord = Coord { x :: Double, y :: Double } | ||
deriving (Show, Typeable, Data) | ||
|
||
main ::IO () | ||
main = do | ||
let req = decode "{\"x\":3.0,\"y\":-1.0}" :: Maybe Coord | ||
print req | ||
let reply = Coord 123.4 20 | ||
BL.putStrLn (encode reply) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,30 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
import Control.Applicative ((<$>), (<*>)) | ||
import Control.Monad (mzero) | ||
import Control.Applicative ((<$>), (<*>), empty) | ||
import Data.Aeson | ||
import qualified Data.Aeson.Types as T | ||
import Data.Attoparsec (parse, Result(..)) | ||
import qualified Data.ByteString.Char8 as BS | ||
import qualified Data.ByteString.Lazy.Char8 as BSL | ||
import qualified Data.ByteString.Lazy.Char8 as BL | ||
|
||
main ::IO () | ||
main = do | ||
print $ parseFromString "{\"x\":3.0,\"y\":-1.0}" | ||
let reply = Coord 123.4 20 | ||
putStrLn $ BSL.unpack (encode reply) | ||
data Coord = Coord { x :: Double, y :: Double } | ||
deriving (Show) | ||
|
||
data Coord = Coord { x :: Double, y :: Double } deriving (Show) | ||
-- A ToJSON instance allows us to encode a value as JSON. | ||
|
||
instance ToJSON Coord where | ||
toJSON (Coord xV yV) = object ["x" .= xV, "y" .= yV] | ||
toJSON (Coord xV yV) = object [ "x" .= xV, | ||
"y" .= yV ] | ||
|
||
-- A FromJSON instance allows us to decode a value from JSON. This | ||
-- should match the format used by the ToJSON instance. | ||
|
||
instance FromJSON Coord where | ||
parseJSON (Object v) = Coord <$> | ||
v .: "x" <*> | ||
v .: "y" | ||
parseJSON _ = mzero | ||
v .: "x" <*> | ||
v .: "y" | ||
parseJSON _ = empty | ||
|
||
parseFromString :: String -> Maybe Coord | ||
parseFromString s = | ||
let bs = BS.pack s | ||
in case parse json bs of | ||
Done _rest res -> T.parseMaybe parseJSON res | ||
_ -> Nothing | ||
main ::IO () | ||
main = do | ||
let req = decode "{\"x\":3.0,\"y\":-1.0}" :: Maybe Coord | ||
print req | ||
let reply = Coord 123.4 20 | ||
BL.putStrLn (encode reply) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# 0.3 to 0.4 | ||
|
||
## Generics support | ||
|
||
Aeson's support for data-type generic programming makes it possible to | ||
use JSON encodings of most data types without writing any boilerplate | ||
instances. | ||
|
||
Thanks to Bas Van Dijk, aeson now supports the two major schemes for | ||
doing datatype-generic programming: | ||
|
||
* the newer mechanism, [built into GHC | ||
itself](http://www.haskell.org/ghc/docs/latest/html/users_guide/generic-programming.html) | ||
|
||
* the older mechanism, based on SYB (for "scrap your boilerplate") | ||
|
||
The GHC-based generics are fast and terse: in fact, they're generally | ||
comparable in performance to hand-written `ToJSON` and `FromJSON` | ||
instances. To see how to use GHC generics, see | ||
[`examples/Generic.hs`](https://github.com/bos/aeson/blob/master/examples/Generic.hs). | ||
|
||
The SYB-based generics support lives in | ||
[Data.Aeson.Generic](http://hackage.haskell.org/packages/archive/aeson/0.4.0.0/doc/html/Data-Aeson-Generic.html), | ||
and is provided mainly for users of GHC older than 7.2. It's far | ||
slower (by about 10x) than the more modern generic mechanism. To see | ||
how to use SYB generics, see | ||
[`examples/GenericSYB.hs`](https://github.com/bos/aeson/blob/master/examples/GenericSYB.hs). |