packages feed

hjpath 2.0 → 3.0

raw patch · 2 files changed

+61/−31 lines, 2 filesdep ~parsecPVP ok

version bump matches the API change (PVP)

Dependency ranges changed: parsec

API changes (from Hackage documentation)

- Text.JSON.JPath: instance Show Element
- Text.JSON.JPath: jPath' :: String -> Json -> [Json]
- Text.JSON.JPath: jPathModify' :: String -> (Json -> Json) -> Json -> Json
+ Text.JSON.JPath: ArrayLookup :: Integer -> QueryElement
+ Text.JSON.JPath: DeepLookup :: QueryElement
+ Text.JSON.JPath: JArray :: [Json] -> Json
+ Text.JSON.JPath: JBool :: Bool -> Json
+ Text.JSON.JPath: JNull :: Json
+ Text.JSON.JPath: JNumber :: Rational -> Json
+ Text.JSON.JPath: JObject :: Map String Json -> Json
+ Text.JSON.JPath: JString :: String -> Json
+ Text.JSON.JPath: ObjectLookup :: String -> QueryElement
+ Text.JSON.JPath: WildcardLookup :: QueryElement
+ Text.JSON.JPath: class HJsonLike a
+ Text.JSON.JPath: class QueryLike a
+ Text.JSON.JPath: data Json :: *
+ Text.JSON.JPath: data QueryElement
+ Text.JSON.JPath: instance HJsonLike Json
+ Text.JSON.JPath: instance HJsonLike String
+ Text.JSON.JPath: instance QueryLike QueryElement
+ Text.JSON.JPath: instance QueryLike String
+ Text.JSON.JPath: instance QueryLike [QueryElement]
+ Text.JSON.JPath: instance Show QueryElement
- Text.JSON.JPath: jPath :: String -> String -> Either String [Json]
+ Text.JSON.JPath: jPath :: (HJsonLike j, QueryLike q) => q -> j -> [j]
- Text.JSON.JPath: jPathModify :: String -> (Json -> Json) -> String -> Either String String
+ Text.JSON.JPath: jPathModify :: (HJsonLike j, QueryLike q) => q -> (Json -> Json) -> j -> j

Files

Text/JSON/JPath.hs view
@@ -1,7 +1,9 @@+{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}+ {- | I wanted to insert a description here, but got tired fighting with haddock escaping. Documentation: <http://bitcheese.net/wiki/code/hjpath> -} -module Text.JSON.JPath (jPath, jPath', jPathModify, jPathModify') where+module Text.JSON.JPath (jPath, jPathModify, Json(..), QueryElement(..), HJsonLike, QueryLike) where  import qualified Data.Map as Map import Data.List@@ -11,36 +13,64 @@ import Text.ParserCombinators.Parsec.Char import Text.ParserCombinators.Parsec.Prim -data Element = ObjectLookup String | ArrayLookup Integer | WildcardLookup | DeepLookup deriving (Show)+-- | JPath query building blocks+data QueryElement+	-- | Look up element in object+	= ObjectLookup String+	-- | Look up array element (from 0 forward or from -1 backward)+	| ArrayLookup Integer+	-- | Matches any child element (array or hash entries)+	| WildcardLookup+	-- | Matches any number of child entries+	| DeepLookup+		deriving (Show) --- |Evaluates JPath query on JSON String-jPath :: String -- ^ JPath query-	-> String 	-- ^ JSON as String-	-> Either String [Json] -- ^ (Left parsing error) or (Right results)-jPath query s = let json = JSON.fromString s-	in either (Left) (Right . jPath' query) json+-- | Class that allows type to be used as JPath query+class QueryLike a where+	toQuery :: a -> [QueryElement] --- |Evaluates JPath query on pre-parsed JSON-jPath' :: String  -- ^ JPath query-	-> Json -- ^ Parsed JSON-	-> [Json] -- ^ List of results-jPath' query v = let parsedQuery = parseExpression query-	in either (const []) (\q -> jPathP q v) parsedQuery+instance QueryLike String where+	toQuery = either (const []) (id) . parseExpression --- |Modifies JSON content under JPath expression-jPathModify :: String -- ^ JPath query-	-> (Json -> Json) -- ^ Element modifier function-	-> String -- ^ JSON as string-	-> Either String String -- ^ (Left parsing error) or (Right modified JSON as string)-jPathModify query modifier json = either (Left) (Right . JSON.toString . jPathModify' query modifier) $ JSON.fromString json+instance QueryLike QueryElement where+	toQuery e = [e] --- |jPathModify for pre-parsed JSON-jPathModify' :: String -- ^ JPath query-	-> (Json -> Json) -- ^ Element modifier function-	-> Json -- ^ JSON-	-> Json -- ^ modified JSON-jPathModify' query modifier json = either (const json) (\q -> (jPathModifyP q modifier json)) $ parseExpression query+instance QueryLike [QueryElement] where+	toQuery = id +-- | Class that allows type to be used as JSON, all you need to implement is converting to and from Text.HJson.Json+class HJsonLike a where+	toHJson :: a -> Json+	fromHJson :: Json -> a++instance HJsonLike String where+	toHJson = either (const JNull) (id) . JSON.fromString+	fromHJson = JSON.toString++instance HJsonLike Json where+	toHJson = id+	fromHJson = id++-- | Evaluates JPath query on JSON String+jPath :: (HJsonLike j, QueryLike q) =>+	q -- ^ JPath query+	-> j 	-- ^ JSON +	-> [j] -- ^ Results+jPath q j = +	let json = toHJson j;+		query = toQuery q+		in map (fromHJson) $ jPathP query json++-- | Modifies JSON content under JPath expression+jPathModify :: (HJsonLike j, QueryLike q) =>	q -- ^ JPath query+	-> (Json -> Json) -- ^ modifier function+	-> j -- ^ JSON+	-> j -- ^ Modified JSON+jPathModify q modifier j = +	let json = toHJson j;+		query = toQuery q+		in fromHJson $ jPathModifyP query modifier json+ -- private functions  expression = do@@ -50,7 +80,7 @@  slash = string "/" -element :: GenParser Char st [Element]+element :: GenParser Char st [QueryElement] element = do 	parsedName <- optionMaybe (deepLookup <|> wildcard <|> name) 	parsedIndex <- optionMaybe index@@ -79,7 +109,7 @@  parseExpression = parse expression "JPath query" -jPathP :: [Element] -> Json -> [Json]+jPathP :: [QueryElement] -> Json -> [Json] jPathP [] v = [v] jPathP (e:es) v = case e of 	ObjectLookup s -> case v of@@ -97,7 +127,7 @@ 		otherwise -> [] 	DeepLookup -> concat [jPathP (WildcardLookup:es) v, jPathP ([WildcardLookup, DeepLookup] ++ es) v] -jPathModifyP :: [Element] -> (Json -> Json) -> Json -> Json+jPathModifyP :: [QueryElement] -> (Json -> Json) -> Json -> Json jPathModifyP [] modifier v = modifier v jPathModifyP (e:es) modifier v = let traverse = jPathModifyP es modifier in case e of 	ObjectLookup s -> case v of
hjpath.cabal view
@@ -1,5 +1,5 @@ Name: hjpath-Version: 2.0+Version: 3.0 Synopsis: XPath-like syntax for querying JSON Category: Text Description: JPath is XPath-inspired query language to query JSON data.@@ -17,4 +17,4 @@  library  Exposed-Modules: Text.JSON.JPath- Build-Depends: base >= 4 && < 5, hjson, parsec >= 2.1, containers+ Build-Depends: base >= 4 && < 5, hjson, parsec >= 3.0.0, containers