waratuman / elm-json-extra / Json.Encode.Extra

Helper functions for the Json.Decode module of elm/json.

iso8601 : Time.Posix -> Json.Encode.Value

Encode a Posix value into a ISO8601 JSON string.

import Json.Encode exposing (..)
import Time

encode 0
    ( object
        [ ( "created_at", iso8601 (Time.millisToPosix 1574447205394 ) ) ]
    )
    --> "{\"created_at\":\"2019-11-22T18:26:45.394Z\"}"

posix : Time.Posix -> Json.Encode.Value

Encode a Posix value into a JSON float representing the number of seconds since epoch.

import Json.Encode exposing (..)
import Time

encode 0
    ( object
        [ ( "created_at", posix (Time.millisToPosix 1574447205 ) ) ]
    )
    --> "{\"created_at\":1574447.205}"

timezone : ( String, Time.Zone ) -> Json.Encode.Value

Encode a ( String, Zone ) value into a JSON string with the value being the name of the timezone.

import Json.Encode exposing (..)
import TimeZone

let
    losAngeles =
        ("America/Los_Angeles", TimeZone.america__los_angeles ())
in
encode 0
    ( object
        [ ( "timezone", timezone losAngeles ) ]
    )
    --> "{\"timezone\":\"America/Los_Angeles\"}"

maybe : (a -> Json.Encode.Value) -> Maybe a -> Json.Encode.Value

Encode a Maybe value with the given encoder.

import Json.Encode exposing (..)

encode 0 (maybe int Nothing)
    --> "null"

encode 0 (maybe int (Just 1))
    --> "1"