Giter Site home page Giter Site logo

Comments (8)

jasminb avatar jasminb commented on August 16, 2024

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.

fabricehong avatar fabricehong commented on August 16, 2024

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.

jasminb avatar jasminb commented on August 16, 2024

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.

fabricehong avatar fabricehong commented on August 16, 2024

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.

jasminb avatar jasminb commented on August 16, 2024

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.

dodos90 avatar dodos90 commented on August 16, 2024

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.

jasminb avatar jasminb commented on August 16, 2024

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.

dodos90 avatar dodos90 commented on August 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.