jsonapi_deserializer v0.1.0 JsonApiDeserializer View Source
Json api deserializer able to deserialize json api documents with relationships.
For instance, this payload:
{
"data": [
{
"type": "posts",
"id": "13608770-76dd-47e5-a1c4-4d0d9c2483ad",
"links": {
"self": "http://link-to-post/1"
},
"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": "john@doe.com"
},
"links": {
"self": "http://link-to-comment/1"
},
"relationships": {}
},
{
"type": "comment",
"id": "22208770-76dd-47e5-a1c4-4d0d9c2483ab",
"attributes": {
"content": "Comment 2 content",
"email": "john@doe.com"
},
"links": {
"self": "http://link-to-comment/2"
},
"relationships": {}
},
{
"type": "comment",
"id": "cb0759b0-03ab-4291-b067-84a9017fea6f",
"attributes": {
"content": "Comment 3 content",
"email": "john@doe.com"
},
"links": {
"self": "http://link-to-comment/3"
},
"relationships": {}
}
]
}
Will be deserialized in a map like this one:
[
%{
"__metadata" => %{
"links" => %{"self" => "http://link-to-post/1"},
"type" => "posts"
},
"comments" => [
%{
"__metadata" => %{
"links" => %{"self" => "http://link-to-comment/2"},
"type" => "comment"
},
"content" => "Comment 2 content",
"email" => "john@doe.com",
"id" => "22208770-76dd-47e5-a1c4-4d0d9c2483ab"
},
%{
"__metadata" => %{
"links" => %{"self" => "http://link-to-comment/3"},
"type" => "comment"
},
"content" => "Comment 3 content",
"email" => "john@doe.com",
"id" => "cb0759b0-03ab-4291-b067-84a9017fea6f"
}
],
"content" => "First post content",
"creator" => %{
"__metadata" => %{
"links" => %{"self" => "http://link-to-creator/1"},
"type" => "creators"
},
"firstname" => "John",
"id" => "22208770-76dd-47e5-a1c4-4d0d9c2483ad",
"lastname" => "Doe"
},
"id" => "13608770-76dd-47e5-a1c4-4d0d9c2483ad",
"title" => "First post"
},
%{
"__metadata" => %{
"links" => %{"self" => "http://link-to-post/2"},
"type" => "posts"
},
"comments" => [
%{
"__metadata" => %{
"links" => %{"self" => "http://link-to-comment/1"},
"type" => "comment"
},
"content" => "Comment 1 content",
"email" => "john@doe.com",
"id" => "22208770-76dd-47e5-a1c4-4d0d9c2483ac"
}
],
"content" => "Second post content",
"creator" => %{
"__metadata" => %{
"links" => %{"self" => "http://link-to-creator/1"},
"type" => "creators"
},
"firstname" => "John",
"id" => "22208770-76dd-47e5-a1c4-4d0d9c2483ad",
"lastname" => "Doe"
},
"id" => "13608770-76dd-47e5-a1c4-4d0d9c2483ae",
"title" => "Second post"
}
]
Link to this section Summary
Functions
Deserialize a payload.
Link to this section Functions
Link to this function
deserialize(body)
View Source
deserialize(body)
View Source
deserialize(binary() | map()) ::
{:ok, list() | map()}
| {:error, Jason.DecodeError.t()}
| {:error, :invalid_data}
| {:error, :bad_relationships_type}
| {:error, :bad_included_type}
| {:error, :relationship_not_found}
| {:error, :bad_relationship_data}
deserialize(binary() | map()) :: {:ok, list() | map()} | {:error, Jason.DecodeError.t()} | {:error, :invalid_data} | {:error, :bad_relationships_type} | {:error, :bad_included_type} | {:error, :relationship_not_found} | {:error, :bad_relationship_data}
Deserialize a payload.
Payload can be a map or a string that will be decoded with Jason.
The return value is {:ok, data} with data beeing the decoded document as a list
or a map. Or {:error, error} if something went wrong when decoding.
Possible errors are:
Jason.DecodeError.t()is an error from json decoding:invalid_datais when"data"is not a map or a list:bad_relationships_typeis when"relationships"is not a map:bad_included_typeis when"included"is not a list:relationship_not_foundis when no relationship could be found for a specified type and id:bad_relationship_datais when the relationship data does not have anidor atypefield