Data.Argonaut.Decode
- Package
- purescript-argonaut-codecs
- Repository
- purescript-contrib/purescript-argonaut-codecs
#fromJsonString Source
fromJsonString :: forall json. DecodeJson json => String -> Either JsonDecodeError json
Parse and decode a json in one step.
Re-exports from Data.Argonaut.Decode.Class
#DecodeJson Source
class DecodeJson a where
Members
decodeJson :: Json -> Either JsonDecodeError a
Instances
(DecodeJson a) => DecodeJson (Identity a)
(DecodeJson a) => DecodeJson (Maybe a)
(DecodeJson a, DecodeJson b) => DecodeJson (Tuple a b)
(DecodeJson a, DecodeJson b) => DecodeJson (Either a b)
DecodeJson Unit
DecodeJson Boolean
DecodeJson Number
DecodeJson Int
DecodeJson String
DecodeJson NonEmptyString
DecodeJson Json
(DecodeJson a) => DecodeJson (NonEmpty Array a)
(DecodeJson a) => DecodeJson (NonEmptyArray a)
(DecodeJson a) => DecodeJson (NonEmpty List a)
(DecodeJson a) => DecodeJson (NonEmptyList a)
DecodeJson CodePoint
(DecodeJson a) => DecodeJson (Object a)
(DecodeJson a) => DecodeJson (Array a)
(DecodeJson a) => DecodeJson (List a)
(Ord a, DecodeJson a) => DecodeJson (Set a)
(Ord a, DecodeJson a, DecodeJson b) => DecodeJson (Map a b)
DecodeJson Void
(GDecodeJson row list, RowToList row list) => DecodeJson (Record row)
Re-exports from Data.Argonaut.Decode.Combinators
#getFieldOptional' Source
getFieldOptional' :: forall a. DecodeJson a => Object Json -> String -> Either JsonDecodeError (Maybe a)
Attempt to get the value for a given key on an Object Json
.
The result will be Right Nothing
if the key and value are not present,
or if the key is present and the value is null
.
Use this accessor if the key and value are optional in your object.
If the key and value are mandatory, use getField
(.:
) instead.
#getFieldOptional Source
getFieldOptional :: forall a. DecodeJson a => Object Json -> String -> Either JsonDecodeError (Maybe a)
Attempt to get the value for a given key on an Object Json
.
The result will be Right Nothing
if the key and value are not present,
but will fail if the key is present but the value cannot be converted to the right type.
This function will treat null
as a value and attempt to decode it into your desired type.
If you would like to treat null
values the same as absent values, use
getFieldOptional'
(.:?
) instead.
#getField Source
getField :: forall a. DecodeJson a => Object Json -> String -> Either JsonDecodeError a
Attempt to get the value for a given key on an Object Json
.
Use this accessor if the key and value must be present in your object.
If the key and value are optional, use getFieldOptional'
(.:?
) instead.
#defaultField Source
defaultField :: forall a. Either JsonDecodeError (Maybe a) -> a -> Either JsonDecodeError a
Helper for use in combination with .:?
to provide default values for optional
Object Json
fields.
Example usage:
newtype MyType = MyType
{ foo :: String
, bar :: Maybe Int
, baz :: Boolean
}
instance decodeJsonMyType :: DecodeJson MyType where
decodeJson json = do
x <- decodeJson json
foo <- x .: "foo" -- mandatory field
bar <- x .:? "bar" -- optional field
baz <- x .:? "baz" .!= false -- optional field with default value of `false`
pure $ MyType { foo, bar, baz }
#(.:?) Source
Operator alias for Data.Argonaut.Decode.Combinators.getFieldOptional' (non-associative / precedence 7)
#(.:!) Source
Operator alias for Data.Argonaut.Decode.Combinators.getFieldOptional (non-associative / precedence 7)
#(.:) Source
Operator alias for Data.Argonaut.Decode.Combinators.getField (non-associative / precedence 7)
#(.!=) Source
Operator alias for Data.Argonaut.Decode.Combinators.defaultField (non-associative / precedence 6)
Re-exports from Data.Argonaut.Decode.Error
#JsonDecodeError Source
data JsonDecodeError
Error type for failures while decoding.
Constructors
TypeMismatch String
UnexpectedValue Json
AtIndex Int JsonDecodeError
AtKey String JsonDecodeError
Named String JsonDecodeError
MissingValue
Instances
#printJsonDecodeError Source
printJsonDecodeError :: JsonDecodeError -> String
Prints a JsonDecodeError
as a readable error message.