json2-0.2.1: This library provides support for JSON.

Data.JSON2.Query

Contents

Description

Synopsis

Data Types

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.

isTrue :: JFilterSource

Filter JSON True.

isFalse :: JFilterSource

Filter JSON False.

isNull :: JFilterSource

Filter JSON null.

isAtomic :: JFilterSource

Filter primitive types.

getFromObj :: JFilterSource

Get all elements from object.

getFromKey :: String -> JFilterSource

Get elements from object with key.

getFromKeys :: [String] -> JFilterSource

Get elements from object with keys.

Examles:

 citys :: Jsons
 citys =
   [
     "Europa"    ==> "Ukraine" ==> ["Kiyv", "Gitomir", "Lviv"],
     "Asia"      ==> "Japan"   ==> ["Tokyo"],
     "Europa"    ==> "UK"      ==> ["London", "Glasgow"],
     "Europa"    ==> "Germany" ==> ["Berlin", "Bonn"],
     "America"   ==> "USA"     ==> ["NewYork"],
     "America"   ==> "Canada"  ==> ["Toronto"],
     "Australia" ==> ["Melburn", "Adelaida"]
   ]
 jCitys = foldl unionRecObj emptyObj citys
 ex1 = pprint jCitys
 {
   "America": {
     "Canada": ["Toronto"],
     "USA": ["NewYork"]
   },
   "Asia": {
     "Japan": ["Tokyo"]
   },
   "Australia": ["Melburn", "Adelaida"],
   "Europa": {
     "Germany": ["Berlin", "Bonn"],
     "UK": ["London", "Glasgow"],
     "Ukraine": ["Kiyv", "Gitomir", "Lviv"]
   }
 }
 query2 = getFromKeys ["Europa", "America", "Atlantida"]
 ex2 = pprints $ query2 jCitys
 [
   {
     "Germany": ["Berlin", "Bonn"],
     "UK": ["London", "Glasgow"],
     "Ukraine": ["Kiyv", "Gitomir", "Lviv"]
   },{
     "Canada": ["Toronto"],
     "USA": ["NewYork"]
   }
 ]

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.

Filter Combinators

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

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

Examples:

 query3 = getFromKeys ["Europa", "America"] >>> getFromObj 
 ex3 = pprints $ query3 jCitys

Result:

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

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

Concat results 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.

deep :: JFilter -> JFilterSource

Tree traversal filter for object and array.

deepObj :: JFilter -> JFilterSource

Tree traversal filter for array.

deepArr :: JFilter -> JFilterSource

Tree traversal filter for array.

Example:

  -- Query:  All city Europa and Australia.
  -- query31, query32, query33 is equal.
  
 query31 = getFromKeys ["Europa", "Australia"] 
   >>> (getFromArr `orElse`  getFromObj)
   >>> (isStr `orElse` getFromArr)
 ex31 = pprints $ query31 jCitys
 
 query32 = getFromKeys ["Europa", "Australia"] 
   >>> (getFromObj `when` isObj)
   >>> getFromArr
 ex32 = pprints $ query32 jCitys
 
 query33 = getFromKeys ["Europa",  "Australia"] 
   >>> deep getFromArr
 ex33 = pprints $ query33 jCitys

Result:

 [
   "Berlin","Bonn","London","Glasgow","Kiyv","Gitomir","Lviv","Melburn","Adelaida"
 ]