When attempting to decode larger json values, I've ran into the following code in the Json-Decode-Extra
library. (located here)
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)
`apply` ("createdAt" := date)
`apply` ("updatedAt" := date)
`apply` ("deletedAt" := maybe date)
userDecoder : Decoder User
userDecoder = metaDecoder User
`apply` ("username" := maybe string)
`apply` ("email" := maybe string)
`apply` ("fullname" := maybe string)
`apply` ("avatar" := maybe string)
`apply` ("isModerator" := bool)
`apply` ("isOrganization" := bool)
`apply` ("isAdmin" := bool)
However, I'm constantly running into a compiler error for the :=
operator. Where is this defined? The JSON decode tutorials don't explicitly import this operator anywhere.
In Elm 0.18, the
:=
operator was replaced byJson.Decode.field
, and using backticks for infix operators was removed.You are using a package (circuithub/elm-json-extra) which has not been updated to Elm 0.18.
Consider switching to use the package maintained by the Elm community:
elm-community/json-extra
. You can useandMap
instead ofapply
. Here is your example code upgraded to the new library and Elm 0.18:Note that the
elm-community/json-extra
package also exports an infix operator|:
which is an infix version ofandMap
. You could use this to make your code more concise. For example: