Help with Json Decoding objects with loads of fields

180 views
Skip to first unread message

Hassan Hayat

unread,
Mar 6, 2015, 11:05:52 AM3/6/15
to elm-d...@googlegroups.com
Hi there,

I have this Json object that's of this Elm type:

type alias DomMouseEvent =
  { screenX : Int
  , screenY : Int
  , clientX : Int
  , clientY : Int
  , ctrlKey : Bool
  , shiftKey : Bool
  , altKey : Bool
  , metaKey : Bool
  , button : Int
  , buttons : Int
  }

and I'd like to produce a Json decoder than can decode all that. Thing is, there are 10 fields there. object goes all the way up to 8. I was wondering how you could make a Decoder of the form:

domMouseEventDecoder : Decoder DomMouseEvent

Thanks.

Hassan Hayat

unread,
Mar 6, 2015, 11:09:54 AM3/6/15
to elm-d...@googlegroups.com
For reference, I'm trying to decode a DOM MouseEvent as specified by the W3C spec

interface MouseEvent : UIEvent {
   
readonly    attribute long           screenX;
   
readonly    attribute long           screenY;
   
readonly    attribute long           clientX;
   
readonly    attribute long           clientY;
   
readonly    attribute boolean        ctrlKey;
   
readonly    attribute boolean        shiftKey;
   
readonly    attribute boolean        altKey;
   
readonly    attribute boolean        metaKey;
   
readonly    attribute unsigned short button;
   
readonly    attribute unsigned short buttons;
   
readonly    attribute EventTarget?   relatedTarget;
   
// Introduced in DOM Level 3
   
boolean getModifierState (DOMString keyArg);
};

Minus the last attribute and the method at the end. 

Jeff Smits

unread,
Mar 6, 2015, 11:26:40 AM3/6/15
to elm-discuss
Use Json.Decode.map and andMap defined as follows:

import Json.Decode as Decode
import Json.Decode (Decoder)


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


--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hassan Hayat

unread,
Mar 6, 2015, 11:28:51 AM3/6/15
to elm-d...@googlegroups.com
Cool. Thanks.

Zinggi

unread,
Mar 7, 2015, 7:26:57 AM3/7/15
to elm-d...@googlegroups.com
This looks so easy and like a mechanical translation, that it really makes me want to have macros.
All the information is in the type and the code could easily be created with some macro that knows about the type.

Jeff Smits

unread,
Mar 7, 2015, 7:59:52 AM3/7/15
to elm-discuss
type directed macros have been discussed before. Last discussion I can find, Evan said his priority was to get Promises done.
Reply all
Reply to author
Forward
0 new messages