Text.JSON.Types

json-0.11: Support for serialising Haskell to and from JSON

Safe Haskell | Safe-Inferred
Language | Haskell2010

Text.JSON.Types

Contents

- JSON Types
- Wrapper Types

Description

Basic support for working with JSON values.

Synopsis

- data JSValue
  - = JSNull
  - | JSBool !Bool
  - | JSRational Bool !Rational
  - | JSString JSString
  - | JSArray [JSValue]
  - | JSObject (JSObject JSValue)
- newtype JSString = JSONString {
  - fromJSString :: String
  }
- toJSString :: String -> JSString
- newtype JSObject e = JSONObject {
  - fromJSObject :: [(String, e)]
  }
- toJSObject :: [(String, a)] -> JSObject a
- get_field :: JSObject a -> String -> Maybe a
- set_field :: JSObject a -> String -> a -> JSObject a

# JSON Types

data JSValue #

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

Constructors

JSNull |
JSBool !Bool |
JSRational Bool !Rational |
JSString JSString |
JSArray [JSValue] |
JSObject (JSObject JSValue) |

#### Instances

Instances details

IsString JSValue # |
Instance details Defined in Text.JSON.Types Methods fromString :: String -> JSValue #
Read JSValue # |
Instance details Defined in Text.JSON.Types Methods readsPrec :: Int -> ReadS JSValue # readList :: ReadS [JSValue] # readPrec :: ReadPrec JSValue # readListPrec :: ReadPrec [JSValue] #
Show JSValue # |
Instance details Defined in Text.JSON.Types Methods showsPrec :: Int -> JSValue -> ShowS # show :: JSValue -> String # showList :: [JSValue] -> ShowS #
Eq JSValue # |
Instance details Defined in Text.JSON.Types Methods (==) :: JSValue -> JSValue -> Bool # (/=) :: JSValue -> JSValue -> Bool #
Ord JSValue # |
Instance details Defined in Text.JSON.Types Methods compare :: JSValue -> JSValue -> Ordering # (<) :: JSValue -> JSValue -> Bool # (<=) :: JSValue -> JSValue -> Bool # (>) :: JSValue -> JSValue -> Bool # (>=) :: JSValue -> JSValue -> Bool # max :: JSValue -> JSValue -> JSValue # min :: JSValue -> JSValue -> JSValue #
JSON JSValue # | To ensure we generate valid JSON, we map Haskell types to JSValue internally, then pretty print that.
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result JSValue # showJSON :: JSValue -> JSValue # readJSONs :: JSValue -> Result [JSValue] # showJSONs :: [JSValue] -> JSValue #

# Wrapper Types

newtype JSString #

Strings can be represented a little more efficiently in JSON

Constructors

JSONString |
Fields fromJSString :: String

#### Instances

Instances details

IsString JSString # |
Instance details Defined in Text.JSON.Types Methods fromString :: String -> JSString #
Read JSString # |
Instance details Defined in Text.JSON.Types Methods readsPrec :: Int -> ReadS JSString # readList :: ReadS [JSString] # readPrec :: ReadPrec JSString # readListPrec :: ReadPrec [JSString] #
Show JSString # |
Instance details Defined in Text.JSON.Types Methods showsPrec :: Int -> JSString -> ShowS # show :: JSString -> String # showList :: [JSString] -> ShowS #
Eq JSString # |
Instance details Defined in Text.JSON.Types Methods (==) :: JSString -> JSString -> Bool # (/=) :: JSString -> JSString -> Bool #
Ord JSString # |
Instance details Defined in Text.JSON.Types Methods compare :: JSString -> JSString -> Ordering # (<) :: JSString -> JSString -> Bool # (<=) :: JSString -> JSString -> Bool # (>) :: JSString -> JSString -> Bool # (>=) :: JSString -> JSString -> Bool # max :: JSString -> JSString -> JSString # min :: JSString -> JSString -> JSString #
JSKey JSString # |
Instance details Defined in Text.JSON Methods toJSKey :: JSString -> String # fromJSKey :: String -> Maybe JSString #
JSON JSString # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result JSString # showJSON :: JSString -> JSValue # readJSONs :: JSValue -> Result [JSString] # showJSONs :: [JSString] -> JSValue #

toJSString :: String -> JSString #

Turn a Haskell string into a JSON string.

newtype JSObject e #

As can association lists

Constructors

JSONObject |
Fields fromJSObject :: [(String, e)]

#### Instances

Instances details

Read e => Read (JSObject e) # |
Instance details Defined in Text.JSON.Types Methods readsPrec :: Int -> ReadS (JSObject e) # readList :: ReadS [JSObject e] # readPrec :: ReadPrec (JSObject e) # readListPrec :: ReadPrec [JSObject e] #
Show e => Show (JSObject e) # |
Instance details Defined in Text.JSON.Types Methods showsPrec :: Int -> JSObject e -> ShowS # show :: JSObject e -> String # showList :: [JSObject e] -> ShowS #
Eq e => Eq (JSObject e) # |
Instance details Defined in Text.JSON.Types Methods (==) :: JSObject e -> JSObject e -> Bool # (/=) :: JSObject e -> JSObject e -> Bool #
Ord e => Ord (JSObject e) # |
Instance details Defined in Text.JSON.Types Methods compare :: JSObject e -> JSObject e -> Ordering # (<) :: JSObject e -> JSObject e -> Bool # (<=) :: JSObject e -> JSObject e -> Bool # (>) :: JSObject e -> JSObject e -> Bool # (>=) :: JSObject e -> JSObject e -> Bool # max :: JSObject e -> JSObject e -> JSObject e # min :: JSObject e -> JSObject e -> JSObject e #
JSON a => JSON (JSObject a) # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result (JSObject a) # showJSON :: JSObject a -> JSValue # readJSONs :: JSValue -> Result [JSObject a] # showJSONs :: [JSObject a] -> JSValue #

toJSObject :: [(String, a)] -> JSObject a #

Make JSON object out of an association list.

get_field :: JSObject a -> String -> Maybe a #

Get the value of a field, if it exist.

set_field :: JSObject a -> String -> a -> JSObject a #

Set the value of a field. Previous values are overwritten.