Encode.md

May 18, 2018 ยท View on GitHub

Module Elm.Json.Encode

A library for turning Elm values into Json values.

This is mainly implemented via the toForeign method in purescript-foreign.

You could also consider the purescript-argonaut-* modules.

Value

type Value = Foreign

Represents a JavaScript value.

encode

encode :: Int -> Value -> String

Convert a Value into a prettified string. The first argument specifies the amount of indentation in the resulting string.

person =
    object
      [ Tuple "name" (string "Tom")
      , Tuple "age" (int 42)
      ]

compact = encode 0 person
-- {"name":"Tom","age":42}

readable = encode 4 person
-- {
--     "name": "Tom",
--     "age": 42
-- }

string

string :: String -> Value

Turn a String into a Value.

int

int :: Int -> Value

Turn an Int into a Value.

float

float :: Float -> Value

Encode a Float. Infinity and NaN are encoded as null.

bool

bool :: Bool -> Value

Encode a Bool.

null

null :: Value

Encode a null value.

list

list :: List Value -> Value

Encode a List.

array

array :: forall f. Foldable f => f Value -> Value

Encode an array type. Uses a polymorphic type in order to accommodate Purescript Array and Elm.Array, among others.

object

object :: forall f. Foldable f => f (Tuple String Value) -> Value

Encode a JSON object.

The signature uses Foldable in order to work with List or Array, amongst others.