Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fabienhenon/jsonapi-deserializer
JsonApi Deserializer for Elixir
https://github.com/fabienhenon/jsonapi-deserializer
Last synced: about 2 months ago
JSON representation
JsonApi Deserializer for Elixir
- Host: GitHub
- URL: https://github.com/fabienhenon/jsonapi-deserializer
- Owner: FabienHenon
- License: mit
- Created: 2019-04-17T10:26:59.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-04-17T13:18:01.000Z (over 5 years ago)
- Last Synced: 2024-10-30T16:56:09.764Z (about 2 months ago)
- Language: Elixir
- Homepage:
- Size: 7.81 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JsonApiDeserializer
Json api deserializer able to deserialize json api documents with relationships for Elixir projects.
## Installation
The package can be installed by adding `jsonapi_deserializer` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:jsonapi_deserializer, "~> 0.1.0"}
]
end
```Documentation [https://hexdocs.pm/jsonapi_deserializer](https://hexdocs.pm/jsonapi_deserializer).
## Example
Using the json api deserializer with a payload like this:
```
{
"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": "[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": {}
}
]
}
```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" => "[email protected]",
"id" => "22208770-76dd-47e5-a1c4-4d0d9c2483ab"
},
%{
"__metadata" => %{
"links" => %{"self" => "http://link-to-comment/3"},
"type" => "comment"
},
"content" => "Comment 3 content",
"email" => "[email protected]",
"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" => "[email protected]",
"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"
}
]
```