-
-
Save michaelt/5568429 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{-#LANGUAGE OverloadedStrings#-} | |
import Data.Aeson.Types | |
import Data.Aeson | |
import qualified Data.Vector as V | |
import qualified Data.ByteString.Lazy as L | |
import Data.HashMap.Strict | |
import qualified Data.Text as T | |
-- this is backward maybe -- also doesn't crack open the Values | |
data Invoice = Invoice (V.Vector Value) | Invoices (HashMap T.Text Value) | |
parseInvoices :: L.ByteString -> Either String Invoice | |
parseInvoices s = case parseObjects s of | |
Left err -> Left err | |
Right (Left obj) -> Right (Invoices obj) | |
Right (Right hmap) -> Right (Invoice hmap) | |
where | |
parseObjects :: L.ByteString -> Either String (Either Object Array) | |
parseObjects s = do | |
result <- eitherDecode s | |
flip parseEither result $ \obj -> do | |
d <- (obj .: "invoices") >>= (.: "invoice") | |
case d of | |
Object v -> return (Left v) | |
Array v -> return (Right v) | |
-- other constructors? | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment