json-builder-0.3: Data structure agnostic JSON serialization

Copyright(c) 2011-2012 Leon P Smith
LicenseBSD3
MaintainerLeon P Smith <leon@melding-monads.com>
Safe HaskellNone
LanguageHaskell98

Data.Json.Builder

Contents

Description

Data structure agnostic JSON serialization

Synopsis

Json Values

data Json #

The Json type represents valid json syntax. It cannot be directly analyzed, however it can be turned into a Builder via toBuilder, a (lazy) ByteString via toJsonBS or toJsonLBS, or used as a component of a json Array or json Object using element or row.

Instances

Value Json # 

Methods

toJson :: Json -> Json #

toBuilder :: Value a => a -> Builder #

toJsonBS :: Value a => a -> ByteString #

class Value a where #

The Value typeclass represents types that can be rendered into valid json syntax.

Minimal complete definition

toJson

Methods

toJson :: a -> Json #

Instances

Value Bool #

renders as true or false

Methods

toJson :: Bool -> Json #

Value Double # 

Methods

toJson :: Double -> Json #

Value Float # 

Methods

toJson :: Float -> Json #

Value Int # 

Methods

toJson :: Int -> Json #

Value Int8 # 

Methods

toJson :: Int8 -> Json #

Value Int16 # 

Methods

toJson :: Int16 -> Json #

Value Int32 # 

Methods

toJson :: Int32 -> Json #

Value Int64 # 

Methods

toJson :: Int64 -> Json #

Value Integer # 

Methods

toJson :: Integer -> Json #

Value Word # 

Methods

toJson :: Word -> Json #

Value Word8 # 

Methods

toJson :: Word8 -> Json #

Value Word16 # 

Methods

toJson :: Word16 -> Json #

Value Word32 # 

Methods

toJson :: Word32 -> Json #

Value Word64 # 

Methods

toJson :: Word64 -> Json #

Value () #

renders as an Array

Methods

toJson :: () -> Json #

Value ByteString # 

Methods

toJson :: ByteString -> Json #

Value ByteString # 

Methods

toJson :: ByteString -> Json #

Value Text # 

Methods

toJson :: Text -> Json #

Value Text # 

Methods

toJson :: Text -> Json #

Value Array # 

Methods

toJson :: Array -> Json #

Value Object # 

Methods

toJson :: Object -> Json #

Value Escaped # 

Methods

toJson :: Escaped -> Json #

Value Json # 

Methods

toJson :: Json -> Json #

Value [Char] # 

Methods

toJson :: [Char] -> Json #

Value a => Value [a] #

renders as an Array

Methods

toJson :: [a] -> Json #

Value a => Value (Vector a) #

renders as an Array

Methods

toJson :: Vector a -> Json #

(Value a, Value b) => Value (a, b) #

renders as an Array

Methods

toJson :: (a, b) -> Json #

(JsString k, Value a) => Value (Map k a) #

renders as an Object

Methods

toJson :: Map k a -> Json #

(JsString k, Value a) => Value (HashMap k a) #

renders as an Object

Methods

toJson :: HashMap k a -> Json #

(Value a, Value b, Value c) => Value (a, b, c) #

renders as an Array

Methods

toJson :: (a, b, c) -> Json #

(Value a, Value b, Value c, Value d) => Value (a, b, c, d) #

renders as an Array

Methods

toJson :: (a, b, c, d) -> Json #

jsNull :: Json #

this renders as Json's null value.

Json Arrays ["foobar",true,42]

data Array #

The Array type represents syntax for a json array. It has been given a singleton constructor element and an instance of Monoid, so that mempty represents the empty array and mappend concatinates two arrays. Arbitrary arrays can be constructed using these operators.

Instances

Monoid Array # 

Methods

mempty :: Array #

mappend :: Array -> Array -> Array #

mconcat :: [Array] -> Array #

JsArray Array # 

Methods

toArray :: Array -> Array #

Value Array # 

Methods

toJson :: Array -> Json #

element :: Value a => a -> Array #

The element function constructs a json array consisting of exactly one value. These arrays can be concatinated using mappend.

class JsArray a where #

Minimal complete definition

toArray

Methods

toArray :: a -> Array #

Instances

JsArray () # 

Methods

toArray :: () -> Array #

JsArray Array # 

Methods

toArray :: Array -> Array #

Value a => JsArray [a] # 

Methods

toArray :: [a] -> Array #

Value a => JsArray (Vector a) # 

Methods

toArray :: Vector a -> Array #

(Value a, Value b) => JsArray (a, b) # 

Methods

toArray :: (a, b) -> Array #

(Value a, Value b, Value c) => JsArray (a, b, c) # 

Methods

toArray :: (a, b, c) -> Array #

(Value a, Value b, Value c, Value d) => JsArray (a, b, c, d) # 

Methods

toArray :: (a, b, c, d) -> Array #

Json Objects {"x":3.14,"y":-2.7}

data Object #

The Object type represents syntax for a json object. It has a singleton constructor row, and an instance of Monoid, so that mempty represents the empty object and mappend concatinates two objects. Arbitrary objects can be constructed using these operators.

Note that duplicate field names will appear in the output, so it is up to the user of this interface to avoid duplicate field names.

Instances

row :: (JsString k, Value a) => k -> a -> Object #

The row function constructs a json object consisting of exactly one field. These objects can be concatinated using mappend.

class JsObject a where #

Minimal complete definition

toObject

Methods

toObject :: a -> Object #

Instances

JsObject Object # 

Methods

toObject :: Object -> Object #

(JsString k, Value a) => JsObject (Map k a) # 

Methods

toObject :: Map k a -> Object #

(JsString k, Value a) => JsObject (HashMap k a) # 

Methods

toObject :: HashMap k a -> Object #

Json Strings

data Escaped #

The Escaped type represents json string syntax. The purpose of this type is so that json strings can be efficiently constructed from multiple Haskell strings without superfluous conversions or concatinations.

Internally, it is just a Builder value which must produce a UTF-8 encoded bytestring with backslashes, quotes, and control characters appropriately escaped. It also must not render the opening or closing quote, which are instead rendered by toJson.

class Value a => JsString a where #

The JsString typeclass represents types that can be render into json string syntax. They are special because only strings can appear as field names of json objects.

Minimal complete definition

escape

Methods

escape :: a -> Escaped #

Instances

JsString ByteString #

must be UTF-8 encoded

Methods

escape :: ByteString -> Escaped #

JsString ByteString #

must be UTF-8 encoded

Methods

escape :: ByteString -> Escaped #

JsString Text # 

Methods

escape :: Text -> Escaped #

JsString Text # 

Methods

escape :: Text -> Escaped #

JsString Escaped # 

Methods

escape :: Escaped -> Escaped #

JsString [Char] # 

Methods

escape :: [Char] -> Escaped #

Monoid (from Data.Monoid)

class Monoid a where #

The class of monoids (types with an associative binary operation that has an identity). Instances should satisfy the following laws:

  • mappend mempty x = x
  • mappend x mempty = x
  • mappend x (mappend y z) = mappend (mappend x y) z
  • mconcat = foldr mappend mempty

The method names refer to the monoid of lists under concatenation, but there are many other instances.

Some types can be viewed as a monoid in more than one way, e.g. both addition and multiplication on numbers. In such cases we often define newtypes and make those instances of Monoid, e.g. Sum and Product.

Minimal complete definition

mempty, mappend

Methods

mempty :: a #

Identity of mappend

mappend :: a -> a -> a #

An associative operation

mconcat :: [a] -> a #

Fold a list using the monoid. For most types, the default definition for mconcat will be used, but the function is included in the class definition so that an optimized version can be provided for specific types.

Instances

Monoid Ordering 
Monoid () 

Methods

mempty :: () #

mappend :: () -> () -> () #

mconcat :: [()] -> () #

Monoid All 

Methods

mempty :: All #

mappend :: All -> All -> All #

mconcat :: [All] -> All #

Monoid Any 

Methods

mempty :: Any #

mappend :: Any -> Any -> Any #

mconcat :: [Any] -> Any #

Monoid ByteString 
Monoid ByteString 
Monoid Builder 
Monoid Poke 

Methods

mempty :: Poke #

mappend :: Poke -> Poke -> Poke #

mconcat :: [Poke] -> Poke #

Monoid Write 

Methods

mempty :: Write #

mappend :: Write -> Write -> Write #

mconcat :: [Write] -> Write #

Monoid CommaMonoid # 
Monoid Array # 

Methods

mempty :: Array #

mappend :: Array -> Array -> Array #

mconcat :: [Array] -> Array #

Monoid Object # 
Monoid Escaped # 
Monoid [a] 

Methods

mempty :: [a] #

mappend :: [a] -> [a] -> [a] #

mconcat :: [[a]] -> [a] #

Monoid a => Monoid (Maybe a)

Lift a semigroup into Maybe forming a Monoid according to http://en.wikipedia.org/wiki/Monoid: "Any semigroup S may be turned into a monoid simply by adjoining an element e not in S and defining e*e = e and e*s = s = s*e for all s ∈ S." Since there is no "Semigroup" typeclass providing just mappend, we use Monoid instead.

Methods

mempty :: Maybe a #

mappend :: Maybe a -> Maybe a -> Maybe a #

mconcat :: [Maybe a] -> Maybe a #

Monoid a => Monoid (IO a) 

Methods

mempty :: IO a #

mappend :: IO a -> IO a -> IO a #

mconcat :: [IO a] -> IO a #

Ord a => Monoid (Max a) 

Methods

mempty :: Max a #

mappend :: Max a -> Max a -> Max a #

mconcat :: [Max a] -> Max a #

Ord a => Monoid (Min a) 

Methods

mempty :: Min a #

mappend :: Min a -> Min a -> Min a #

mconcat :: [Min a] -> Min a #

Monoid a => Monoid (Dual a) 

Methods

mempty :: Dual a #

mappend :: Dual a -> Dual a -> Dual a #

mconcat :: [Dual a] -> Dual a #

Monoid (Endo a) 

Methods

mempty :: Endo a #

mappend :: Endo a -> Endo a -> Endo a #

mconcat :: [Endo a] -> Endo a #

Num a => Monoid (Sum a) 

Methods

mempty :: Sum a #

mappend :: Sum a -> Sum a -> Sum a #

mconcat :: [Sum a] -> Sum a #

Num a => Monoid (Product a) 

Methods

mempty :: Product a #

mappend :: Product a -> Product a -> Product a #

mconcat :: [Product a] -> Product a #

Monoid (First a) 

Methods

mempty :: First a #

mappend :: First a -> First a -> First a #

mconcat :: [First a] -> First a #

Monoid (Last a) 

Methods

mempty :: Last a #

mappend :: Last a -> Last a -> Last a #

mconcat :: [Last a] -> Last a #

Monoid (Array a) 

Methods

mempty :: Array a #

mappend :: Array a -> Array a -> Array a #

mconcat :: [Array a] -> Array a #

Monoid (Vector a) 

Methods

mempty :: Vector a #

mappend :: Vector a -> Vector a -> Vector a #

mconcat :: [Vector a] -> Vector a #

Monoid b => Monoid (a -> b) 

Methods

mempty :: a -> b #

mappend :: (a -> b) -> (a -> b) -> a -> b #

mconcat :: [a -> b] -> a -> b #

(Monoid a, Monoid b) => Monoid (a, b) 

Methods

mempty :: (a, b) #

mappend :: (a, b) -> (a, b) -> (a, b) #

mconcat :: [(a, b)] -> (a, b) #

Monoid (Proxy k s) 

Methods

mempty :: Proxy k s #

mappend :: Proxy k s -> Proxy k s -> Proxy k s #

mconcat :: [Proxy k s] -> Proxy k s #

Ord k => Monoid (Map k v) 

Methods

mempty :: Map k v #

mappend :: Map k v -> Map k v -> Map k v #

mconcat :: [Map k v] -> Map k v #

(Eq k, Hashable k) => Monoid (HashMap k v) 

Methods

mempty :: HashMap k v #

mappend :: HashMap k v -> HashMap k v -> HashMap k v #

mconcat :: [HashMap k v] -> HashMap k v #

(Monoid a, Monoid b, Monoid c) => Monoid (a, b, c) 

Methods

mempty :: (a, b, c) #

mappend :: (a, b, c) -> (a, b, c) -> (a, b, c) #

mconcat :: [(a, b, c)] -> (a, b, c) #

Alternative f => Monoid (Alt * f a) 

Methods

mempty :: Alt * f a #

mappend :: Alt * f a -> Alt * f a -> Alt * f a #

mconcat :: [Alt * f a] -> Alt * f a #

(Monoid a, Monoid b, Monoid c, Monoid d) => Monoid (a, b, c, d) 

Methods

mempty :: (a, b, c, d) #

mappend :: (a, b, c, d) -> (a, b, c, d) -> (a, b, c, d) #

mconcat :: [(a, b, c, d)] -> (a, b, c, d) #

(Monoid a, Monoid b, Monoid c, Monoid d, Monoid e) => Monoid (a, b, c, d, e) 

Methods

mempty :: (a, b, c, d, e) #

mappend :: (a, b, c, d, e) -> (a, b, c, d, e) -> (a, b, c, d, e) #

mconcat :: [(a, b, c, d, e)] -> (a, b, c, d, e) #