hjson-query-1.0.1: examples/Example1.hs
import Text.HJson
import Text.HJson.Query
import Text.HJson.Pretty as PP
import Data.List
j1 = jParse "{\"Europa\": {\"Ukraine\": [\"Kiyv\", \"Gitomir\", \"Lviv\"]}}"
j2 = jParse "{\"Asia\": {\"Japan\": [\"Tokyo\"]}}"
j3 = jParse "{\"Europa\": {\"UnitedKingdom\": [\"London\", \"Glasgow\"]}}"
j4 = jParse "{\"Europa\": {\"Germany\": [\"Berlin\", \"Bonn\"]}}"
j5 = jParse "{\"Africa\": {}}"
j6 = jParse"{\"America\": {\"USA\": [], \"Canada\": [\"Toronto\"]}}"
j7 = jParse "{\"Australia\": [\"Melburn\", \"Adelaida\"]}"
merg = mergesRec [j1, j2, j3, j4, j5, j6, j7]
ex0 = pputJson merg
---------------------------------------------------------------------------
query1 = getFromKeys ["Europa", "America", "Africa"]
json1 = query1 merg
ex1 = pputJsons json1
---------------------------------------------------------------------------
record1 = "[\"Sidoroff\",\"Jhon\",1975,null]"
ex11 = debugQuery (getFromIndexes [3,1,0]) record1
---------------------------------------------------------------------------
query2 = query1 >>> getFromObj
json2 = query2 merg
ex2 = pputJsons json2
---------------------------------------------------------------------------
-- Qwery: All citys Europa, America, Australia and Africa
q31 = getFromKeys ["Europa", "America", "Africa", "Australia"]
>>> (getFromArr `orElse` getFromObj)
>>> (isStr `orElse` getFromArr)
ex31 = pputJsons $ q31 merg
q32 = getFromKeys ["Europa", "America", "Africa", "Australia"]
>>> (getFromObj `when` isObj)
>>> getFromArr
ex32 = pputJsons $ q32 merg
q33 = getFromKeys ["Europa", "America", "Africa", "Australia"]
>>>
deep getFromArr
ex33 = pputJsons $ q33 merg
----------------------------------------------------------------------------
ex4 = debugQuery (deep isPrimitive) "[1,[false, true, [null]]]"
--- Debug print and helpers ------------------------------------------------
--
debugQuery :: JFilter -> String -> IO ()
debugQuery query jstr = case (fromString jstr) of
Left errmsg -> error errmsg
Right json -> putStrLn $ pprints $ query json
--
pputJson :: Json -> IO ()
pputJson = putStrLn . pprint
--
pputJsons :: Jsons -> IO ()
pputJsons = putStrLn . pprints
-- parse JSON
jParse :: String -> Json
jParse jstr = case (fromString jstr) of
Left l -> error l
Right json -> json
pprint = PP.toString " "
pprints js = concat $ intersperse "\n" $ map pprint js