Skip to content

Instantly share code, notes, and snippets.

@michaelt michaelt/gist:5568429 forked from reite/gist:5568152
Last active Dec 17, 2015

What would you like to do?
{-#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)
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
You can’t perform that action at this time.