Closed maycowmeira closed 7 years ago
Hi, I have the following json:
{"meta": {"app": "catabicho-api", "version": "0.0v", "easter_egg": "You found it! You can redeem your gift in www.catabicho.com.br/no-way-cowboy-haha", "authors": [{"name": "Julia Ulson Tretel"}, {"name": "Yudi Matsuzake"}, {"name": "Maycow Thomaz"}]}, "data": [{"relationships": {"breeds": [{"data": {"attributes": {"breed": "Labrador"}, "type": "breeds", "id": 0}, "links": {"self": "/api/breeds/0"}}, {"data": {"attributes": {"breed": "Pastor Alemão"}, "type": "breeds", "id": 1}, "links": {"self": "/api/breeds/1"}}, {"data": {"attributes": {"breed": "Vira Lata"}, "type": "breeds", "id": 2}, "links": {"self": "/api/breeds/2"}}, {"data": {"attributes": {"breed": "Poodle"}, "type": "breeds", "id": 3}, "links": {"self": "/api/breeds/3"}}]}, "attributes": {"species": "Cão"}, "type": "species", "id": "0", "links": {"self": "/api/species/0"}}, {"relationships": {"breeds": [{"data": {"attributes": {"breed": "Persa"}, "type": "breeds", "id": 4}, "links": {"self": "/api/breeds/4"}}, {"data": {"attributes": {"breed": "Siamês"}, "type": "breeds", "id": 5}, "links": {"self": "/api/breeds/5"}}]}, "attributes": {"species": "Gato"}, "type": "species", "id": "1", "links": {"self": "/api/species/1"}}, {"relationships": {"breeds": null}, "attributes": {"species": "Furão"}, "type": "species", "id": "2", "links": {"self": "/api/species/2"}}, {"relationships": {"breeds": null}, "attributes": {"species": "Hamster"}, "type": "species", "id": "3", "links": {"self": "/api/species/3"}}, {"relationships": {"breeds": null}, "attributes": {"species": "Chinchila"}, "type": "species", "id": "4", "links": {"self": "/api/species/4"}}, {"relationships": {"breeds": [{"data": {"attributes": {"breed": "Azulão"}, "type": "breeds", "id": 6}, "links": {"self": "/api/breeds/6"}}, {"data": {"attributes": {"breed": "Curió"}, "type": "breeds", "id": 7}, "links": {"self": "/api/breeds/7"}}]}, "attributes": {"species": "Pássaro"}, "type": "species", "id": "5", "links": {"self": "/api/species/5"}}, {"relationships": {"breeds": {"data": {"attributes": {"breed": "Tarântulas"}, "type": "breeds", "id": 8}, "links": {"self": "/api/breeds/8"}}}, "attributes": {"species": "Aranha"}, "type": "species", "id": "6", "links": {"self": "/api/species/6"}}], "jsonapi": {"version": "1.0v"}}
I've created the following Models according to the specifications:
@JsonApi(type = "species") public class Specie extends Resource { private String specie; private HasMany<Breed> breeds; } @JsonApi(type = "breeds") public class Breed extends Resource { private String breed; private HasOne<Specie> specie; }
And a "controller" class to deserialize it.
public class SpecieController { private JsonAdapter.Factory jsonApiAdapterFactory; private Moshi moshi; private Specie[] allSpecies; public SpecieController(){ jsonApiAdapterFactory = ResourceAdapterFactory.builder() .add(Specie.class) .add(Breed.class) .build(); moshi = new Moshi.Builder() .add(jsonApiAdapterFactory) .build(); } public Specie[] deserializeAllSpecies(String json){ try { allSpecies = moshi.adapter(Specie[].class).fromJson(json); } catch (IOException e) { System.out.println(e.getMessage()); e.printStackTrace(); } return allSpecies; } }
But, when I call the function deserializeAllSpecies(String json) with that JSON I have the following err:
deserializeAllSpecies(String json)
com.squareup.moshi.JsonDataException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at path $.relationships.breeds
Did I do something wrong ?
The JSON you tries to deserialize does not conform to JSON API specification. Check about relationships in JSON API.
relationships
Hi, I have the following json:
I've created the following Models according to the specifications:
And a "controller" class to deserialize it.
But, when I call the function
deserializeAllSpecies(String json)
with that JSON I have the following err:Did I do something wrong ?