-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | This library provides support for JSON. -- -- This library provides support for JSON. @package json2 @version 0.2.2 module Data.JSON2 data Json JString :: String -> Json JNumber :: Rational -> Json JBool :: Bool -> Json JNull :: Json JArray :: [Json] -> Json JObject :: (Map String Json) -> Json type Jsons = [Json] -- | This module provides instances ToJson for : (), Char, -- Json, Maybe, Bool, String, Integr, Int, Double, Float, Rational, Map -- String a, List, tuples 2-5 sizes . -- -- Example : -- --
-- table :: [(Int, Maybe String, Bool)] -- table = [ -- (1, Just "One", False), -- (2, Nothing, True) -- ] -- jTable = toJson table -- pprint jTable -- [[1, "One", false], [2, null, true]] --class ToJson a toJson :: ToJson a => a -> Json -- | This module provides instances FromJson for : Json, -- Maybe, Bool, String, Integr, Int, Double, Float, Rational, Map String -- a, List, tuples 2-5 sizes . -- -- Example : -- --
-- fromJson jTable :: [(Double, Maybe String, Bool)] -- [(1.0, Just "One", False), (2.0, Nothing, True)] --class FromJson a fromJson :: FromJson a => Json -> a -- | Create empty JSON object. emptyObj :: Json -- | Create single JSON object. singleObj :: ToJson a => String -> a -> Json -- | (==>) Eq singleObj . (==>) :: ToJson a => String -> a -> Json -- | Create JSON object from list. fromList :: ToJson a => [(String, a)] -> Json -- | Merge two JSON Objects. -- -- Example: -- --
-- objA = fromList [("a", toJson "1"), ("r", fromList [("ra", "11"), ("rb", "12")])]
-- pprint objA
-- {
-- "a": "1",
-- "r": {
-- "ra": "11",
-- "rb": "12"
-- }
-- }
-- objB = fromList [("b", toJson "2"), ("r", fromList [("rb", "13"), ("rc", "14")])]
-- pprint objB
-- {
-- "b": "2",
-- "r": {
-- "rb": "13",
-- "rc": "14"
-- }
-- }
-- pprint $ objA `unionObj` objB
-- {
-- "a": "1",
-- "b": "2",
-- "r": {
-- "ra": "11",
-- "rb": "12"
-- }
-- }
--
--
unionObj :: Json -> Json -> Json
-- | Merge JSON objects from list.
unionsObj :: [Json] -> Json
-- | Recursive merge two JSON Objects.
--
-- Example :
--
--
-- objR = objA `unionRecObj` objB
-- pprint objR
-- {
-- "a": "1",
-- "b": "2",
-- "r": {
-- "ra": "11",
-- "rb": "12",
-- "rc": "14"
-- }
-- }
--
unionRecObj :: Json -> Json -> Json
-- | Renders JSON to String.
toString :: Json -> String
-- | Pretty-prints JSON.
pprint :: Json -> IO ()
-- | Pretty-prints JSONs.
pprints :: [Json] -> IO ()
instance [incoherent] Eq Json
instance [incoherent] Show Json
instance [incoherent] Read Json
instance [incoherent] (FromJson t1, FromJson t2, FromJson t3, FromJson t4, FromJson t5) => FromJson (t1, t2, t3, t4, t5)
instance [incoherent] (ToJson t1, ToJson t2, ToJson t3, ToJson t4, ToJson t5) => ToJson (t1, t2, t3, t4, t5)
instance [incoherent] (FromJson t1, FromJson t2, FromJson t3, FromJson t4) => FromJson (t1, t2, t3, t4)
instance [incoherent] (ToJson t1, ToJson t2, ToJson t3, ToJson t4) => ToJson (t1, t2, t3, t4)
instance [incoherent] (FromJson t1, FromJson t2, FromJson t3) => FromJson (t1, t2, t3)
instance [incoherent] (ToJson t1, ToJson t2, ToJson t3) => ToJson (t1, t2, t3)
instance [incoherent] (FromJson t1, FromJson t2) => FromJson (t1, t2)
instance [incoherent] (ToJson t1, ToJson t2) => ToJson (t1, t2)
instance [incoherent] FromJson a => FromJson [a]
instance [incoherent] ToJson a => ToJson [a]
instance [incoherent] FromJson a => FromJson (Map String a)
instance [incoherent] ToJson a => ToJson (Map String a)
instance [incoherent] FromJson Rational
instance [incoherent] ToJson Rational
instance [incoherent] FromJson Float
instance [incoherent] ToJson Float
instance [incoherent] FromJson Double
instance [incoherent] ToJson Double
instance [incoherent] FromJson Int
instance [incoherent] ToJson Int
instance [incoherent] FromJson Integer
instance [incoherent] ToJson Integer
instance [incoherent] FromJson String
instance [incoherent] ToJson String
instance [incoherent] FromJson Bool
instance [incoherent] ToJson Bool
instance [incoherent] FromJson a => FromJson (Maybe a)
instance [incoherent] ToJson a => ToJson (Maybe a)
instance [incoherent] FromJson Json
instance [incoherent] ToJson Json
instance [incoherent] ToJson Char
instance [incoherent] ToJson ()
module Data.JSON2.FromSQL
-- | Conversion SQL-like JSON array to tree-like JSON object with use
-- column.
groupWithCol :: Json -> Json
-- | Conversion SQL-like JSON array to tree-like JSON object with use
-- columns.
--
-- COMPLEX EXAMPLE :
--
--
-- import Database.HDBC
-- import Database.HDBC.Sqlite3
-- import Data.JSON2
--
-- dbName = "..."
-- selectSql = "SELECT * from test"
--
-- main = do
-- conn <- connectSqlite3 dbName
-- stmtSel <- prepare conn selectSql
-- execute stmtSel []
-- q <- sFetchAllRows stmtSel
-- mapM print q
-- -- [Just "A",Just "1",Just "Pupkin",Just "1"]
-- -- [Just "A",Just "2",Just "Sidorov",Just "2"]
-- -- [Just "B",Just "22",Just "Petrov",Just "3"]
-- -- [Just "B",Just "22",Just "Ivanov",Just "4"]
-- -- [Just "B",Just "22",Just "Golubev",Just "5"]
-- -- [Just "B",Just "33",Just "Petrov",Just "6"]
-- let json = toJson (q :: [[Maybe String]])
-- pprint json
-- -- [
-- -- ["A", "1", "Pupkin", "1"],
-- -- ["A", "2", "Sidorov", "2"],
-- -- ["B", "22", "Petrov", "3"],
-- -- ["B", "22", "Ivanov", "4"],
-- -- ["B", "22", "Golubev", "5"],
-- -- ["B", "33", "Petrov", "6"]
-- -- ]
-- let json' = groupWithNCol 2 json
-- pprint json'
-- -- {
-- -- "A": {
-- -- "1": [["Pupkin", "1"]],
-- -- "2": [["Sidorov", "2"]]
-- -- },
-- -- "B": {
-- -- "22": [["Petrov", "3"], ["Ivanov", "4"], ["Golubev", "5"]],
-- -- "33": [["Petrov", "6"]]
-- -- }
-- -- }
-- disconnect conn
--
groupWithNCol :: Int -> Json -> Json
-- | See also: www.haskell.org/haskellwiki/HXT
module Data.JSON2.Query
type JFilter = Json -> Jsons
-- | Filter JSON objects.
isObj :: JFilter
-- | Filter JSON arrays.
isArr :: JFilter
-- | Filter JSON strings.
isStr :: JFilter
-- | Predicative filter JSON strings.
isStrBy :: (String -> Bool) -> JFilter
-- | Filter JSON numbers.
isNum :: JFilter
-- | Predicative filter JSON numbers.
isNumBy :: Fractional a => (a -> Bool) -> JFilter
-- | Filter JSON Bool.
isBool :: JFilter
-- | Filter JSON True.
isTrue :: JFilter
-- | Filter JSON False.
isFalse :: JFilter
-- | Filter JSON null.
isNull :: JFilter
-- | Filter primitive types.
isAtomic :: JFilter
-- | Get all elements from object.
getFromObj :: JFilter
-- | Get elements from object with key.
getFromKey :: String -> JFilter
-- | 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"]
-- }
-- ]
--
getFromKeys :: [String] -> JFilter
-- | Get all elements from array.
getFromArr :: JFilter
-- | Get element from array with index.
getFromIndex :: Int -> JFilter
-- | Get elements from array with indexes.
getFromIndexes :: [Int] -> JFilter
-- | Get all elements from object and array.
getChildern :: JFilter
-- | (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 -> JFilter -- | Concat results two filters. (<+>) :: JFilter -> JFilter -> JFilter -- | (f orElse g) - Apply f, if f returned -- null apply g. orElse :: JFilter -> JFilter -> JFilter -- | (f when g) - When g returned not -- null, apply f. when :: JFilter -> JFilter -> JFilter -- | (f guards g ) - If f returned null then -- null else apply g. guards :: JFilter -> JFilter -> JFilter -- | Tree traversal filter for object and array. deep :: JFilter -> JFilter -- | Tree traversal filter for array. deepObj :: JFilter -> JFilter -- | 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" -- ] --deepArr :: JFilter -> JFilter