Creates a trivial ObjectMapping.
This, along with with, static make up a pipeline-style API
which can be used like this:
import DesktopApp.JsonMapping exposing (ObjectMapping, int, object, with)
type alias MyData =
{ total : Int
, count : Int
}
myObjectMapping : ObjectMapping MyData MyData
myObjectMapping =
object MyData
|> with "total" .total int
|> with "count" .count intAdds a field to an object. It will be represented in both your Elm model and in the JSON.
Adds a static field to an object. The field will not be represented in your Elm model, but this exact field name and value will be added to the encoded JSON.
Transforms the type that an ObjectMapping decodes.
Transforms the type that an ObjectMapping encodes.
ObjectMappingsNormally you will just pass your ObjectMapping to DesktopApp.program,
but the following functions are available if you want to manually make use of an ObjectMapping
for other purposes.
Encodes a given encodesFrom value with the given ObjectMapping (into a JSON string).
Encodes a given encodesFrom value with the given ObjectMapping (into a Json.Encode.Value).
Gets the Json.Decode.Decoder for the given ObjectMapping.
Represents how to encode a to and from JSON.
Maps an Elm Int to and from JSON.
Maps an Elm String to and from JSON.
Maps an Elm Bool to and from JSON.
Maps an Elm Maybe to and from JSON.
Nothing will map to null,
and Just a will use the given mapping for a.
import DesktopApp.JsonMapping exposing (maybe, int, encode, decoder)
import Json.Decode exposing (decodeString)
encode (maybe int) (Just 7) --> "7"
encode (maybe int) Nothing --> "null"
decodeString (decoder (maybe string)) "\"hi\"" --> Just "hi"
decodeString (decoder (maybe string)) "null" --> NothingMaps an Elm List to and from a JSON array.
Creates a JsonMapping that uses the given Elm JSON encoder and decoder
Creates a JsonMapping from an ObjectMapping.
This allows you to create JsonMappings that can then be used as nested fields within other ObjectMappings.
import DesktopApp.JsonMapping exposing (fromObjectMapping, int, object, string, with)
type alias MyData =
{ name : String
, admin : Bool
}
myDataMapping : JsonMapping MyData
myDataMapping =
object MyData
|> with "name" .name string
|> with "admin" .admin bool
|> fromObjectMappingTransforms a JsonMapping. This requires functions for transforming in each direction
so that both encoding and decoding can be handled.
Maps an Elm custom type (sometimes also called a "union type", "tagged union", or "ADT") to and from a JSON object.
VariantDecoders and VariantEncoders are created using the variant* functions (see the example below).
This function returns an ObjectMapping instead of a JsonMapping so that it is possible to have a custom type as the top-level of your persisted data when using DesktopApp.program.
If you need a JsonMapping, you can use this with fromObjectMapping
(as shown in the example).
As this example shows, creating a mapping for your custom type requires the following:
let block, create Variants for each variant of your custom type using the variant* funtionscustomType, pass a list containing the .decode from all of your variantscustomType, pass a function that destructures a value of your type and returns the corresponding variant's encoder with the destructured parameter values applied.Example:
import DesktopApp.JsonMapping exposing (JsonMapping, bool, customType, fromObjectMapping, int, string, variant0, variant1, variant2)
type MyType
= NotAuthorized
| Guest Bool String
| Employee Int
myTypeMapping : JsonMapping MyType
myTypeMapping =
let
notAuthorized =
variant0
"NotAuthorized"
NotAuthorized
guest =
variant2
"Guest"
Guest
( "is_vip", bool )
( "name", string )
employee =
variant1
"Employee"
Employee
( "employee_id", int )
in
customType
[ notAuthorized.decode
, guest.decode
, employeed.decode
]
(\x ->
case x of
NotAuthorized ->
notAuthorized.encode
Guest isVip name ->
guest.encode isVip name
Employee id ->
employeed.encode id
)
|> fromObjectMapping
encodeString myTypeMapping NotAuthorized
--> """{"$":"NotAuthorized"}"""
encodeString myTypeMapping (Guest True "Kai")
--> """{"$":"Guest","is\_vip":true,"name":"Kai"}"""
encodeString myTypeMapping (Employee 24601)
--> """{"$":"Employee","employee\_id":24601}"""Represents how to map an Elm custom type variant (sometimes also called a "tag", "contructor", or "enum value") to and from a JSON object.
This is different from a JsonMapping because it carries with it information about the name of the variant
and does some tricks with the type system to make the customType API as nice as possible.
Normally you will not need to reference this type directly. See the example for customType.
Represents how to encode an Elm custom type variant to a JSON object.
Normally you will not need to reference this type directly. See the example for customType.
Represents how to decode an Elm custom type variant from a JSON object.
Normally you will not need to reference this type directly. See the example for customType.
Creates a VariantEncoder and VariantDecoder for an Elm custom type variant that takes no parameters.
See customType for an example of how to use the variant* functions.
Creates a VariantEncoder and VariantDecoder for an Elm custom type variant that takes one parameter.
See customType for an example of how to use the variant* functions.
Creates a VariantEncoder and VariantDecoder for an Elm custom type variant that takes two parameters.
See customType for an example of how to use the variant* functions.
Creates a VariantEncoder and VariantDecoder for an Elm custom type variant that takes three parameters.
See customType for an example of how to use the variant* functions.
Creates a VariantEncoder and VariantDecoder for an Elm custom type variant that takes four parameters.
See customType for an example of how to use the variant* functions.
Creates a VariantEncoder and VariantDecoder for an Elm custom type variant that takes five parameters.
See customType for an example of how to use the variant* functions.
Represents both how to encode
encodesFrominto a JSON object and decode a JSON object intodecodesTo. This is similar toJsonMapping, but it allows a pipeline-style API for building up mappings (seeobject,with,static).Notably this is used with
DesktopApp.programto specify how to save and load data. When used in that way, theencodesFromtype will be your program's model, and thedecodesTotype will be your program's msg (which will be produced when data is loaded).Also of note: when
encodesFromanddecodesToare the same type it specifies a two-way mapping to and from JSON (and can then be turned into aJsonMappingwithfromObjectMapping).