Library for turning Elm values into Json values.
Represents a JavaScript value.
Turn a String into a JSON string.
import Json.Encode exposing (encode, string)
-- encode 0 (string "") == "\"\""
-- encode 0 (string "abc") == "\"abc\""
-- encode 0 (string "hello") == "\"hello\""Turn an Int into a JSON number.
import Json.Encode exposing (encode, int)
-- encode 0 (int 42) == "42"
-- encode 0 (int -7) == "-7"
-- encode 0 (int 0) == "0"Turn a Float into a JSON number.
import Json.Encode exposing (encode, float)
-- encode 0 (float 3.14) == "3.14"
-- encode 0 (float 1.618) == "1.618"
-- encode 0 (float -42) == "-42"
-- encode 0 (float NaN) == "null"
-- encode 0 (float Infinity) == "null"Note: Floating point numbers are defined in the IEEE 754 standard
which is hardcoded into almost all CPUs. This standard allows Infinity and
NaN. The JSON spec does not include these values, so we encode them
both as null.
Turn a Bool into a JSON boolean.
import Json.Encode exposing (encode, bool)
-- encode 0 (bool True) == "true"
-- encode 0 (bool False) == "false"Create a JSON null value.
import Json.Encode exposing (encode, null)
-- encode 0 null == "null"Turn a List into a JSON array.
import Json.Encode as Encode exposing (bool, encode, int, list, string)
-- encode 0 (list int [1,3,4]) == "[1,3,4]"
-- encode 0 (list bool [True,False]) == "[true,false]"
-- encode 0 (list string ["a","b"]) == """["a","b"]"""Create a JSON object.
import Json.Encode as Encode
tom : Encode.Value
tom =
Encode.object
[ ( "name", Encode.string "Tom" )
, ( "age", Encode.int 42 )
]
-- Encode.encode 0 tom == """{"name":"Tom","age":42}"""Turn a Dict into a JSON object.
import Dict exposing (Dict)
import Json.Encode as Encode
people : Dict String Int
people =
Dict.fromList [ ("Tom",42), ("Sue", 38) ]
-- Encode.encode 0 (Encode.dict identity Encode.int people)
-- == """{"Tom":42,"Sue":38}"""
Convert a
Valueinto a prettified string. The first argument specifies the amount of indentation in the resulting string.