packages feed

hjpath (empty) → 0.1

raw patch · 4 files changed

+116/−0 lines, 4 filesdep +basedep +containersdep +jsonsetup-changed

Dependencies added: base, containers, json, parsec

Files

+ COPYING view
@@ -0,0 +1,13 @@+Copyright (c) 2009, Voker57++All rights reserved.++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.++Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.++The name of author may not be used to endorse or promote products derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,3 @@+import Distribution.Simple++main = defaultMain
+ Text/JSON/JPath.hs view
@@ -0,0 +1,80 @@+module Text.JSON.JPath (jPath, jPath') where++import qualified Data.Map as Map+import Data.Maybe+import Text.JSON+import Text.ParserCombinators.Parsec.Combinator+import Text.ParserCombinators.Parsec.Char+import Text.ParserCombinators.Parsec.Prim++data Element = ObjectLookup String | ArrayLookup Int | WildcardLookup | DeepLookup deriving (Show)++-- |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'++-- |Evaluates JPath query on pre-parsed JSON+jPath' :: String  -- ^ JPath query+	-> JSValue -- ^ Parsed JSON+	-> Either String [JSValue] -- ^ Either error text or list of results+jPath' query v = let parsedQuery = parseExpression query+	in either (Left . show) (\q -> Right $ jPathP q v) parsedQuery++expression = do+	result <- element `sepBy` slash+	eof+	return $ concat result++slash = string "/"++element :: GenParser Char st [Element]+element = do+	parsedName <- optionMaybe (deepLookup <|> wildcard <|> name)+	parsedIndex <- optionMaybe index+	return $ catMaybes [parsedName, parsedIndex]++name = do+	parsedName <- many1 (noneOf "/[]")+	return $ ObjectLookup parsedName++deepLookup = do+	string "**"+	return DeepLookup++wildcard = do+	string "*"+	return WildcardLookup++integer = do+	minus <- optionMaybe (string "-")+	number <- many1 digit+	return $ (fromMaybe "" minus) ++ number++index = do+	result <- between (string "[") (string "]") integer+	return $ ArrayLookup $ read result++parseExpression = parse expression "JPath query"++jPathP :: [Element] -> JSValue -> [JSValue]+jPathP [] v = [v]+jPathP (e:es) v = case e of+	ObjectLookup s -> case v of+		JSObject wtf -> maybe [] (jPathP es) $ s `lookup` (fromJSObject wtf)+		otherwise -> []+	ArrayLookup i -> case v of+		JSArray 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+		otherwise -> []+	DeepLookup -> concat [jPathP (WildcardLookup:es) v, jPathP ([WildcardLookup, DeepLookup] ++ es) v]
+ hjpath.cabal view
@@ -0,0 +1,20 @@+Name: hjpath+Version: 0.1+Synopsis: XPath-like syntax for querying JSON+Category: Text+Description: JPath is XPath-inspired query language to query JSON data.+License: BSD3+License-file: COPYING+Author: Voker57+Maintainer: voker57@gmail.com+Homepage: http://bitcheese.net/wiki/code/hjpath+Build-type: Simple+Cabal-version: >= 1.6++Source-repository head+    Type: git+    Location: git://git.bitcheese.net/hjpath++library+ Exposed-Modules: Text.JSON.JPath+ Build-Depends: base >= 4 && < 5, json, parsec >= 2.1, containers