Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Html.App exposing (beginnerProgram)
- import Html exposing (text, div, button)
- import Html.Events exposing (onClick)
- import Json.Decode as Decode exposing ((:=), decodeString)
- main =
- beginnerProgram
- { view = view
- , model = Nothing
- , update = update
- }
- type Msg
- = DecodeJson
- update msg model =
- case msg of
- DecodeJson ->
- let
- decodedResult =
- decodeString decoder faultyJsonString
- in
- model
- view model =
- div []
- [ text faultyJsonString
- , button [ onClick DecodeJson ]
- [ text "Attempt to decode." ]
- ]
- type Decoded
- = Decoded { id : Int, contained : List Decoded }
- faultyJsonString =
- "{\"id\":1,\"contained\":[{\"id\":2,\"contained\":[{\"id\":3,\"contained\":[]}]}]}"
- decoder =
- Decode.object2 (\id contained -> Decoded { id = id, contained = contained })
- ("id" := Decode.int)
- ("contained" := Decode.list decoder)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement