Safe Haskell | Safe |
---|---|
Language | Haskell98 |
Serialising Haskell values to and from JSON values.
Synopsis
- data JSValue
- class JSON a where
- data Result a
- encode :: JSON a => a -> String
- decode :: JSON a => String -> Result a
- encodeStrict :: JSON a => a -> String
- decodeStrict :: JSON a => String -> Result a
- data JSString
- toJSString :: String -> JSString
- fromJSString :: JSString -> String
- data JSObject e
- toJSObject :: [(String, a)] -> JSObject a
- fromJSObject :: JSObject e -> [(String, e)]
- resultToEither :: Result a -> Either String a
- readJSNull :: GetJSON JSValue
- readJSBool :: GetJSON JSValue
- readJSString :: GetJSON JSValue
- readJSRational :: GetJSON Rational
- readJSArray :: GetJSON JSValue
- readJSObject :: GetJSON JSValue
- readJSValue :: GetJSON JSValue
- showJSNull :: ShowS
- showJSBool :: Bool -> ShowS
- showJSArray :: [JSValue] -> ShowS
- showJSRational :: Rational -> ShowS
- showJSRational' :: Bool -> Rational -> ShowS
- showJSObject :: JSObject JSValue -> ShowS
- showJSValue :: JSValue -> ShowS
- makeObj :: [(String, JSValue)] -> JSValue
- valFromObj :: JSON a => String -> JSObject JSValue -> Result a
- class JSKey a where
- encJSDict :: (JSKey a, JSON b) => [(a, b)] -> JSValue
- decJSDict :: (JSKey a, JSON b) => String -> JSValue -> Result [(a, b)]
JSON Types
JSON values
The type to which we encode Haskell values. There's a set of primitives, and a couple of heterogenous collection types.
Objects:
An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). A name is a string. A single colon comes after each name, separating the name from the value. A single comma separates a value from a following name.
Arrays:
An array structure is represented as square brackets surrounding zero or more values (or elements). Elements are separated by commas.
Only valid JSON can be constructed this way
JSNull | |
JSBool !Bool | |
JSRational Bool !Rational | |
JSString JSString | |
JSArray [JSValue] | |
JSObject (JSObject JSValue) |
Instances
Eq JSValue Source # | |
Ord JSValue Source # | |
Read JSValue Source # | |
Show JSValue Source # | |
IsString JSValue Source # | |
Defined in Text.JSON.Types fromString :: String -> JSValue # | |
JSON JSValue Source # | To ensure we generate valid JSON, we map Haskell types to JSValue internally, then pretty print that. |
Serialization to and from JSValues
The class of types serialisable to and from JSON
readJSON :: JSValue -> Result a Source #
showJSON :: a -> JSValue Source #
Instances
Encoding and Decoding
A type for parser results
encode :: JSON a => a -> String Source #
Encode a Haskell value into a string, in JSON format.
This is a superset of JSON, as types other than Array and Object are allowed at the top level.
decode :: JSON a => String -> Result a Source #
Decode a String representing a JSON value (either an object, array, bool, number, null)
This is a superset of JSON, as types other than Array and Object are allowed at the top level.
encodeStrict :: JSON a => a -> String Source #
Encode a value as a String in strict JSON format. This follows the spec, and requires all values at the top level to be wrapped in either an Array or Object. JSON types to be an Array or Object.
decodeStrict :: JSON a => String -> Result a Source #
Decode a String representing a strict JSON value. This follows the spec, and requires top level JSON types to be an Array or Object.
Wrapper Types
Strings can be represented a little more efficiently in JSON
toJSString :: String -> JSString Source #
Turn a Haskell string into a JSON string.
fromJSString :: JSString -> String Source #
As can association lists
Instances
Eq e => Eq (JSObject e) Source # | |
Ord e => Ord (JSObject e) Source # | |
Read e => Read (JSObject e) Source # | |
Show e => Show (JSObject e) Source # | |
JSON a => JSON (JSObject a) Source # | |
toJSObject :: [(String, a)] -> JSObject a Source #
Make JSON object out of an association list.
fromJSObject :: JSObject e -> [(String, e)] Source #
Serialization to and from Strings.
Reading JSON
readJSNull :: GetJSON JSValue Source #
Read the JSON null type
readJSBool :: GetJSON JSValue Source #
Read the JSON Bool type
readJSString :: GetJSON JSValue Source #
Read the JSON String type
readJSRational :: GetJSON Rational Source #
Read an Integer or Double in JSON format, returning a Rational
readJSArray :: GetJSON JSValue Source #
Read a list in JSON format
readJSObject :: GetJSON JSValue Source #
Read an object in JSON format
readJSValue :: GetJSON JSValue Source #
Read one of several possible JS types
Writing JSON
showJSNull :: ShowS Source #
Write the JSON null type
showJSBool :: Bool -> ShowS Source #
Write the JSON Bool type
showJSArray :: [JSValue] -> ShowS Source #
Show a list in JSON format
showJSRational :: Rational -> ShowS Source #
Show a Rational in JSON format
showJSValue :: JSValue -> ShowS Source #
Show JSON values
Instance helpers
valFromObj :: JSON a => String -> JSObject JSValue -> Result a Source #
Pull a value out of a JSON object.
Haskell types that can be used as keys in JSON objects.