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

Safe Haskell | Safe-Inferred
Language | Haskell2010

Text.JSON

Contents

- JSON Types
- Serialization to and from JSValues
- Encoding and Decoding
- Wrapper Types
- Serialization to and from Strings.
  - Reading JSON
  - Writing JSON
  - Instance helpers

Description

Serialising Haskell values to and from JSON values.

Synopsis

- data JSValue
  - = JSNull
  - | JSBool !Bool
  - | JSRational Bool !Rational
  - | JSString JSString
  - | JSArray [JSValue]
  - | JSObject (JSObject JSValue)
- class JSON a where
  - readJSON :: JSValue -> Result a
  - showJSON :: a -> JSValue
  - readJSONs :: JSValue -> Result [a]
  - showJSONs :: [a] -> JSValue
- data Result a
  - = Ok a
  - | Error String
- 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
  - toJSKey :: a -> String
  - fromJSKey :: String -> Maybe a
- encJSDict :: (JSKey a, JSON b) => [(a, b)] -> JSValue
- decJSDict :: (JSKey a, JSON b) => String -> JSValue -> Result [(a, b)]

# JSON Types

data JSValue Source #

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

# Serialization to and from JSValues

class JSON a where Source #

The class of types serialisable to and from JSON

Minimal complete definition

readJSON, showJSON

Methods

readJSON :: JSValue -> Result a Source #

showJSON :: a -> JSValue Source #

readJSONs :: JSValue -> Result [a] Source #

showJSONs :: [a] -> JSValue Source #

#### Instances

Instances details

JSON ByteString Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result ByteString Source # showJSON :: ByteString -> JSValue Source # readJSONs :: JSValue -> Result [ByteString] Source # showJSONs :: [ByteString] -> JSValue Source #
JSON ByteString Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result ByteString Source # showJSON :: ByteString -> JSValue Source # readJSONs :: JSValue -> Result [ByteString] Source # showJSONs :: [ByteString] -> JSValue Source #
JSON IntSet Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result IntSet Source # showJSON :: IntSet -> JSValue Source # readJSONs :: JSValue -> Result [IntSet] Source # showJSONs :: [IntSet] -> JSValue Source #
JSON Int16 Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result Int16 Source # showJSON :: Int16 -> JSValue Source # readJSONs :: JSValue -> Result [Int16] Source # showJSONs :: [Int16] -> JSValue Source #
JSON Int32 Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result Int32 Source # showJSON :: Int32 -> JSValue Source # readJSONs :: JSValue -> Result [Int32] Source # showJSONs :: [Int32] -> JSValue Source #
JSON Int64 Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result Int64 Source # showJSON :: Int64 -> JSValue Source # readJSONs :: JSValue -> Result [Int64] Source # showJSONs :: [Int64] -> JSValue Source #
JSON Int8 Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result Int8 Source # showJSON :: Int8 -> JSValue Source # readJSONs :: JSValue -> Result [Int8] Source # showJSONs :: [Int8] -> JSValue Source #
JSON Word16 Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result Word16 Source # showJSON :: Word16 -> JSValue Source # readJSONs :: JSValue -> Result [Word16] Source # showJSONs :: [Word16] -> JSValue Source #
JSON Word32 Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result Word32 Source # showJSON :: Word32 -> JSValue Source # readJSONs :: JSValue -> Result [Word32] Source # showJSONs :: [Word32] -> JSValue Source #
JSON Word64 Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result Word64 Source # showJSON :: Word64 -> JSValue Source # readJSONs :: JSValue -> Result [Word64] Source # showJSONs :: [Word64] -> JSValue Source #
JSON Word8 Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result Word8 Source # showJSON :: Word8 -> JSValue Source # readJSONs :: JSValue -> Result [Word8] Source # showJSONs :: [Word8] -> JSValue Source #
JSON Ordering Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result Ordering Source # showJSON :: Ordering -> JSValue Source # readJSONs :: JSValue -> Result [Ordering] Source # showJSONs :: [Ordering] -> JSValue Source #
JSON JSString Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result JSString Source # showJSON :: JSString -> JSValue Source # readJSONs :: JSValue -> Result [JSString] Source # showJSONs :: [JSString] -> JSValue Source #
JSON JSValue Source # | 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 Source # showJSON :: JSValue -> JSValue Source # readJSONs :: JSValue -> Result [JSValue] Source # showJSONs :: [JSValue] -> JSValue Source #
JSON Text Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result Text Source # showJSON :: Text -> JSValue Source # readJSONs :: JSValue -> Result [Text] Source # showJSONs :: [Text] -> JSValue Source #
JSON Integer Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result Integer Source # showJSON :: Integer -> JSValue Source # readJSONs :: JSValue -> Result [Integer] Source # showJSONs :: [Integer] -> JSValue Source #
JSON () Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result () Source # showJSON :: () -> JSValue Source # readJSONs :: JSValue -> Result [()] Source # showJSONs :: [()] -> JSValue Source #
JSON Bool Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result Bool Source # showJSON :: Bool -> JSValue Source # readJSONs :: JSValue -> Result [Bool] Source # showJSONs :: [Bool] -> JSValue Source #
JSON Char Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result Char Source # showJSON :: Char -> JSValue Source # readJSONs :: JSValue -> Result [Char] Source # showJSONs :: [Char] -> JSValue Source #
JSON Double Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result Double Source # showJSON :: Double -> JSValue Source # readJSONs :: JSValue -> Result [Double] Source # showJSONs :: [Double] -> JSValue Source #
JSON Float Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result Float Source # showJSON :: Float -> JSValue Source # readJSONs :: JSValue -> Result [Float] Source # showJSONs :: [Float] -> JSValue Source #
JSON Int Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result Int Source # showJSON :: Int -> JSValue Source # readJSONs :: JSValue -> Result [Int] Source # showJSONs :: [Int] -> JSValue Source #
JSON Word Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result Word Source # showJSON :: Word -> JSValue Source # readJSONs :: JSValue -> Result [Word] Source # showJSONs :: [Word] -> JSValue Source #
JSON a => JSON (IntMap a) Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result (IntMap a) Source # showJSON :: IntMap a -> JSValue Source # readJSONs :: JSValue -> Result [IntMap a] Source # showJSONs :: [IntMap a] -> JSValue Source #
(Ord a, JSON a) => JSON (Set a) Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result (Set a) Source # showJSON :: Set a -> JSValue Source # readJSONs :: JSValue -> Result [Set a] Source # showJSONs :: [Set a] -> JSValue Source #
JSON a => JSON (JSObject a) Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result (JSObject a) Source # showJSON :: JSObject a -> JSValue Source # readJSONs :: JSValue -> Result [JSObject a] Source # showJSONs :: [JSObject a] -> JSValue Source #
JSON a => JSON (Maybe a) Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result (Maybe a) Source # showJSON :: Maybe a -> JSValue Source # readJSONs :: JSValue -> Result [Maybe a] Source # showJSONs :: [Maybe a] -> JSValue Source #
JSON a => JSON [a] Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result [a] Source # showJSON :: [a] -> JSValue Source # readJSONs :: JSValue -> Result [[a]] Source # showJSONs :: [[a]] -> JSValue Source #
(Ord a, JSON a, JSON b) => JSON (Map a b) Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result (Map a b) Source # showJSON :: Map a b -> JSValue Source # readJSONs :: JSValue -> Result [Map a b] Source # showJSONs :: [Map a b] -> JSValue Source #
(Ix i, JSON i, JSON e) => JSON (Array i e) Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result (Array i e) Source # showJSON :: Array i e -> JSValue Source # readJSONs :: JSValue -> Result [Array i e] Source # showJSONs :: [Array i e] -> JSValue Source #
(JSON a, JSON b) => JSON (Either a b) Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result (Either a b) Source # showJSON :: Either a b -> JSValue Source # readJSONs :: JSValue -> Result [Either a b] Source # showJSONs :: [Either a b] -> JSValue Source #
(JSON a, JSON b) => JSON (a, b) Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result (a, b) Source # showJSON :: (a, b) -> JSValue Source # readJSONs :: JSValue -> Result [(a, b)] Source # showJSONs :: [(a, b)] -> JSValue Source #
(JSON a, JSON b, JSON c) => JSON (a, b, c) Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result (a, b, c) Source # showJSON :: (a, b, c) -> JSValue Source # readJSONs :: JSValue -> Result [(a, b, c)] Source # showJSONs :: [(a, b, c)] -> JSValue Source #
(JSON a, JSON b, JSON c, JSON d) => JSON (a, b, c, d) Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result (a, b, c, d) Source # showJSON :: (a, b, c, d) -> JSValue Source # readJSONs :: JSValue -> Result [(a, b, c, d)] Source # showJSONs :: [(a, b, c, d)] -> JSValue Source #

# Encoding and Decoding

data Result a Source #

A type for parser results

Constructors

Ok a |
Error String |

#### Instances

Instances details

Alternative Result Source # |
Instance details Defined in Text.JSON Methods empty :: Result a Source # (<|>) :: Result a -> Result a -> Result a Source # some :: Result a -> Result [a] Source # many :: Result a -> Result [a] Source #
Applicative Result Source # |
Instance details Defined in Text.JSON Methods pure :: a -> Result a Source # (<*>) :: Result (a -> b) -> Result a -> Result b Source # liftA2 :: (a -> b -> c) -> Result a -> Result b -> Result c Source # (*>) :: Result a -> Result b -> Result b Source # (<*) :: Result a -> Result b -> Result a Source #
Functor Result Source # |
Instance details Defined in Text.JSON Methods fmap :: (a -> b) -> Result a -> Result b Source # (<$) :: a -> Result b -> Result a Source #
Monad Result Source # |
Instance details Defined in Text.JSON Methods (>>=) :: Result a -> (a -> Result b) -> Result b Source # (>>) :: Result a -> Result b -> Result b Source # return :: a -> Result a Source #
MonadPlus Result Source # |
Instance details Defined in Text.JSON Methods mzero :: Result a Source # mplus :: Result a -> Result a -> Result a Source #
MonadFail Result Source # |
Instance details Defined in Text.JSON Methods fail :: String -> Result a Source #
Show a => Show (Result a) Source # |
Instance details Defined in Text.JSON Methods showsPrec :: Int -> Result a -> ShowS Source # show :: Result a -> String Source # showList :: [Result a] -> ShowS Source #
Eq a => Eq (Result a) Source # |
Instance details Defined in Text.JSON Methods (==) :: Result a -> Result a -> Bool Source # (/=) :: Result a -> Result a -> Bool Source #

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

data JSString Source #

Strings can be represented a little more efficiently in JSON

#### Instances

Instances details

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

toJSString :: String -> JSString Source #

Turn a Haskell string into a JSON string.

fromJSString :: JSString -> String Source #

data JSObject e Source #

As can association lists

#### Instances

Instances details

Read e => Read (JSObject e) Source # |
Instance details Defined in Text.JSON.Types Methods readsPrec :: Int -> ReadS (JSObject e) Source # readList :: ReadS [JSObject e] Source # readPrec :: ReadPrec (JSObject e) Source # readListPrec :: ReadPrec [JSObject e] Source #
Show e => Show (JSObject e) Source # |
Instance details Defined in Text.JSON.Types Methods showsPrec :: Int -> JSObject e -> ShowS Source # show :: JSObject e -> String Source # showList :: [JSObject e] -> ShowS Source #
Eq e => Eq (JSObject e) Source # |
Instance details Defined in Text.JSON.Types Methods (==) :: JSObject e -> JSObject e -> Bool Source # (/=) :: JSObject e -> JSObject e -> Bool Source #
Ord e => Ord (JSObject e) Source # |

Instance details

Defined in Text.JSON.Types

Methods

compare :: JSObject e -> JSObject e -> Ordering Source #

(<) :: JSObject e -> JSObject e -> Bool Source #

(<=) :: JSObject e -> JSObject e -> Bool Source #

(>) :: JSObject e -> JSObject e -> Bool Source #

(>=) :: JSObject e -> JSObject e -> Bool Source #

max :: JSObject e -> JSObject e -> JSObject e Source #

min :: JSObject e -> JSObject e -> JSObject e Source #

JSON a => JSON (JSObject a) Source # |
Instance details Defined in Text.JSON Methods readJSON :: JSValue -> Result (JSObject a) Source # showJSON :: JSObject a -> JSValue Source # readJSONs :: JSValue -> Result [JSObject a] Source # showJSONs :: [JSObject a] -> JSValue Source #

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

Make JSON object out of an association list.

fromJSObject :: JSObject e -> [(String, e)] Source #

resultToEither :: Result a -> Either String a Source #

Map Results to Eithers

# 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

showJSRational' :: Bool -> Rational -> ShowS Source #

showJSObject :: JSObject JSValue -> ShowS Source #

Show an association list in JSON format

showJSValue :: JSValue -> ShowS Source #

Show JSON values

## Instance helpers

makeObj :: [(String, JSValue)] -> JSValue Source #

valFromObj :: JSON a => String -> JSObject JSValue -> Result a Source #

Pull a value out of a JSON object.

class JSKey a where Source #

Haskell types that can be used as keys in JSON objects.

Methods

toJSKey :: a -> String Source #

fromJSKey :: String -> Maybe a Source #

#### Instances

Instances details

JSKey JSString Source # |
Instance details Defined in Text.JSON Methods toJSKey :: JSString -> String Source # fromJSKey :: String -> Maybe JSString Source #
JSKey String Source # |
Instance details Defined in Text.JSON Methods toJSKey :: String -> String Source # fromJSKey :: String -> Maybe String Source #
JSKey Int Source # |
Instance details Defined in Text.JSON Methods toJSKey :: Int -> String Source # fromJSKey :: String -> Maybe Int Source #

encJSDict :: (JSKey a, JSON b) => [(a, b)] -> JSValue Source #

Encode an association list as `JSObject` value.

decJSDict :: (JSKey a, JSON b) => String -> JSValue -> Result [(a, b)] Source #

Decode a `JSObject` value into an association list.