Helper functions for the Json.Decode
module of elm/json
.
nothing : Json.Decode.Decoder ()
Decode a JSON value and do nothing with it.
import Json.Decode exposing (..)
decodeString nothing "{}" --> Ok ()
date : Json.Decode.Decoder ( Basics.Int, Time.Month, Basics.Int )
Decode an ISO8601 JSON string into a Posix
.
import Json.Decode exposing (..)
import Time
decodeString (field "created_at" date) "{ \"created_at\": \"2019-11-22\"}"
--> Ok (2019, Time.Nov, 22)
iso8601 : Json.Decode.Decoder Time.Posix
Decode an ISO8601 JSON string into a Posix
.
import Json.Decode exposing (..)
import Time
decodeString (field "created_at" iso8601)
"{ \"created_at\": \"2019-11-22T18:26:45Z\"}"
--> Ok (Time.millisToPosix 1574447205000)
posix : Json.Decode.Decoder Time.Posix
Decode a JSON float value representing the number of seconds since epoch
into a Posix
.
import Json.Decode exposing (..)
import Time
decodeString (field "created_at" posix)
"{ \"created_at\": 1574447205.394}"
--> Ok (Time.millisToPosix 1574447205000)
timezone : Json.Decode.Decoder ( String, Time.Zone )
Decode a timezone JSON string value into a Zone
.
import Json.Decode exposing (..)
import TimeZone
decodeString (field "timezone" timezone)
"{ \"timezone\": \"America/Los_Angeles\"}"
--> Ok ("America/Los_Angeles", TimeZone.america__los_angeles ())