purescript-iso 0.0.5 → 0.0.6
raw patch · 3 files changed
+52/−2 lines, 3 filesdep +vector
Dependencies added: vector
Files
- purescript-iso.cabal +5/−2
- src/Data/Aeson/JSONMaybe.hs +45/−0
- test/Spec.hs +2/−0
purescript-iso.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: a43d5ec3d9017065ae05e5357c9112d551d36e5abf3184248379ba30bc7a97d8+-- hash: 1ac457c2e840c68856d81fe509f3eeec0c7612ec55a6ecd803f01902d5c3d8ff name: purescript-iso-version: 0.0.5+version: 0.0.6 synopsis: Isomorphic trivial data type definitions over JSON description: Please see the README on GitHub at <https://github.com/githubuser/purescript-iso#readme> category: Web@@ -34,6 +34,7 @@ Data.Aeson.JSONEmailAddress Data.Aeson.JSONInt Data.Aeson.JSONInteger+ Data.Aeson.JSONMaybe Data.Aeson.JSONScientific Data.Aeson.JSONString Data.Aeson.JSONTuple@@ -69,6 +70,7 @@ , time , utf8-string , uuid+ , vector , zeromq4-haskell , zeromq4-simple default-language: Haskell2010@@ -107,6 +109,7 @@ , time , utf8-string , uuid+ , vector , zeromq4-haskell , zeromq4-simple default-language: Haskell2010
+ src/Data/Aeson/JSONMaybe.hs view
@@ -0,0 +1,45 @@+{-# LANGUAGE+ OverloadedStrings+ , OverloadedLists+ , DeriveGeneric+ , DeriveAnyClass+ #-}++module Data.Aeson.JSONMaybe where++import Data.Aeson (ToJSON (..), FromJSON (..), Value (String, Array))+import Data.Aeson.Types (typeMismatch)+import qualified Data.Vector as V+import Control.DeepSeq (NFData)+import Test.QuickCheck (Arbitrary (..))+import Test.QuickCheck.Gen (oneof)+import GHC.Generics (Generic)+++data JSONMaybe a+ = JSONNothing+ | JSONJust a+ deriving (Eq, Ord, Show, Generic, NFData)++instance (ToJSON a) => ToJSON (JSONMaybe a) where+ toJSON x = case x of+ JSONNothing -> String ""+ JSONJust y -> Array [toJSON y]++instance (FromJSON a) => FromJSON (JSONMaybe a) where+ parseJSON json = case json of+ String s+ | s == "" -> pure JSONNothing+ | otherwise -> fail'+ Array a+ | length a == 1 -> JSONJust <$> parseJSON (a V.! 0)+ | otherwise -> fail'+ _ -> fail'+ where+ fail' = typeMismatch "JSONMaybe" json++instance (Arbitrary a) => Arbitrary (JSONMaybe a) where+ arbitrary = oneof+ [ pure JSONNothing+ , JSONJust <$> arbitrary+ ]
test/Spec.hs view
@@ -4,6 +4,7 @@ import Data.Aeson.JSONUnit (JSONUnit) import Data.Aeson.JSONEither (JSONEither)+import Data.Aeson.JSONMaybe (JSONMaybe) import Data.Aeson.JSONTuple (JSONTuple) import Data.Aeson.JSONDateTime (JSONDateTime) import Data.Aeson.JSONString (JSONString)@@ -55,6 +56,7 @@ registerTopic "ServerToClient" (Proxy :: Proxy ServerToClient) registerTopic "JSONUnit" (Proxy :: Proxy JSONUnit) registerTopic "JSONEither" (Proxy :: Proxy (JSONEither JSONUnit JSONUnit))+ registerTopic "JSONMaybe" (Proxy :: Proxy (JSONMaybe JSONUnit)) registerTopic "JSONTuple" (Proxy :: Proxy (JSONTuple JSONUnit JSONUnit)) registerTopic "JSONDate" (Proxy :: Proxy Day) registerTopic "JSONDateTime" (Proxy :: Proxy JSONDateTime)