Last active
November 23, 2015 21:21
-
-
Save MangelMaxime/01d4349a80d870567a0f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ItemTask where | |
import Time exposing (..) | |
import CommonTypes exposing (ID) | |
import Json.Encode | |
import Json.Decode | |
import Json.Decode exposing ((:=)) | |
-- MODEL | |
type alias Model = | |
{ id: ID | |
, title: String | |
, duration: Time | |
, completed: Bool | |
, editing : Bool | |
} | |
newTask : ID -> String -> Time -> Model | |
newTask id title duration = | |
{ id = id | |
, title = title | |
, duration = duration | |
, completed = False | |
, editing = False | |
} | |
jsonEncModel : Model -> Json.Encode.Value | |
jsonEncModel model = | |
Json.Encode.object | |
[ ("id", Json.Encode.int model.id) | |
, ("title", Json.Encode.string model.title) | |
, ("duration", Json.Encode.float model.duration) | |
, ("completed", Json.Encode.bool model.completed) | |
] | |
apply : Json.Decode.Decoder (a -> b) -> Json.Decode.Decoder a -> Json.Decode.Decoder b | |
apply func value = | |
Json.Decode.object2 (<|) func value | |
jsonDecModel : Json.Decode.Decoder Model | |
jsonDecModel = | |
Json.Decode.map Model | |
("id" := Json.Decode.int) | |
`apply` ("title" := Json.Decode.string) | |
`apply` ("duration" := Json.Decode.float) | |
`apply` ("completed" := Json.Decode.bool) | |
-- Here how to force a bool value : False | |
jsonTest = "{ | |
\"id\": 0, | |
\"title\": \"test\", | |
\"duration\": 0, | |
\"completed\": false | |
}" | |
testDecoder = | |
(Json.Decode.decodeString jsonDecModel jsonTest) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment