JSON-Combinator 0.1.2 → 0.2.0
raw patch · 4 files changed
+72/−54 lines, 4 filesdep +failure
Dependencies added: failure
Files
- JSON-Combinator.cabal +2/−1
- Text/JSON/Combinator.hs +39/−30
- Text/JSON/JSONField.hs +31/−22
- Text/JSON/JSONParse.hs +0/−1
JSON-Combinator.cabal view
@@ -1,5 +1,5 @@ Name: JSON-Combinator-Version: 0.1.2+Version: 0.2.0 License: BSD3 License-File: LICENSE Author: Tony Morris <ʇǝu˙sıɹɹoɯʇ@ןןǝʞsɐɥ>@@ -28,6 +28,7 @@ , json , bytestring , bytestring-trie+ , failure GHC-Options: -Wall
Text/JSON/Combinator.hs view
@@ -1,3 +1,6 @@+{-# LANGUAGE FlexibleContexts #-}++ -- | Combinators for JSON data types. module Text.JSON.Combinator (@@ -98,6 +101,8 @@ import Text.JSON.JSONPrint import Text.JSON.Interact import Text.JSON.InteractFile+import Text.JSON.Failure+import Control.Failure import Control.Applicative import Data.Maybe import Data.Monoid@@ -221,43 +226,47 @@ -- | Returns the potential boolean value of a JSON value. getBool ::- JSONLike j s a o f =>+ (Failure (ExpectedBool j) m, JSONLike j s a o f) => j -- ^ The JSON value.- -> Maybe Bool-getBool =- foldJSON Nothing (Just True) (Just False) (const Nothing) (const Nothing) (const Nothing) (const Nothing)+ -> m Bool+getBool j =+ let fl = failure (ExpectedBool j)+ in foldJSON fl (return True) (return False) (const fl) (const fl) (const fl) (const fl) j --- | Returns the potential number value of a JSON value. getNumber ::- JSONLike j s a o f =>+ (Failure (ExpectedNumber j) m, JSONLike j s a o f) => j -- ^ The JSON value.- -> Maybe Rational-getNumber =- foldJSON Nothing Nothing Nothing Just (const Nothing) (const Nothing) (const Nothing)+ -> m Rational+getNumber j =+ let fl = failure (ExpectedNumber j)+ in foldJSON fl fl fl return (const fl) (const fl) (const fl) j -- | Returns the potential string value of a JSON value. getString ::- JSONLike j s a o f =>+ (Failure (ExpectedString j) m, JSONLike j s a o f) => j -- ^ The JSON value.- -> Maybe s-getString =- foldJSON Nothing Nothing Nothing (const Nothing) Just (const Nothing) (const Nothing)+ -> m s+getString j =+ let fl = failure (ExpectedString j)+ in foldJSON fl fl fl (const fl) return (const fl) (const fl) j -- | Returns the potential array value of a JSON value. getArray ::- JSONLike j s a o f =>+ (Failure (ExpectedArray j) m, JSONLike j s a o f) => j -- ^ The JSON value.- -> Maybe (a j)-getArray =- foldJSON Nothing Nothing Nothing (const Nothing) (const Nothing) Just (const Nothing)+ -> m (a j)+getArray j =+ let fl = failure (ExpectedArray j)+ in foldJSON fl fl fl (const fl) (const fl) return (const fl) j -- | Returns the potential object value of a JSON value. getObject ::- JSONLike j s a o f =>+ (Failure (ExpectedObject j) m, JSONLike j s a o f) => j -- ^ The JSON value.- -> Maybe (o j)-getObject =- foldJSON Nothing Nothing Nothing (const Nothing) (const Nothing) (const Nothing) Just+ -> m (o j)+getObject j =+ let fl = failure (ExpectedObject j)+ in foldJSON fl fl fl (const fl) (const fl) (const fl) return j -- | Returns a number value from a JSON value or if it is not a number, returns the given default. numberOr ::@@ -419,10 +428,10 @@ -- | Returns the possible value associated with the given field if this is an object. An alias for 'field'. (-|) ::- JSONField j s =>- s+ (JSONField j f, Failure (NoSuchField f) m) =>+ f -> j -- ^ The JSON value.- -> Maybe j+ -> m j (-|) = field @@ -567,19 +576,19 @@ -- | Traverses down JSON objects with the association fields and returns the potential value. field' ::- (JSONField j f, F.Foldable t) =>+ (F.Foldable t, Failure (NoSuchField f) m, JSONField j f) => t f- -> j -- ^ The JSON value.- -> Maybe j+ -> j+ -> m j field' = flip (F.foldrM field) -- | Traverses down JSON objects with the association fields and returns the potential value. An alias for 'field''. (-||) ::- (JSONField j f, F.Foldable t) =>+ (F.Foldable t, Failure (NoSuchField f) m, JSONField j f) => t f- -> j -- ^ The JSON value.- -> Maybe j+ -> j+ -> m j (-||) = field'
Text/JSON/JSONField.hs view
@@ -1,4 +1,4 @@-{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FunctionalDependencies #-}+{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FunctionalDependencies, FlexibleContexts #-} -- | The base functions for accessing JSON object fields. module Text.JSON.JSONField@@ -11,44 +11,53 @@ import Text.JSONb import qualified Text.JSON as J import Text.JSON.Types+import Text.JSON.Failure+import Control.Failure class JSONField j f | j -> f where field ::+ Failure (NoSuchField f) m => f -> j- -> Maybe j+ -> m j fields ::+ Failure (ExpectedObject j) m => j- -> Maybe [f]+ -> m [f] -- | Returns the potential object field values of a JSON value. values ::+ Failure (ExpectedObject j) m => j- -> Maybe [j]+ -> m [j] instance JSONField JSON ByteString where field f (Object o) =- T.lookup f o- field _ _ =- Nothing+ case T.lookup f o of+ Nothing -> failure (NoSuchField f)+ Just x -> return x+ field f _ =+ failure (NoSuchField f) fields (Object o) =- Just (T.keys o)- fields _ =- Nothing+ return (T.keys o)+ fields j =+ failure (ExpectedObject j) values (Object o) =- Just . fmap snd . T.toList $ o- values _ =- Nothing+ return . fmap snd . T.toList $ o+ values j =+ failure (ExpectedObject j) instance JSONField J.JSValue [Char] where field f (J.JSObject (JSONObject o)) =- lookup f o- field _ _ =- Nothing+ case lookup f o of+ Nothing -> failure (NoSuchField f)+ Just x -> return x+ field f _ =+ failure (NoSuchField f) fields (J.JSObject (JSONObject o)) =- Just (fmap fst o)- fields _ =- Nothing+ return (fmap fst o)+ fields j =+ failure (ExpectedObject j) values (J.JSObject (JSONObject o)) =- Just (fmap snd o)- values _ =- Nothing+ return (fmap snd o)+ values j =+ failure (ExpectedObject j)
Text/JSON/JSONParse.hs view
@@ -35,4 +35,3 @@ -> Either e j -- ^ Either error or a JSON object. parseJSON' = parseJSON []-