Comments (8)
Hello,
Reason for the NPE
is a fact that you are using readObjectCollection
instead of readObject
;
readObjectCollection()
is expecting a collection of elements to be present:
{
"data" : [
{
"id" : "id",
"type" : "some-type"
}
]
}
If you switch to readObject
method, it should work fine.
I will add proper exception handling in order to communicate these cases accordingly.
from jsonapi-converter.
Hi,
I'm not using readObjectCollection by myself, JSONAPIResponseBodyConverter is doing it.
I'm initializing retrofit this way :
ObjectMapper objectMapper = new ObjectMapper();
return new Retrofit.Builder()
.addConverterFactory(new JSONAPIConverterFactory(objectMapper, Offer.class, OfferVariation.class, Product.class))
.baseUrl("xxx")
.client(client)
.build();
as suggested in the README.md
my Offer object, is something like that:
@Type("offer")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Offer {
@Id
private String id;
should I init retrofit differently ?
from jsonapi-converter.
Hey,
Do you mind sharing your retrofit service interface?
It looks like you defined a method in your service stub that expects List<YourType>
as result and it should be YourType
instead.
from jsonapi-converter.
my bad sorry. My interface didn't match the json :
@GET("offers/current")
@Headers("Content-Type:application/json")
Call<List<Offer>> getCurrentDeal(@Query("locale") String locale, @Query("website_identifier") String websiteId);
while it should be
Call<Offer> getCurrentDeal(@Query("locale") String locale, @Query("website_identifier") String websiteId);
I suggest to check for type of the jsonelement, and throw an addequate exception if it's expecting a list while it's not
thanks a lot for your very fast support
from jsonapi-converter.
No problem at all, I've already pushed change to develop that validates inputs and throws exceptions in case data being parsed is not matching expected inputs.
from jsonapi-converter.
Hi,
I've got a similar issue with the following JSON :
{ "data":[ { "type":"device", "id":4001, "attributes":{ "id":4001, "serial":"0a621724c372cc07", "activationCode":"4000123412341235", "creationDate":1467099796000, "lastCommDate":0 }, "relationships":{ "userDeviceMap":{ "data":[ { "type":"user_device_map", "id":4001 } ] } } } ], "included":[ { "type":"user_device_map", "id":4001, "attributes":{ "id":4001, "active":true, "name":"Device1", "ack":true, "creationDate":1467099796000 } } ] }
When I try to deserialize this JSON, I got the following error :
java.lang.NullPointerException: Attempt to invoke virtual method 'void java.lang.reflect.Field.set(java.lang.Object, java.lang.Object)' on a null object reference at com.github.jasminb.jsonapi.ResourceConverter.setIdValue(ResourceConverter.java:523) at com.github.jasminb.jsonapi.ResourceConverter.readObject(ResourceConverter.java:297) at com.github.jasminb.jsonapi.ResourceConverter.parseRelationship(ResourceConverter.java:483)
This JSON is obtained through a Retrofit client created like this :
this.retrofit = new Retrofit.Builder().baseUrl("xxx) .addConverterFactory(new JSONAPIConverterFactory(new ResourceConverter(Device.class))) .client(new OkHttpClient.Builder().build()) .build();
My Device class looks like this :
@Type("device")
@Parcel
public class Device extends DeviceBeforeCreation implements Model {
@Id String id;
// SOME ATTRIBUTES HERE
@Relationship("userDeviceMap")
private UserDeviceMap userDeviceMap;
// GETTERS AND SETTERS
}
And my "UserDeviceMap" looks like this :
@Type("user_device_map")
@Parcel
@JsonIgnoreProperties(ignoreUnknown
= true)
public class UserDeviceMap {
@Id private String id;
public UserDeviceMap() { }
// GETTERS AND SETTERS
}
I don't know if there is something missing from the JSON, or if my client configuration is wrong... I just need to read the data from the "included" block inside my UserDeviceMap object...
Thanks for your help !
EDIT : The Retrofit interface looks like this :
public interface DeviceService { @GET("users/{userId}/boxes/{boxId}/devices") Call<List<Device>> getDevices(@Path("userId") int userId, @Path("boxId") int boxId); }
When I remove the @relationship annotation, I'm able to get the serialization working...
from jsonapi-converter.
Hey @dodos90, UserDeviceMap
also needs to be registered in ResourceConverter
when creating it (besides Device
class).
Edit:
It looks like that one device can have many device map entries, so your model should look like this:
@Type("device")
@Parcel
public class Device extends DeviceBeforeCreation implements Model {
@Id String id;
// SOME ATTRIBUTES HERE
@Relationship("userDeviceMap")
private List<UserDeviceMap> userDeviceMap;
from jsonapi-converter.
Oh yeah ! I didn't really understand that we need to add the compound classes (like UserDeviceMap
) to the ResourceConverter
as well. Now it works perfectly, thanks !
from jsonapi-converter.
Related Issues (20)
- How to generate this nested JSON (tenor) ? HOT 1
- Should ALLOW_UNKNOWN_TYPE_IN_RELATIONSHIP be less restrictive HOT 7
- @Id obfuscated by proguard even after excluding the annotation HOT 2
- POST Example with RestTemplate HOT 2
- How to configure ObjectMapper? HOT 2
- @Type serialization HOT 2
- Document collection always throw error while using readCollections HOT 1
- Object with relations itself HOT 1
- @Relationship is not being deserialized HOT 2
- Creating a resource on the server - required id field HOT 2
- Does the library support Javas webclient and the reactor implementation? HOT 2
- "jsonapi": { "version": "1.0" } Parser HOT 2
- Get value above "attributes" level HOT 2
- Relationship attributes dont serialized HOT 3
- The library does not support JSON Specification "8.3 Inclusion of Related Resources" HOT 13
- Class doesn't have a Type annotation error, even though the class does have one HOT 3
- Proguard issues with jsonapi-converter when Android project to targets java 17 HOT 2
- InvalidJsonApiResourceException: Resource must contain at least one of 'data', 'error' or 'meta' nodes
- Handling local identifier ("lid") when creating resource HOT 4
- Backward compatibility HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonapi-converter.