Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
24
Task/JSON/Haskell/json-1.hs
Normal file
24
Task/JSON/Haskell/json-1.hs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
{-# LANGUAGE OverloadedStrings #-}
|
||||
|
||||
import Data.Aeson
|
||||
import Data.Attoparsec (parseOnly)
|
||||
import Data.Text
|
||||
import qualified Data.ByteString.Lazy.Char8 as B
|
||||
import qualified Data.ByteString.Char8 as S
|
||||
|
||||
testdoc = object [
|
||||
"foo" .= (1 :: Int),
|
||||
"bar" .= ([1.3, 1.6, 1.9] :: [Double]),
|
||||
"baz" .= ("some string" :: Text),
|
||||
"other" .= object [
|
||||
"yes" .= ("sir" :: Text)
|
||||
]
|
||||
]
|
||||
|
||||
main = do
|
||||
let out = encode testdoc
|
||||
B.putStrLn out
|
||||
case parseOnly json (S.concat $ B.toChunks out) of
|
||||
Left e -> error $ "strange error re-parsing json: " ++ (show e)
|
||||
Right v | v /= testdoc -> error "documents not equal!"
|
||||
Right v | otherwise -> print v
|
||||
16
Task/JSON/Haskell/json-2.hs
Normal file
16
Task/JSON/Haskell/json-2.hs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
{-# LANGUAGE TemplateHaskell, OverloadedStrings #-}
|
||||
import Data.Aeson
|
||||
import Data.Aeson.TH
|
||||
|
||||
data Person = Person { firstName :: String
|
||||
, lastName :: String
|
||||
, age :: Maybe Int
|
||||
} deriving (Show, Eq)
|
||||
|
||||
$(deriveJSON defaultOptions ''Person)
|
||||
|
||||
main = do
|
||||
let test1 = "{\"firstName\":\"Bob\", \"lastName\":\"Smith\"}"
|
||||
test2 = "{\"firstName\":\"Miles\", \"lastName\":\"Davis\", \"age\": 45}"
|
||||
print (decode test1 :: Maybe Person)
|
||||
print (decode test2 :: Maybe Person)
|
||||
17
Task/JSON/Haskell/json-3.hs
Normal file
17
Task/JSON/Haskell/json-3.hs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
{-# LANGUAGE DeriveGeneric, OverloadedStrings #-}
|
||||
import Data.Aeson
|
||||
import GHC.Generics
|
||||
|
||||
data Person = Person { firstName :: String
|
||||
, lastName :: String
|
||||
, age :: Maybe Int
|
||||
} deriving (Show, Eq, Generic)
|
||||
|
||||
instance FromJSON Person
|
||||
instance ToJSON Person
|
||||
|
||||
main = do
|
||||
let test1 = "{\"firstName\":\"Bob\", \"lastName\":\"Smith\"}"
|
||||
test2 = "{\"firstName\":\"Miles\", \"lastName\":\"Davis\", \"age\": 45}"
|
||||
print (decode test1 :: Maybe Person)
|
||||
print (decode test2 :: Maybe Person)
|
||||
Loading…
Add table
Add a link
Reference in a new issue