Module

Data.Argonaut.Decode.Combinators

Package
purescript-argonaut-codecs
Repository
purescript-contrib/purescript-argonaut-codecs

#getField Source

getField :: forall a. DecodeJson a => Object Json -> String -> Either String 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.

#getFieldDeprecated Source

getFieldDeprecated :: forall a. Warn (Text "`.?` is deprecated, use `.:` instead") => DecodeJson a => Object Json -> String -> Either String a

#getFieldOptional Source

getFieldOptional :: forall a. DecodeJson a => Object Json -> String -> Either String (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.

#getFieldOptionalDeprecated Source

getFieldOptionalDeprecated :: forall a. Warn (Text "`.??` is deprecated, use `.:!` or `.:?` instead") => DecodeJson a => Object Json -> String -> Either String (Maybe a)

#getFieldOptional' Source

getFieldOptional' :: forall a. DecodeJson a => Object Json -> String -> Either String (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.

#defaultField Source

defaultField :: forall a. Either String (Maybe a) -> a -> Either String 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 }

#defaultFieldDeprecated Source

defaultFieldDeprecated :: forall a. Warn (Text "`.?=` is deprecated, use `.!=` instead") => Either String (Maybe a) -> a -> Either String a

#(.:) Source

Operator alias for Data.Argonaut.Decode.Combinators.getField (non-associative / precedence 7)

#(.?) Source

Operator alias for Data.Argonaut.Decode.Combinators.getFieldDeprecated (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.getFieldOptional' (non-associative / precedence 7)

#(.??) Source

Operator alias for Data.Argonaut.Decode.Combinators.getFieldOptionalDeprecated (non-associative / precedence 7)

#(.!=) Source

Operator alias for Data.Argonaut.Decode.Combinators.defaultField (non-associative / precedence 6)

#(.?=) Source

Operator alias for Data.Argonaut.Decode.Combinators.defaultFieldDeprecated (non-associative / precedence 6)