circuithub/elm-json-extra/1.0.3

NOTE: this package is not compatible with Elm 0.19.1

Json.Decode.Extra

Convenience functions for working with Json

Date

date : Decoder Date

Extract a date.

Note that this function is not total, it will throw an exception given an incorrectly formatted date. See 'Date.fromString' and 'Json.customDecoder'.

Incremental Decoding

andApply : Decoder (a -> b) -> Decoder a -> Decoder b

Can be helpful when decoding large objects incrementally.

import Date (Date)


type alias User =
  { id                : Int
  , createdAt         : Date
  , updatedAt         : Date
  , deletedAt         : Maybe Date
  , username          : Maybe String
  , email             : Maybe String
  , fullname          : Maybe String
  , avatar            : Maybe String
  , isModerator       : Bool
  , isOrganization    : Bool
  , isAdmin           : Bool
  }


metaDecoder : (Int -> Date -> Date -> Maybe Date -> b) -> Decoder b
metaDecoder f = f
  `map`      ("id"        := int)
  `andApply` ("createdAt" := date)
  `andApply` ("updatedAt" := date)
  `andApply` ("deletedAt" := maybe date)


userDecoder : Decoder User
userDecoder = metaDecoder User
  `andApply` ("username"          := maybe string)
  `andApply` ("email"             := maybe string)
  `andApply` ("fullname"          := maybe string)
  `andApply` ("avatar"            := maybe string)
  `andApply` ("isModerator"       := bool)
  `andApply` ("isOrganization"    := bool)
  `andApply` ("isAdmin"           := bool)

This is a shortened form of

metaDecoder : (Int -> Date -> Date -> Maybe Date -> b) -> Decoder b
metaDecoder f = f
  `map`      ("id"        := int)
  `andThen` \f -> f `map` ("createdAt" := date)
  `andThen` \f -> f `map` ("updatedAt" := date)
  `andThen` \f -> f `map` ("deletedAt" := maybe date)


userDecoder : Decoder User
userDecoder = metaDecoder User
  `andThen` \f -> f `map` ("username"          := maybe string)
  `andThen` \f -> f `map` ("email"             := maybe string)
  `andThen` \f -> f `map` ("fullname"          := maybe string)
  `andThen` \f -> f `map` ("avatar"            := maybe string)
  `andThen` \f -> f `map` ("isModerator"       := bool)
  `andThen` \f -> f `map` ("isOrganization"    := bool)
  `andThen` \f -> f `map` ("isAdmin"           := bool)