Skip to content

Instantly share code, notes, and snippets.

@yang-wei
Last active May 1, 2019 05:36
Show Gist options
  • Save yang-wei/2d1c33b9c114fcd652ff to your computer and use it in GitHub Desktop.
Save yang-wei/2d1c33b9c114fcd652ff to your computer and use it in GitHub Desktop.
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
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