FabienHenon / jsonapi / JsonApi.Encode

Provides a function to encode documents to a Json.Encode.Value. You can then finally encode it to a json api string with Json.Encode.encode.

Encoders

document : Document -> Json.Encode.Value

Encodes a document.

Here is an example with many resources and a meta object:

type alias Post =
    { id : String
    , links : Dict String String
    , title : String
    , content : String
    , creator : Creator
    , comments : List Comment
    }

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

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

post : Post
post =
    { id = "post-1"
    , links = Dict.fromList [ ( "self", "http://url-to-post/1" ) ]
    , title = "Post 1"
    , content = "Post content 1"
    , creator = creator
    , comments = [ comment1 ]
    }

creator : Creator
creator =
    { id = "creator-1"
    , links = Dict.fromList [ ( "self", "http://url-to-creator/1" ) ]
    , firstname = "John"
    , lastname = "Doe"
    }

comment1 : Comment
comment1 =
    { id = "comment-1"
    , links = Dict.fromList [ ( "self", "http://url-to-comment/1" ) ]
    , content = "Comment 1"
    , email = "email@email.com"
    }

postToResource : Post -> Resource
postToResource post =
    JsonApi.Resource.build "posts"
        |> JsonApi.Resource.withId post.id
        |> JsonApi.Resource.withLinks post.links
        |> JsonApi.Resource.withAttributes
            [ ( "title", string post.title )
            , ( "content", string post.content )
            ]
        |> JsonApi.Resource.withRelationship "creator" (JsonApi.Resource.relationship post.creator.id (creatorToResource post.creator))
        |> JsonApi.Resource.withRelationship "comments" (JsonApi.Resource.relationships (List.map commentRelationship post.comments))

creatorToResource : Creator -> Resource
creatorToResource creator =
    JsonApi.Resource.build "creators"
        |> JsonApi.Resource.withId creator.id
        |> JsonApi.Resource.withLinks creator.links
        |> JsonApi.Resource.withAttributes
            [ ( "firstname", string creator.firstname )
            , ( "lastname", string creator.lastname )
            ]

commentRelationship : Comment -> ( String, Resource )
commentRelationship comment =
    ( comment.id, commentToResource comment )

commentToResource : Comment -> Resource
commentToResource comment =
    JsonApi.Resource.build "comment"
        |> JsonApi.Resource.withId comment.id
        |> JsonApi.Resource.withLinks comment.links
        |> JsonApi.Resource.withAttributes
            [ ( "content", string comment.content )
            , ( "email", string comment.email )
            ]

documentToEncode : Document
documentToEncode =
    JsonApi.Encode.Document.build
        |> JsonApi.Encode.Document.withResource (postToResource post)
        |> JsonApi.Encode.Document.withMeta (object [ ( "redirect", bool True ) ])


-- Encodes the document
JsonApi.Encode.document documentToEncode