How can parse this data when one field change of name? - java

I am trying to make several POJOs using Json.fromJson, to parse a String json to a POJO.
To make this I have the following class:
public class Queue {
#SerializedName("reference")
#Expose
private String reference;
#SerializedName("type")
#Expose
private QueuesTypes type;
#SerializedName("desc")
#Expose
private String desc;
#SerializedName("alias")
#Expose
private String alias;
private QueueObjects queueObjects;
}
As you can see all objects have their notations less the last because in this case is not the same.
Sometimes that label should be calls, or whatsapps or tweets, according to the information.
And this Queue Object that can has different attributes to the last object because is merged in the response like this:
{"success":true,"data":[
{"reference":"","type":"","desc":"","alias":"","calls":[{fromPhone:'', toPhone:''}]},
{"reference":"","type":"","desc":"","alias":"","whatsapps": [message:'']},
{"reference":"","type":"","desc":"","alias":"","calls":[fromPhone:'', toPhone:'']},
{"reference":"","type":"","desc":"","alias":"","calls":[fromPhone:'', toPhone:'']},
{"reference":"","type":"","desc":"","alias":"","whatsapps": [message:''],}
{"reference":"","type":"","desc":"","alias":"","fax": [fromFax:'', toFax:'', message:'']}]
So this is a:
public class SocketQueueResponse {
#SerializedName("success")
#Expose
private boolean success;
#SerializedName("data")
#Expose
private List<Queue> listQueue;
}
The problem is how can put several attributes with its several kind ob objects according to the response in QueueClass.
Now I have
public interface QueueObjects {
}
And another class according to the response, but the problem is how can set the notation to QueueObjects.
Thanks.

Related

Sending model class with Boolean values to retrofit endpoint that doesn't sometimes accept that boolean value

I want to ignore a boolean value when sending request all other data types work fine but boolean is set to false by default and is sent even when endpoint doesn't accept it ..
NOTE: I don't want to use transient in GSON because other endpoints accept that boolean value
public class UserModel implements Parcelable {
#SerializedName("agePrivate")
#Expose
private boolean agePrivate;
#SerializedName("username")
#Expose
private String username;
#SerializedName("_id")
#Expose
private String _id;
#SerializedName("user_id")
#Expose
private String user_id;
#SerializedName("email")
#Expose
private String email;
#SerializedName("phone")
#Expose
private String phone;
#SerializedName("birthdayDate")
#Expose
private String birthdayDate;
#SerializedName("job")
#Expose
private String job;
#SerializedName("image")
#Expose
private String image;
#SerializedName("location")
#Expose
private LocationModel location;
private String freind_status;
It's pretty simple, boolean primitive type's default value is always false, You should replace boolean by it's equivalent object type which is Boolean and it's default value is null and null will be ignored by Gson.
So replace:
#SerializedName("agePrivate")
#Expose
private boolean agePrivate;
with
#SerializedName("agePrivate")
#Expose
private Boolean agePrivate;
You can switch to different approach like sending the data value by value by extracting values from the object.
#GET("/endpoint")
Call<List<model>> getdata(#Field("location") String var, ....);
Call that on creating object of call like this
Call<List<model>> call = interface.getdata(obj.getLocation(),...);
Then it will be easy to do this.

I want to remove few fields from JSON result field T result. I am not able to to delete specific fields from T result

As per code given i want to remove few fields from in ResultJSON.
In field I am going pass list of ResultBean.
I am setting fields in result field like resultJson.setResult(ResultBeanList)
Note: ResultBean is not a JSON object. It simple Java bean.
Hope You understand my issue.
public class ResultJson<T> {
private Integer success;
private T result;
private String message;
private String errorCode;
}
public class ResultBean implements Serializable {
private static final long serialVersionUID = 1L;
private String customerName;
private String customerCode;
private String customerAlias;
private String pOR;
private String pOD;
}
We can skip/remove fields in JSON but question how can we delete fields from inside of JSON fields.

How to hide fields with a condition in RESTful WS?

I have a class called Report that I need to share using RESTful WS.
once in full with all its attributes
once in only a reduced version
Normally I'd use something like #XmlTransient to hide the fields, but this would prevent the full version from working.
Is there any way to set a condition or to kind of pre-filter fields just before the output so that it doesn't affect other uses of the same class?
My Report class looks like this:
public class Report {
private String reportId;
private String title;
private String content;
private Date created;
private Date modified;
...
}
The RESTful sharing for the full Report looks like this:
#GET
#Path("/{reportId}")
public Report getReport(#PathParam("reportId") String reportId) {
return Mock.getReport(reportId);
}
The full output I need looks like this:
{
"reportId": "d83badf3",
"title": "The tales of lamaru",
"content": "When once the great Imgur started his journey...",
"created": 1519672434866,
"modified": 1519672434866
}
The short output I need should look like this:
{
"reportId": "d83badf3",
"title": "The tales of lamaru"
}
What is necessary to achieve this?
Why don't you use Inheritance?
Parent
public class Report {
private String reportId;
private String title;
}
Child
public class FullReport extends Report{
private String content;
private Date createdp;
private Date modified;
}
When you need full report set return type as FullReport otherwise Report
Jackson has two different annotations to use when you want to exclude some class members from the JSON serialization and deserialization processes. These two annotations are #JsonIgnore and #JsonIgnoreProperties.
#JsonIgnoreProperties is an annotation at the class level and it expects that the properties to be excluded would be explicitly indicated in the form of a list of strings.
#JsonIgnore instead is a member-level or method-level annotation, which expects that the properties to be excluded are marked one by one.
try this.
public class Report {
private String reportId;
private String title;
#JsonIgnore
private String content;
#JsonIgnore
private Date created;
#JsonIgnore
private Date modified;
...
}

Jackson: remove some values from json and keep some null values

I have a model like this:
public class Employee {
#JsonProperty("emplyee_id")
private Integer id;
#JsonProperty("emplyee_first_name")
private String firstName;
#JsonProperty("emplyee_last_name")
private String lastName;
#JsonProperty("emplyee_address")
private String address;
#JsonProperty("emplyee_age")
private Byte age;
#JsonProperty("emplyee_level")
private Byte level;
//getters and setters
}
now I need to create two JSONs using this (only) model.
the first one must like this for example:
{
"employee_id":101,
"employee_first_name":"Alex",
"employee_last_name":"Light",
"employee_age":null,
"employee_address":null
}
and the second one must like this for example:
{
"employee_id":101,
"employee_level":5
}
by the way, I already tested #JsonIgnore and #JsonInclude(JsonInclude.Include.NON_NULL).
the problem of the first one (as much as I know) is, those fields can't be included in other JSONs (for example if level get this annotation, it won't be included in the second JSON)
and the problem of the second one is, null values can't be included in JSON.
so can I keep null values and prevent some other property to be included in JSON without creating extra models? if the answer is yes, so how can I do it? if it's not I really appreciate if anyone gives me the best solution for this state.
thanks very much.
it could be useful for you using #JsonView annotation
public class Views {
public static class Public {
}
public static class Base {
}
}
public class Employee {
#JsonProperty("emplyee_id")
#JsonView({View.Public.class,View.Base.class})
private Integer id;
#JsonProperty("emplyee_first_name")
#JsonView(View.Public.class)
private String firstName;
#JsonProperty("emplyee_last_name")
#JsonView(View.Public.class)
private String lastName;
#JsonProperty("emplyee_address")
private String address;
#JsonProperty("emplyee_age")
private Byte age;
#JsonProperty("emplyee_level")
#JsonView(View.Base.class)
private Byte level;
//getters and setters
}
in your json response add #JsonView(Public/Base.class) it will return based on jsonview annotations
//requestmapping
#JsonView(View.Public.class)
public ResponseEntity<Employee> getEmployeeWithPublicView(){
//do something
}
response:
{
"employee_id":101,
"employee_first_name":"Alex",
"employee_last_name":"Light",
"employee_age":null,
"employee_address":null
}
for the second one
//requestmapping
#JsonView(View.Base.class)
public ResponseEntity<Employee> getEmployeeWithBaseView(){
//do something
}
response
{
"employee_id":101,
"employee_level":5
}

Nested Objects: Same Parent Object for multiple Child Objects

I am using Retrofit to fetch data from an API which returns JSON Objects in this format:
{
"error": 0
"message": "Request Successful"
"data": [ ... ]
}
I fetch it with GSON to POJO with these classes:
public class SearchResponse {
#SerializedName("error")
#Expose
private Integer error;
#SerializedName("message")
#Expose
private String message;
#SerializedName("data")
#Expose
private List<SearchResult> data;
(Getter and Setter here)
}
public class SearchResult {
#SerializedName("name")
#Expose
private String name;
#SerializedName("id")
#Expose
private Integer id;
....
(Getter and Setter here)
}
The problem is, that for every request I make I have to make two new Classes, even though the outer Class always contains the same three Variables: "error", "message" and "data". Is there any way to use the same Parent for every child without completely removing it? (I still need the "message" field)
You can use generics:
public class SearchResponse<T> { // T is a type variable
#SerializedName("error")
#Expose
private Integer error;
#SerializedName("message")
#Expose
private String message;
#SerializedName("data")
#Expose
private List<T> data; // We have a list of T
(Getter and Setter here)
}
You would deserialize like:
Type t = new TypeToken<SearchResponse<SearchResult>>(){}.getType();
SearchResponse<SearchResult> response = gson.fromJson(jsonString, t);
You would still have to make a TypeToken for each child type though.

Categories