Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

34 lines (27 sloc) 0.961 kb
{-# LANGUAGE OverloadedStrings #-}
import Control.Applicative ((<$>), (<*>))
import Control.Monad (mzero)
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
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)
instance ToJSON Coord where
toJSON (Coord xV yV) = object ["x" .= xV, "y" .= yV]
instance FromJSON Coord where
parseJSON (Object v) = Coord <$>
v .: "x" <*>
v .: "y"
parseJSON _ = mzero
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
Jump to Line
Something went wrong with that request. Please try again.