packages feed

hjpath 0.1 → 1.0

raw patch · 2 files changed

+14/−16 lines, 2 filesdep +RJsondep −jsonPVP ok

version bump matches the API change (PVP)

Dependencies added: RJson

Dependencies removed: json

API changes (from Hackage documentation)

- Text.JSON.JPath: jPath :: String -> String -> Either String [JSValue]
+ Text.JSON.JPath: jPath :: String -> String -> Either String [JsonData]
- Text.JSON.JPath: jPath' :: String -> JSValue -> Either String [JSValue]
+ Text.JSON.JPath: jPath' :: String -> JsonData -> [JsonData]

Files

Text/JSON/JPath.hs view
@@ -2,7 +2,7 @@  import qualified Data.Map as Map import Data.Maybe-import Text.JSON+import Text.RJson import Text.ParserCombinators.Parsec.Combinator import Text.ParserCombinators.Parsec.Char import Text.ParserCombinators.Parsec.Prim@@ -12,18 +12,16 @@ -- |Evaluates JPath query on JSON String jPath :: String -- ^ JPath query 	-> String 	-- ^ JSON as String-	-> Either String [JSValue] -- ^ Either error text or list of results-jPath query s = let json = (decode s) :: Result JSValue-	in case json of-		Error s -> Left s-		Ok json' -> jPath' query json'+	-> Either String [JsonData] -- ^ Either error text or list of results+jPath query s = let json = parseJsonString s+	in either (Left) (Right . jPath' query) json  -- |Evaluates JPath query on pre-parsed JSON jPath' :: String  -- ^ JPath query-	-> JSValue -- ^ Parsed JSON-	-> Either String [JSValue] -- ^ Either error text or list of results+	-> JsonData -- ^ Parsed JSON+	-> [JsonData] -- ^ List of results jPath' query v = let parsedQuery = parseExpression query-	in either (Left . show) (\q -> Right $ jPathP q v) parsedQuery+	in either (const []) (\q -> jPathP q v) parsedQuery  expression = do 	result <- element `sepBy` slash@@ -61,20 +59,20 @@  parseExpression = parse expression "JPath query" -jPathP :: [Element] -> JSValue -> [JSValue]+jPathP :: [Element] -> JsonData -> [JsonData] jPathP [] v = [v] jPathP (e:es) v = case e of 	ObjectLookup s -> case v of-		JSObject wtf -> maybe [] (jPathP es) $ s `lookup` (fromJSObject wtf)+		JDObject wtf -> maybe [] (jPathP es) $ s `Map.lookup` wtf 		otherwise -> [] 	ArrayLookup i -> case v of-		JSArray vs -> if i >= length vs || i < 0 - length vs  then+		JDArray vs -> if i >= length vs || i < 0 - length vs then 			[] 			else 			jPathP es $ vs !! (if i < 0 then length vs - abs i else i) 		otherwise -> [] 	WildcardLookup -> case v of-		JSObject wtf -> concat $ map (jPathP es) (map (snd) $ fromJSObject wtf)-		JSArray vs -> concat $ map (jPathP es) vs+		JDObject wtf -> concat $ map (jPathP es) (Map.elems wtf)+		JDArray vs -> concat $ map (jPathP es) vs 		otherwise -> [] 	DeepLookup -> concat [jPathP (WildcardLookup:es) v, jPathP ([WildcardLookup, DeepLookup] ++ es) v]
hjpath.cabal view
@@ -1,5 +1,5 @@ Name: hjpath-Version: 0.1+Version: 1.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, json, parsec >= 2.1, containers+ Build-Depends: base >= 4 && < 5, RJson, parsec >= 2.1, containers