Orasund/elm-jsonstore/2.0.0

NOTE: the latest version is 2.0.1

Jsonstore

Decoding and Encoding

type Json a

The Json type combines both the Json Decoder and Encoder.

decode : Json a -> Decoder a

Returns the decoder of a Json type

encode : Json a -> a -> Value

Returns the encoder of a Json type

encodeList : Json a -> List a -> Value

Returns the encoder for a List of a Json type

decodeList : Json a -> Decoder (List a)

Returns the decoder for a List of a Json type

map : (a -> b) -> (b -> a) -> Json a -> Json b

Basics

bool : Json Bool
import Json.Decode as D


"true"
|> (bool |> decode |> D.decodeString)
--> Ok True
int : Json Int
import Json.Decode as D


"1" |> (int |> decode |> D.decodeString) --> Ok 1
float : Json Float
import Json.Decode as D


"3.14" |> (float |> decode |> D.decodeString) --> Ok 3.14
string : Json String
import Json.Decode as D


"\"Hello World\""
|> (string |> decode |> D.decodeString)
--> Ok "Hello World"
dict : Json a -> Json (Dict String a)
import Json.Decode as D
import Dict exposing (Dict)


"{\"value\":42}"
|> (int |> dict |> decode |> D.decodeString)
--> Ok (Dict.singleton "value" 42)

Dealing with Objects

object : obj -> JsonObject obj a
import Json.Decode as D


type alias Obj =
    { value : Int
    , name : String
    }


"{\"value\":42,\"name\":\"Elm\"}"
|> ( object Obj
    |> with "value" int .value
    |> with "name" string .name
    |> toJson
    |> decode
    |> D.decodeString
    )
--> Ok {value=42,name="Elm"}
toJson : JsonObject obj obj -> Json obj
String
-> Json a
-> (obj -> a)
-> JsonObject (a -> fun) obj
-> JsonObject fun obj
String
-> Json a
-> (obj -> List a)
-> JsonObject (List a -> fun) obj
-> JsonObject fun obj
String
-> Json a
-> (obj -> Maybe a)
-> JsonObject (Maybe a -> fun) obj
-> JsonObject fun obj

Http Requests

update : String -> Json a -> (a -> a) -> Task Error ()

First gets the value, then either inserts a new value or does nothing

Use delete if you want to delete an element.

There is a max limit of 100kb that can be inserted at once. Therefore never try to update a full list of object, rather send an seperate update/delete file for every entry of the list.

get : String -> Decoder a -> Task Error (Maybe a)

Gets an Element

Returns Nothing if the element does not exist.

insert : String -> Value -> Task Error ()

Inserts a new Element.

Do not use this function to update fields, use update instead.

There is a max limit of 100kb that can be inserted at once. Therefore never try to update a full list of object, rather send an seperate update/delete file for every entry of the list.

delete : String -> Task Error ()

Deletes an Element.

Will be successfull even if the content is empty.