Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upobject decoder for more than 8 fields #181
Conversation
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment
Hide comment
uehaj
Aug 31, 2015
@jvoigtlaender, Thanks for notification.
I tried:
import Json.Decode as Decode
import Json.Decode exposing (..)
import Text exposing (fromString)
import Graphics.Element exposing (flow,down,show)
import List
type alias DomMouseEvent =
{ screenX : Int
, screenY : Int
, clientX : Int
, clientY : Int
, ctrlKey : Bool
, shiftKey : Bool
, altKey : Bool
, metaKey : Bool
, button : Int
, buttons : Int
}
andMap : Decoder (a -> b) -> Decoder a -> Decoder b
andMap = Decode.object2 (<|)
decodeDomMouseEvent : Decoder DomMouseEvent
decodeDomMouseEvent =
DomMouseEvent
`Decode.map` Decode.int
`andMap` Decode.int
`andMap` Decode.int
`andMap` Decode.int
`andMap` Decode.bool
`andMap` Decode.bool
`andMap` Decode.bool
`andMap` Decode.bool
`andMap` Decode.int
`andMap` Decode.int
main = flow down
[ show <| decodeString Decode.int "3"
, show <| decodeString (list string) "[\"abc\",\"def\"]"
, show <| decodeString decodeDomMouseEvent "{\"screenX\":3, \"screenY\":3, \"clientX\":3, \"clientY\":3, \"ctrlKey\":true, \"shiftKey\":true, \"altKey\":true, \"metaKey\":true, \"button\":3, \"buttons\":3}"
]
on Elm Platform 0.15.1 but get following error:
Ok 3
Ok ["abc","def"]
Err ("expecting an Int but got {\"screenX\":3,\"screenY\":3,\"clientX\":3,\"clientY\":3,\"ctrlKey\":true,\"shiftKey\":true,\"altKey\":true,\"metaKey\":true,\"button\":3,\"buttons\":3}")
Something wrong? or bad usage?
uehaj
commented
Aug 31, 2015
|
@jvoigtlaender, Thanks for notification.
on Elm Platform 0.15.1 but get following error:
Something wrong? or bad usage? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment
Hide comment
jvoigtlaender
Aug 31, 2015
Contributor
This works:
decodeDomMouseEvent : Decoder DomMouseEvent
decodeDomMouseEvent =
DomMouseEvent
`Decode.map` ("screenX" := Decode.int)
`andMap` ("screenY" := Decode.int)
`andMap` ("clientX" := Decode.int)
`andMap` ("clientY" := Decode.int)
`andMap` ("ctrlKey" := Decode.bool)
`andMap` ("shiftKey" := Decode.bool)
`andMap` ("altKey" := Decode.bool)
`andMap` ("metaKey" := Decode.bool)
`andMap` ("button" := Decode.int)
`andMap` ("buttons" := Decode.int)|
This works: decodeDomMouseEvent : Decoder DomMouseEvent
decodeDomMouseEvent =
DomMouseEvent
`Decode.map` ("screenX" := Decode.int)
`andMap` ("screenY" := Decode.int)
`andMap` ("clientX" := Decode.int)
`andMap` ("clientY" := Decode.int)
`andMap` ("ctrlKey" := Decode.bool)
`andMap` ("shiftKey" := Decode.bool)
`andMap` ("altKey" := Decode.bool)
`andMap` ("metaKey" := Decode.bool)
`andMap` ("button" := Decode.int)
`andMap` ("buttons" := Decode.int) |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment
Hide comment
uehaj
commented
Aug 31, 2015
|
OK, it works for me, so cool, thanks! |
uehaj
closed this
Aug 31, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
uehaj commentedFeb 23, 2015
I can't find the easy &simple way of object decoder, for the object which have more than 8 fields in current Elm's Json.Decode package. This PR provides helper functions for define generic decoder for object which have any number of fields.
I think this PR is not perfect, because:
If the direction is ok, I can additional tweak for this PR.