Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Json.Decode example
import Graphics.Element exposing (Element, show)
import Task exposing (Task, andThen)
import Json.Decode exposing (Decoder, int, string, object3, (:=), at, keyValuePairs)
import Http
type alias Languages =
List (String, Int)
mailbox =
Signal.mailbox []
-- VIEW
main : Signal Element
main =
Signal.map show mailbox.signal
-- TASK
fetchApi =
Http.get decoder api
handleResponse data =
Signal.send mailbox.address data
decoder : Decoder (List (String, Int))
decoder =
keyValuePairs int
port run : Task Http.Error ()
port run =
fetchApi `andThen` handleResponse
api =
"https://api.github.com/repos/elm-lang/elm-lang.org/languages"
import Graphics.Element exposing (Element, show)
import Task exposing (Task, andThen)
import Json.Decode exposing (Decoder, int, string, list, at, (:=), object1)
import Dict exposing (Dict)
import Http
mailbox =
Signal.mailbox []
-- VIEW
main : Signal Element
main =
Signal.map show mailbox.signal
-- TASK
fetchApi =
Http.get decoder api
handleResponse data =
Signal.send mailbox.address data
fullNameDecoder : Decoder String
fullNameDecoder =
object1 identity ("full_name" := string)
decoder =
at ["items"] (list fullNameDecoder)
port run : Task Http.Error ()
port run =
fetchApi `andThen` handleResponse
api =
"https://api.github.com/search/repositories?q=language:elm&sort=starts&language=elm"
import Graphics.Element exposing (Element, show)
import Task exposing (Task, andThen)
import Json.Decode exposing (Decoder, int, string, object3, (:=))
import Http
type alias RepoRecord =
{ name : String
, description : String
, watchers_count : Int
}
mailbox =
Signal.mailbox (RepoRecord "" "" 0)
-- VIEW
main : Signal Element
main =
Signal.map show mailbox.signal
-- TASK
fetchApi =
Http.get repoRecordDecoder api
handleResponse data =
Signal.send mailbox.address data
repoRecordDecoder : Decoder RepoRecord
repoRecordDecoder =
object3 RepoRecord
("name" := string)
("description" := string)
("watchers_count" := int)
port run : Task Http.Error ()
port run =
fetchApi `andThen` handleResponse
api =
"https://api.github.com/repos/elm-lang/core"
import Graphics.Element exposing (Element, show)
import Task exposing (Task, andThen)
import Json.Decode exposing (Decoder, int, string, object3, (:=))
import Http
type alias RepoTuple = ( String, String, Int )
mailbox =
Signal.mailbox ("", "", 0)
-- VIEW
main : Signal Element
main =
Signal.map show mailbox.signal
-- TASK
fetchApi =
Http.get repoTupleDecoder api
handleResponse data =
Signal.send mailbox.address data
-- decoder changes depends on our data type
repoTupleDecoder : Decoder RepoTuple
repoTupleDecoder =
object3 (,,)
("name" := string)
("description" := string)
("watchers_count" := int)
port run : Task Http.Error ()
port run =
fetchApi `andThen` handleResponse
api =
"https://api.github.com/repos/elm-lang/core"
@niccolox

This comment has been minimized.

Copy link

niccolox commented May 1, 2019

whats your elm-package look like?

what version of elm are you running?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.