Orasund / elm-jsonstore / Jsonstore

Decoding and Encoding


type Json a

The Json type combines both the Json Decoder and Encoder.

decode : Json a -> Json.Decode.Decoder a

Returns the decoder of a Json type

encode : Json a -> a -> Json.Encode.Value

Returns the encoder of a Json type

encodeList : Json a -> List a -> Json.Encode.Value

Returns the encoder for a List of a Json type

decodeList : Json a -> Json.Decode.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 Basics.Bool

import Json.Decode as D

"true"
|> (bool |> decode |> D.decodeString)
--> Ok True

int : Json Basics.Int

import Json.Decode as D

"1" |> (int |> decode |> D.decodeString) --> Ok 1

float : Json Basics.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

with : String -> Json a -> (obj -> a) -> JsonObject (a -> fun) obj -> JsonObject fun obj

withList : String -> Json a -> (obj -> List a) -> JsonObject (List a -> fun) obj -> JsonObject fun obj

withMaybe : String -> Json a -> (obj -> Maybe a) -> JsonObject (Maybe a -> fun) obj -> JsonObject fun obj

Http Requests

update : String -> Json a -> (a -> a) -> Task Http.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 -> Json.Decode.Decoder a -> Task Http.Error (Maybe a)

Gets an Element

Returns Nothing if the element does not exist.

insert : String -> Json.Encode.Value -> Task Http.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 Http.Error ()

Deletes an Element.

Will be successfull even if the content is empty.