New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

object decoder for more than 8 fields #181

Closed
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
2 participants
@uehaj

uehaj commented Feb 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.

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Aug 30, 2015

Contributor

@uehaj, is this issue still relevant for you? Discussion of decoding objects with more than 8 fields can be found here.

Does that work for you? Can we close the PR here?

Contributor

jvoigtlaender commented Aug 30, 2015

@uehaj, is this issue still relevant for you? Discussion of decoding objects with more than 8 fields can be found here.

Does that work for you? Can we close the PR here?

@uehaj

This comment has been minimized.

Show comment
Hide comment
@uehaj

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.
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?

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

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)
Contributor

jvoigtlaender commented Aug 31, 2015

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)
@uehaj

This comment has been minimized.

Show comment
Hide comment
@uehaj

uehaj Aug 31, 2015

OK, it works for me, so cool, thanks!
I'll close this ticket.

uehaj commented Aug 31, 2015

OK, it works for me, so cool, thanks!
I'll close this ticket.

@uehaj uehaj closed this Aug 31, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment