Skip to content

Instantly share code, notes, and snippets.

@michaelt
Forked from reite/gist:5568152
Last active December 17, 2015 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelt/5568429 to your computer and use it in GitHub Desktop.
Save michaelt/5568429 to your computer and use it in GitHub Desktop.
{-#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