Basic functions

toJsonString :: Json a => a -> String

Converts the value to a string encoded with JSON

fromJsonString :: Json a => String -> a

Parses a JSON encoded string into a value

Supported value types

This module supports the following value types:

instance Json String
instance Json Short
instance Json Integer
instance Json Long
instance Json Float
instance Json Double

instance (Json a) => Json [a]
instance (Json a) => Json (Maybe a)

instance Json ()
instance (Json a, Json b) => Json (a, b)
instance (Json a, Json b, Json c) => Json (a, b, c)
instance (Json a, Json b, Json c, Json d) => Json (a, b, c, d)
instance (Json a, Json b, Json c, Json d, Json e) => Json (a, b, c, d, e) 

instance Json Json

Generic JSON Type

data Json

A JSON value held in memory as a tree. Numbers are kept as either JsonDouble or JsonLong depending on whether the source had a fractional part, and the fields of a JsonObject keep the order they were written or read in.

JsonString :: String -> Json
JsonDouble :: Double -> Json
JsonLong :: Long -> Json
JsonArray :: [Json] -> Json
JsonBoolean :: Boolean -> Json
JsonNull :: Json
JsonObject :: [JsonField] -> Json
data JsonField

One field of a JSON object: its name and its value.

JsonField :: String -> Json -> JsonField

Adding support for additional value types

data JsonGenerator

A sink that JSON is written to as a stream of tokens, one value at a time, so that a document never has to be held in memory as a whole. It is what the writeJson method of the Json class writes to.

data JsonParser

A source that JSON is read from as a stream of tokens, one value at a time, so that a document never has to be held in memory as a whole. It is what the readJson method of the Json class reads from.

class Json a

The class of types whose values can be encoded as JSON and decoded back. Methods toJson and fromJson must be implemented; writeJson and readJson then work through them, and are worth implementing separately only when building the intermediate Json value is worth avoiding.

The comment at the top of this module shows how an instance is written.

writeJson :: Json a => JsonGenerator -> a -> <Proc> ()

Writes the value directly to the generator, without building a Json value in between.

readJson :: Json a => JsonParser -> <Proc> a

Reads a value from the parser, starting at the token the parser is currently on, without building a Json value in between.

toJson :: Json a => a -> Json

The value as a Json value.

fromJson :: Json a => Json -> a

The value that the Json value represents. Fails if the JSON value is not of the shape the type expects.

It is enough to implement toJson and fromJson.

Undocumented entities

lookupJsonField :: String -> Json -> Maybe Json

The value of the named field of a JSON object, or Nothing if the object has no such field. If the object has several fields of that name, the first one is returned. Fails if the value is not an object.