Advertisement
Guest User

Untitled

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