{ encoder : a -> Value
, decoder : Json.Decode.Decoder a
}
A pair of JSON encoder and decoder.
You can make custom converters using this directly if you need.
Json.Encode.Value
Represents a JavaScript value.
encode : Converter a -> Basics.Int -> a -> String
Convert data into a prettified string. The second argument specifies the amount of indentation in the resulting string.
decodeValue : Converter a -> Value -> Result Json.Decode.Error a
Decode a JSON Value
.
decodeString : Converter a -> String -> Result Json.Decode.Error a
Parse the given string into a JSON value and then decode it.
string : Converter String
Converter for String
.
"hello" <== string ==> "\"hello\""
int : Converter Basics.Int
Converter for Int
.
42 <== int ==> "42"
float : Converter Basics.Float
Converter for Float
.
3.14 <== float ==> "3.14"
bool : Converter Basics.Bool
Convert between an Elm Bool
and a JSON boolean.
True <== bool ==> "true"
null : a -> Converter a
Convert between some Elm value and a JSON null
value.
"Some Value" <== null "Some Value" ==> null
list : Converter a -> Converter (List a)
Convert between an Elm List
and a JSON array.
[ 1, 2, 3 ] <== list int ==> "[1,2,3]"
array : Converter a -> Converter (Array a)
Convert between an Elm Array
and a JSON array.
Array.fromList [ 1, 2, 3 ] <== array int ==> "[1,2,3]"
dict : Converter a -> Converter (Dict String a)
Convert between an Elm Dict
and a JSON object.
Dict.fromList [ ( "alice", 42 ), ( "bob", 99 ) ] <== dict int ==> "{ \"alice\": 42, \"bob\": 99 }"
A helper data type for building an object converter.
The following functions have a little tricky types, but you do not need to think about it! Just write like this:
type alias User =
{ name : String
, age : Int
, height : Maybe Float
}
converter : Converter User
converter =
object User <|
field "name" .name string
>> field "age" .age int
>> option "height" .height float
object : constr -> (Field constr a -> Field a a) -> Converter a
Build an object converter with a constructor. Use it along with field
or option
.
field : String -> (r -> a) -> Converter a -> Field (a -> l) r -> Field l r
Represents a field of an object. Use it along with object
.
option : String -> (r -> Maybe a) -> Converter a -> Field (Maybe a -> l) r -> Field l r
Optional version of field
.
{ get : a -> b, reverseGet : b -> a }
A pair of a function and its inverse. Same definition for Monocle.Iso.
map : Iso a b -> Converter a -> Converter b
Transform a converter. You need Iso a b
because the decoder requires a function a -> b
and the encoder requires an inverse function b -> a
to map.
string2CharListIso : Iso String (List Char)
string2CharListIso =
Iso String.toList String.fromList
charList : Converter (List Char)
charList =
map string2CharListIso string
reverse : Iso a b -> Iso b a
Create the reversed isomorphism.
compose : Iso a b -> Iso b c -> Iso a c
Compose two isomorphisms.
nullable : Converter a -> Converter (Maybe a)
Convert between an Elm value and a nullable JSON value.
Just 42 <== nullable int ==> "42"
Nothing <== nullable int ==> "null"
value : Converter Value
Do not do anything, just build a bridge between a JSON Value and an Elm Value
.
lazy : (() -> Converter a) -> Converter a
For building a converter of recursive structure.
Use it like lazy (\_ -> converter)
instead of just converter
in order for the converter not to expand infinitly deep.
type Tree a
= Tree a (Maybe (Tree a)) (Maybe (Tree a))
tree : Converter a -> Converter (Tree a)
tree item =
object Tree <|
field "item" (\(Tree a _ _) -> a) item
>> option "left" (\(Tree _ l _) -> l) (lazy <| \_ -> tree item)
>> option "right" (\(Tree _ _ r) -> r) (lazy <| \_ -> tree item)