Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help: Is there any options to parse this json? Generic type. #221

Open
ExtinctAmoeba opened this issue Aug 14, 2019 · 2 comments
Open

Help: Is there any options to parse this json? Generic type. #221

ExtinctAmoeba opened this issue Aug 14, 2019 · 2 comments

Comments

@ExtinctAmoeba
Copy link

Json: https://jsonblob.com/96555bba-be78-11e9-bd8b-cdbff37ad6cb

It has generic type and I cannot find any solution for this issue.

@ExtinctAmoeba ExtinctAmoeba changed the title Help: Is there any options to parse this json? Help: Is there any options to parse this json? Generic type. Aug 14, 2019
@ExtinctAmoeba
Copy link
Author

@jasminb Can You please help me with this?

@jasminb
Copy link
Owner

jasminb commented Aug 20, 2019

Hello @ExtinctAmoeba, this can be achieved in following way:

Write a base class like this (having in mind that attributes are similar in both sub-types, your base class can hold all of the common attributes, below is a just simple example):

public class BaseModel {

	@Id
	private String id;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
}

And than write two different classes for each of your types (Movie and Vod):

@Type("Movie")
public class Movie extends BaseModel {
	private Integer categoryId;

	public Integer getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}
}
@Type("Vod")
public class Vod extends BaseModel {

	private Integer categoryId;

	public Integer getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}
}

Make sure to register both types with ResourceConverter instance.

To handle response, do:

JSONAPIDocument<List<BaseModel>> elements = converter.readDocumentCollection(data, BaseModel.class);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants