{-# LANGUAGE CPP #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Data.Binary.Instances.Aeson where import Data.Binary (Binary, Get, get, put) import Data.Binary.Orphans () import Data.Binary.Instances.Scientific () import Data.Binary.Instances.Text () import Data.Binary.Instances.UnorderedContainers () import Data.Binary.Instances.Vector () import qualified Data.Aeson as A #if MIN_VERSION_aeson(2,0,0) import qualified Data.Aeson.Key as Key import qualified Data.Aeson.KeyMap as KM #endif instance Binary A.Value where get :: Get Value get = do Int t <- forall t. Binary t => Get t get :: Get Int case Int t of Int 0 -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b fmap Object -> Value A.Object forall t. Binary t => Get t get Int 1 -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b fmap Array -> Value A.Array forall t. Binary t => Get t get Int 2 -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b fmap Text -> Value A.String forall t. Binary t => Get t get Int 3 -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b fmap Scientific -> Value A.Number forall t. Binary t => Get t get Int 4 -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b fmap Bool -> Value A.Bool forall t. Binary t => Get t get Int 5 -> forall (m :: * -> *) a. Monad m => a -> m a return Value A.Null Int _ -> forall (m :: * -> *) a. MonadFail m => String -> m a fail forall a b. (a -> b) -> a -> b $ String "Invalid Value tag: " forall a. [a] -> [a] -> [a] ++ forall a. Show a => a -> String show Int t put :: Value -> Put put (A.Object Object v) = forall t. Binary t => t -> Put put (Int 0 :: Int) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b >> forall t. Binary t => t -> Put put Object v put (A.Array Array v) = forall t. Binary t => t -> Put put (Int 1 :: Int) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b >> forall t. Binary t => t -> Put put Array v put (A.String Text v) = forall t. Binary t => t -> Put put (Int 2 :: Int) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b >> forall t. Binary t => t -> Put put Text v put (A.Number Scientific v) = forall t. Binary t => t -> Put put (Int 3 :: Int) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b >> forall t. Binary t => t -> Put put Scientific v put (A.Bool Bool v) = forall t. Binary t => t -> Put put (Int 4 :: Int) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b >> forall t. Binary t => t -> Put put Bool v put Value A.Null = forall t. Binary t => t -> Put put (Int 5 :: Int) #if MIN_VERSION_aeson(2,0,0) instance Binary Key.Key where get :: Get Key get = Text -> Key Key.fromText forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b <$> forall t. Binary t => Get t get put :: Key -> Put put = forall t. Binary t => t -> Put put forall b c a. (b -> c) -> (a -> b) -> a -> c . Key -> Text Key.toText instance Binary v => Binary (KM.KeyMap v) where get :: Get (KeyMap v) get = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b fmap forall v. [(Key, v)] -> KeyMap v KM.fromList forall t. Binary t => Get t get put :: KeyMap v -> Put put = forall t. Binary t => t -> Put put forall b c a. (b -> c) -> (a -> b) -> a -> c . forall v. KeyMap v -> [(Key, v)] KM.toList #endif