Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1.  
  2. import Html.App exposing (beginnerProgram)
  3. import Html exposing (text, div, button)
  4. import Html.Events exposing (onClick)
  5. import Json.Decode as Decode exposing ((:=), decodeString)
  6.  
  7.  
  8. main =
  9. beginnerProgram
  10. { view = view
  11. , model = "Attempt to decode."
  12. , update = update
  13. }
  14.  
  15.  
  16. type Msg
  17. = DecodeJson
  18.  
  19.  
  20. update msg model =
  21. case msg of
  22. DecodeJson ->
  23. let
  24. decodedResult =
  25. decodeString decoder faultyJsonString
  26. in
  27. model
  28.  
  29.  
  30. view model =
  31. div []
  32. [ text faultyJsonString
  33. , button [ onClick DecodeJson ]
  34. [ text model ]
  35. ]
  36.  
  37.  
  38. type Decoded
  39. = Decoded { id : Int, contained : List Decoded }
  40.  
  41.  
  42. faultyJsonString =
  43. "{\"id\":1,\"contained\":[{\"id\":2,\"contained\":[{\"id\":3,\"contained\":[]}]}]}"
  44.  
  45.  
  46. decoder =
  47. Decode.object2 (\id contained -> Decoded { id = id, contained = contained })
  48. ("id" := Decode.int)
  49. ("contained" := Decode.list decoder)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement