json2-0.2.1: This library provides support for JSON.

Data.JSON2

Contents

Synopsis

Data types and classes

type Jsons = [Json]Source

class ToJson a whereSource

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]]

Methods

toJson :: a -> JsonSource

Instances

ToJson Bool 
ToJson Char 
ToJson Double 
ToJson Float 
ToJson Int 
ToJson Integer 
ToJson Rational 
ToJson String 
ToJson () 
ToJson Json 
ToJson a => ToJson [a] 
ToJson a => ToJson (Maybe a) 
(ToJson t1, ToJson t2) => ToJson (t1, t2) 
ToJson a => ToJson (Map String a) 
(ToJson t1, ToJson t2, ToJson t3) => ToJson (t1, t2, t3) 
(ToJson t1, ToJson t2, ToJson t3, ToJson t4) => ToJson (t1, t2, t3, t4) 
(ToJson t1, ToJson t2, ToJson t3, ToJson t4, ToJson t5) => ToJson (t1, t2, t3, t4, t5) 

class FromJson a whereSource

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)]

Methods

fromJson :: Json -> aSource

Instances

FromJson Bool 
FromJson Double 
FromJson Float 
FromJson Int 
FromJson Integer 
FromJson Rational 
FromJson String 
FromJson Json 
FromJson a => FromJson [a] 
FromJson a => FromJson (Maybe a) 
(FromJson t1, FromJson t2) => FromJson (t1, t2) 
FromJson a => FromJson (Map String a) 
(FromJson t1, FromJson t2, FromJson t3) => FromJson (t1, t2, t3) 
(FromJson t1, FromJson t2, FromJson t3, FromJson t4) => FromJson (t1, t2, t3, t4) 
(FromJson t1, FromJson t2, FromJson t3, FromJson t4, FromJson t5) => FromJson (t1, t2, t3, t4, t5) 

Building JSON objects

emptyObj :: JsonSource

Create empty JSON object.

singleObj :: ToJson a => String -> a -> JsonSource

Create single JSON object.

(==>) :: ToJson a => String -> a -> JsonSource

(==>) Eq singleObj .

fromList :: ToJson a => [(String, a)] -> JsonSource

Create JSON object from list.

Union JSON objects

unionObj :: Json -> Json -> JsonSource

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"
   }
 }
 

unionsObj :: [Json] -> JsonSource

Merge JSON objects from list.

unionRecObj :: Json -> Json -> JsonSource

Recursive merge two JSON Objects.

Example :

 objR = objA `unionRecObj` objB
 pprint objR
 {
   "a": "1",
   "b": "2",
   "r": {
     "ra": "11",
     "rb": "12",
     "rc": "14"
   }
 }

Rendering to string

toString :: Json -> StringSource

Renders JSON to String.

Pretty print

pprint :: Json -> IO ()Source

Pretty-prints JSON.

pprints :: [Json] -> IO ()Source

Pretty-prints JSONs.