Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add json examples

  • Loading branch information...
commit bc716cedc2c13f41bc5a8953df7b6bed9afd2a42 1 parent a1f389c
@gregorycollins gregorycollins authored
Showing with 37 additions and 0 deletions.
  1. +37 −0 examples/JsonExample.hs
View
37 examples/JsonExample.hs
@@ -0,0 +1,37 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+import Control.Applicative
+import Data.Aeson
+import Data.Attoparsec (parseOnly)
+import Data.ByteString.Char8 (ByteString)
+import qualified Data.ByteString.Char8 as S
+import qualified Data.ByteString.Lazy.Char8 as L
+
+
+------------------------------------------------------------------------------
+example1 :: ByteString -> Either String Coord
+example1 bs = parseOnly json bs >>= convert
+ where
+ convert value = case fromJSON value of
+ (Error e) -> Left e
+ (Success a) -> Right a
+
+example2 :: Coord -> ByteString
+example2 c = S.concat $ L.toChunks $ encode c
+
+
+------------------------------------------------------------------------------
+data Coord = Coord { _x :: Double, _y :: Double }
+ deriving (Show, Eq)
+
+instance ToJSON Coord where
+ toJSON (Coord x y) = object ["x" .= x, "y" .= y]
+
+
+instance FromJSON Coord where
+ parseJSON (Object v) = Coord <$>
+ v .: "x" <*>
+ v .: "y"
+
+ -- A non-Object value is of the wrong type, so use mzero to fail.
+ parseJSON _ = empty
Please sign in to comment.
Something went wrong with that request. Please try again.