hjson-query-1.0.2: library for querying from JSON

Text.HJson.Query

Contents

Synopsis

Types

type Jsons = [Json]Source

Building

emptyObj :: JsonSource

Create empty JSON object.

(-->) :: String -> Json -> JsonSource

Create single JSON object.

(<>) :: Json -> Json -> JsonSource

Merge two JSON Objects.

(<.>) :: Json -> Json -> JsonSource

Recursive merge two JSON Objects.

merges :: [Json] -> JsonSource

Merge list JSON Objects.

mergesRec :: [Json] -> JsonSource

recursive merge lists JSON Objects

Example:

 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 = mergsRec [j1, j2, j3, j4, j5, j6, j7]
 ex0 = pputJson merg

Result:

{
   "Africa": {
   },
   "America": {
      "Canada": ["Toronto"],
      "USA": []
   },
   "Asia": {
      "Japan": ["Tokyo"]
   },
   "Australia": ["Melburn", "Adelaida"],
   "Europa": {
      "Germany": ["Berlin", "Bonn"],
      "Ukraine": ["Kiyv", "Gitomir", "Lviv"],
      "UnitedKingdom": ["London", "Glasgow"]
   }
}

Filtering

isObj :: JFilterSource

Filter JSON objects .

isArr :: JFilterSource

Filter JSON arrays.

isStr :: JFilterSource

Filter JSON strings.

isStrBy :: (String -> Bool) -> JFilterSource

Predicative filter JSON strings.

isNum :: JFilterSource

Filter JSON numbers.

isNumBy :: Fractional a => (a -> Bool) -> JFilterSource

Predicative filter JSON numbers.

isBool :: JFilterSource

Filter JSON Bool.

isNull :: JFilterSource

Filter JSON null.

isPrimitive :: JFilterSource

Filter primitive types.

getFromKeys :: [String] -> JFilterSource

Get elements from object with keys.

Example:

 query1 = getFromKeys ["Europa", "America", "Africa"] 
 json1 = query1 merg 
 ex1 = pputJsons json1

Result:

 {
    "Germany": ["Berlin", "Bonn"],
    "Ukraine": ["Kiyv", "Gitomir", "Lviv"],
    "UnitedKingdom": ["London", "Glasgow"]
 }
 {
    "Canada": ["Toronto"],
    "USA": []
 }
 {
 
 }

getFromObj :: JFilterSource

Get all elements from object.

getFromArr :: JFilterSource

Get all elements from array.

getFromIndex :: Int -> JFilterSource

Get element from array with index.

getFromIndexes :: [Int] -> JFilterSource

Get elements from array with indexes.

getChildern :: JFilterSource

Get all elements from object and array.

getFromKey :: String -> JFilterSource

Get elements from object with key.

Filter Combinators

(>>>) :: JFilter -> JFilter -> JFilterSource

(f >>> g) - Apply filter f, later filter g .

Example:

 query2 = query1 >>> getFromObj 
 json2 = query2 merg
 ex2 = pputJsons json2

Result:

 ["Berlin", "Bonn"]
 ["Kiyv", "Gitomir", "Lviv"]
 ["London", "Glasgow"]
 ["Toronto"]
 []

(<+>) :: JFilter -> JFilter -> JFilterSource

Concat result two filters.

orElse :: JFilter -> JFilter -> JFilterSource

(f orElse g) - Apply f, if f returned null apply g.

when :: JFilter -> JFilter -> JFilterSource

(f when g) - When g returned not null, apply f.

guards :: JFilter -> JFilter -> JFilterSource

(f guards g ) - If f returned null then null else apply g.

deepObj :: JFilter -> JFilterSource

Tree traversal filter for object.

deepArr :: JFilter -> JFilterSource

Tree traversal filter for array.

deep :: JFilter -> JFilterSource

Tree traversal filter for objects and arrays.

Example:

 -- Qwery:  All city Europa, America, Australia and Africa
 -- q31, q32, q33 is equal
 
 q31 = getFromKeys ["Europa", "America", "Africa", "Australia"] 
   >>> (getFromArr `orElse`  getFromObj)
   >>> (isStr `orElse` getFromArr)
 
 q32 = getFromKeys ["Europa", "America", "Africa", "Australia"] 
   >>> (getFromObj `when` isObj)
   >>> getFromArr
 
 q33 = getFromKeys ["Europa", "America", "Africa", "Australia"] 
   >>> 
 deep getFromArr

See also: http://www.haskell.org/haskellwiki/HXT#The_concept_of_filters