ghcjs-base-0.2.0.0: base library for GHCJS

Copyright(c) 2011-2013 Bryan O'Sullivan (c) 2011 MailRank, Inc.
LicenseApache
MaintainerBryan O'Sullivan <bos@serpentine.com>
Stabilityexperimental
Portabilityportable
Safe HaskellNone
LanguageHaskell98

JavaScript.JSON.Types.Instances

Contents

Description

Types for working with JSON data.

Synopsis

Type classes

Core JSON classes

class FromJSON a where Source

Minimal complete definition

Nothing

Methods

parseJSON :: Value -> Parser a Source

class ToJSON a where Source

Minimal complete definition

Nothing

Methods

toJSON :: a -> Value Source

Generic JSON classes

class GFromJSON f where Source

Methods

gParseJSON :: Options -> Value -> Parser (f a) Source

class GToJSON f where Source

Methods

gToJSON :: Options -> f a -> Value Source

Types

newtype DotNetTime :: *

A newtype wrapper for UTCTime that uses the same non-standard serialization format as Microsoft .NET, whose System.DateTime type is by default serialized to JSON as in the following example:

/Date(1302547608878)/

The number represents milliseconds since the Unix epoch.

Constructors

DotNetTime 

Fields

fromDotNetTime :: UTCTime

Acquire the underlying value.

Inspecting Values

withObject :: String -> (Object -> Parser a) -> Value -> Parser a Source

withObject expected f value applies f to the Object when value is an Object and fails using typeMismatch expected otherwise.

withArray :: String -> (JSArray -> Parser a) -> Value -> Parser a Source

withArray expected f value applies f to the Array when value is an Array and fails using typeMismatch expected otherwise.

withDouble :: String -> (Double -> Parser a) -> Value -> Parser a Source

withScientific expected f value applies f to the Double number when value is a Number. and fails using typeMismatch expected otherwise.

withBool :: String -> (Bool -> Parser a) -> Value -> Parser a Source

withBool expected f value applies f to the Bool when value is a Bool and fails using typeMismatch expected otherwise.

Functions

fromJSON :: FromJSON a => Value -> Result a Source

Convert a value from JSON, failing if the types do not match.

(.:) :: FromJSON a => Object -> JSString -> Parser a Source

Retrieve the value associated with the given key of an Object. The result is empty if the key is not present or the value cannot be converted to the desired type.

This accessor is appropriate if the key and value must be present in an object for it to be valid. If the key and value are optional, use '(.:?)' instead.

(.:?) :: FromJSON a => Object -> JSString -> Parser (Maybe a) Source

Retrieve the value associated with the given key of an Object. The result is Nothing if the key is not present, or empty if the value cannot be converted to the desired type.

This accessor is most useful if the key and value can be absent from an object without affecting its validity. If the key and value are mandatory, use '(.:)' instead.

(.!=) :: Parser (Maybe a) -> a -> Parser a Source

Helper for use in combination with .:? to provide default values for optional JSON object fields.

This combinator is most useful if the key and value can be absent from an object without affecting its validity and we know a default value to assign in that case. If the key and value are mandatory, use '(.:)' instead.

Example usage:

 v1 <- o .:? "opt_field_with_dfl" .!= "default_val"
 v2 <- o .:  "mandatory_field"
 v3 <- o .:? "opt_field2"

(.=) :: ToJSON a => JSString -> a -> Pair Source

Construct a Pair from a key and a value.

typeMismatch Source

Arguments

:: String

The name of the type you are trying to parse.

-> Value

The actual value encountered.

-> Parser a 

Fail parsing due to a type mismatch, with a descriptive message.