FabienHenon/jsonapi/1.1.1

NOTE: this package is not compatible with Elm 0.19.1

JsonApi.Decode

Provides functions to decode json api resources and their relationships

Example json:

{
    "data": [
        {
            "type": "posts",
            "id": "13608770-76dd-47e5-a1c4-4d0d9c2483ad",
            "links": {
                "self": "http://link-to-post/2"
            },
            "attributes": {
                "title": "First post",
                "content": "First post content"
            },
            "relationships": {
                "creator": {
                    "data": {
                        "type": "creators",
                        "id": "22208770-76dd-47e5-a1c4-4d0d9c2483ad"
                    },
                    "links": {
                        "related": "http://link-to-creator/1"
                    }
                },
                "comments": {
                    "links": {},
                    "data": [
                        {
                            "type": "comment",
                            "id": "22208770-76dd-47e5-a1c4-4d0d9c2483ab"
                        },
                        {
                            "type": "comment",
                            "id": "cb0759b0-03ab-4291-b067-84a9017fea6f"
                        }
                    ]
                }
            }
        },
        {
            "type": "posts",
            "id": "13608770-76dd-47e5-a1c4-4d0d9c2483ae",
            "links": {
                "self": "http://link-to-post/2"
            },
            "attributes": {
                "title": "Second post",
                "content": "Second post content"
            },
            "relationships": {
                "creator": {
                    "data": {
                        "type": "creators",
                        "id": "22208770-76dd-47e5-a1c4-4d0d9c2483ad"
                    },
                    "links": {
                        "related": "http://lnk-to-creator/1"
                    }
                },
                "comments": {
                    "links": {},
                    "data": [
                        {
                            "type": "comment",
                            "id": "22208770-76dd-47e5-a1c4-4d0d9c2483ac"
                        }
                    ]
                }
            }
        }
    ],
    "included": [
        {
            "type": "creators",
            "id": "22208770-76dd-47e5-a1c4-4d0d9c2483ad",
            "attributes": {
                "firstname": "John",
                "lastname": "Doe"
            },
            "links": {
                "self": "http://link-to-creator/1"
            },
            "relationships": {}
        },
        {
            "type": "comment",
            "id": "22208770-76dd-47e5-a1c4-4d0d9c2483ac",
            "attributes": {
                "content": "Comment 1 content",
                "email": "[email protected]"
            },
            "links": {
                "self": "http://link-to-comment/1"
            },
            "relationships": {}
        },
        {
            "type": "comment",
            "id": "22208770-76dd-47e5-a1c4-4d0d9c2483ab",
            "attributes": {
                "content": "Comment 2 content",
                "email": "[email protected]"
            },
            "links": {
                "self": "http://link-to-comment/2"
            },
            "relationships": {}
        },
        {
            "type": "comment",
            "id": "cb0759b0-03ab-4291-b067-84a9017fea6f",
            "attributes": {
                "content": "Comment 3 content",
                "email": "[email protected]"
            },
            "links": {
                "self": "http://link-to-comment/3"
            },
            "relationships": {}
        }
    ]
}

Decoders

resources : String -> (ResourceInfo -> Decoder a) -> Decoder (List a)

Decode resources from the json api content.

You pass it the type of the resources ("posts" in our example above) and the resource decoder and it will return a new Decoder representing a List of your resources.

Here is an example of resource Decoder:

type alias Post =
    { id : String
    , title : String
    , content : String
    }


postDecoder : ResourceInfo -> Decoder Post
postDecoder resourceInfo =
    map3 Post
        (succeed (JsonApi.id resourceInfo))
        (field "title" string)
        (field "content" string)


-- Decoder for our posts from json api
resources "posts" postDecoder
resource : String -> (ResourceInfo -> Decoder a) -> Decoder a

Decode only one resource from the json api content.

You pass it the type of the resource ("posts" in our example above) and the resource decoder and it will return a new Decoder representing your resource.

(The json data attribute is an object and not a list)

Here is an example of resource Decoder:

type alias Post =
    { id : String
    , title : String
    , content : String
    }


postDecoder : ResourceInfo -> Decoder Post
postDecoder resourceInfo =
    map3 Post
        (succeed (JsonApi.id resourceInfo))
        (field "title" string)
        (field "content" string)


-- Decoder for our post from json api
resource "posts" postDecoder
relationship : String -> ResourceInfo -> (ResourceInfo -> Decoder a) -> Decoder a

Decode a relationship from your json api resources.

You pass it the type of the relationship resource ("creator" in our example above), the ResourceInfo passed to your resources Decoder and the relationship decoder. It will return a new Decoder representing the relationship.

Here is an example of resource Decoder with a relationship:

type alias Post =
    { id : String
    , title : String
    , content : String
    , creator : Creator
    }


type alias Creator =
    { id : String
    , firstname : String
    , lastname : String
    }


creatorDecoder : ResourceInfo -> Decoder Creator
creatorDecoder resourceInfo =
    map3 Creator
        (succeed (JsonApi.id resourceInfo))
        (field "firstname" string)
        (field "lastname" string)


postDecoder : ResourceInfo -> Decoder Post
postDecoder resourceInfo =
    map4 Post
        (succeed (JsonApi.id resourceInfo))
        (field "title" string)
        (field "content" string)
        (relationship "creator" resourceInfo creatorDecoder)


-- Decoder for our posts and its creator from json api
resources "posts" postDecoder
String
-> ResourceInfo
-> (ResourceInfo -> Decoder a)
-> Decoder (List a)

Decode a list of relationships from your json api resources.

You pass it the type of the relationship resource ("comments" in our example above), the ResourceInfo passed to your resources Decoder and the relationship decoder. It will return a new Decoder representing the List of relationships.

Here is an example of resource Decoder with a list of relationships:

type alias Post =
    { id : String
    , title : String
    , content : String
    , comments : List Comment
    }


type alias Comment =
    { id : String
    , content : String
    , email : String
    }


commentDecoder : ResourceInfo -> Decoder Comment
commentDecoder resourceInfo =
    map3 Comment
        (succeed (JsonApi.id resourceInfo))
        (field "content" string)
        (field "email" string)


postDecoder : ResourceInfo -> Decoder Post
postDecoder resourceInfo =
    map4 Post
        (succeed (JsonApi.id resourceInfo))
        (field "title" string)
        (field "content" string)
        (relationships "comments" resourceInfo commentDecoder)


-- Decoder for our posts and its creator from json api
resources "posts" postDecoder