Mapping a single JSON object to three different JAVA classes? - java

Can any one help me out in this , i'm new to JSON . The problem is that i want to map a single json objects Keys values pair to three different java classes?
{"firstName":"Sasi","lastName":"Dunston","jobTitle":"Trainee","dateOfBirth":"13/09/1990","bloodGroup":"O+ve",
"listOfEmail":["dunston08#gmail.com","charles.gmail.com","ravi.gmail.com"],
"listOfURL":["www.google.com","www.gmail.com","www.facebook.com"],
"listOfFaxNo":[8888888888,1111111111,2222222222,3333333333],
"listOfOfficeNo":[9999999999,8888888888,7777777777,6666666666],
"listOfAddress":[{"streetName":"xxxxx","city":"yyyyy","zipCode":"5555555","state":"hhhhhhh"},
{"streetName":"xxxxx","city":"yyyyy","zipCode":"5555555","state":"hhhhhhh"}]
}
this is my json object i want to map it to three different classes
Class PersonDetail
{
firstName
lastName
jobTitle
dateOfBirth
bloodGroup
/* Getter Setter */ of the above attributes
}
class PersonContact extends PersonDetail
{
ArrayList<String> listOfEmail=new ArrayList<String>();
ArrayList<String> listOfURL=new ArrayList<String>();
ArrayList<String> listOfFaxNo=new ArrayList<String>();
ArrayList<String> listOfPhoneNo=new ArrayList<String>();
/* Getter Setter */ of the above attributes
}
class Address extends PersonContact
{
String streetName;
String city;
String zipcode;
String state;
/* Getter Setter */ of the above attributes
}

Not tested, but the you should be able to map your json to PersonContact using the following class definitions:
Class PersonDetail
{
protected String firstName;
protected String lastName;
protected String jobTitle;
protected String dateOfBirth;
protected String bloodGroup;
/* Getter Setter */ of the above attributes
}
class PersonContact extends PersonDetail
{
private ArrayList<String> listOfEmail;
private ArrayList<String> listOfURL;
private ArrayList<String> listOfFaxNo;
private ArrayList<String> listOfOfficeNo;
private List<Address> listOfAddress;
/* Getter Setter */ of the above attributes
}
class Address
{
private String streetName;
private String city;
private String zipcode;
private String state;
/* Getter Setter */ of the above attributes
}

Just use https://github.com/FasterXML/jackson-databind
The tutorial should enable you to map the same source to different java objects (classes)

Related

Map multiiple Java pojo fields to one json field with Jackson annotations

I'm trying to map two fields from Java POJO to one json field;
public class Person {
private String firstName;
//this two fields should be in separate json property (object)
private String street;
private String streetNo;
...
//getters and setters
}
And I want to get response something like this:
{
firstName: "Peter",
address: {
street: "Square nine",
streetNumber: "12"
}
}
You should implement then another POJO Address and add address field to your Person POJO
public class Person {
private String firstName;
private Address address = new Address();
...
//getters and setters
}
// another POJO
public class Address {
private String street;
private String streetNo;
//getters and setters
}

How to find differences between two collections

I have following DTOs:
#Data
public class PersonDTO implements Diffable<PersonDTO> {
private String id;
private String firstName;
private String lastName;
private List<AddressDTO> addresses;
#Override
public DiffResult diff(PersonDTO personDTO) {
return new DiffBuilder(this, personDTO, SHORT_PREFIX_STYLE)
.append("id", this.id, personDTO.getId())
.append("firstName", this.firstName, personDTO.getFirstName())
.append("lastName", this.lastName, personDTO.getLastName())
.append("addresses", addresses, personDTO.getAddresses())
.build();
}
}
#Data
public class AddressDTO implements Diffable<AddressDTO> {
private String id;
private String personId;
private String addressType;
private String street;
private String houseNumber;
private String postalCode;
private String city;
private String countryId;
#Override
public DiffResult diff(AddressDTO addressDTO) {
return new DiffBuilder(this, addressDTO, SHORT_PREFIX_STYLE)
.append("id", this.id, addressDTO.getId())
.append("personId", this.personId, addressDTO.getPersonId())
.append("addressType", this.addressType, addressDTO.getAddressType())
.append("street", this.street, addressDTO.getStreet())
.append("houseNumber", this.houseNumber, addressDTO.getHouseNumber())
.append("postalCode", this.postalCode, addressDTO.getPostalCode())
.append("city", this.city, addressDTO.getCity())
.append("countryId", this.countryId, addressDTO.getCountryId())
.build();
}
}
My main goal is to find differences between two similar person objects. Currently I've tried to use Diffable interface from apache commons which is perfectly good for object. Please advise how to deal with collections when size of each collection can be different. For instance few addresses were removed, few was added and few was updated. Please see example below:
Probably there is another library which helps to achieve similar goals, please advice
source can be your first object
target can be your second object
Iterator targetIt = target.iterator();
for (Object obj:source)
if (!obj.equals(targetIt.next())
// Element has changed

Mapstruct mapping - String to List<String>

I am struggling to map a string object from source(Relation.class) and to a List of target(RelationListDTO.class) .
Relation.java
public class Relation {
private String name;
private String email;
private String completeAddress;
// getters and setters
}
RelationListDTO.java
public class RelationListDTO {
private String name;
private String email;
private List<Address> address;
// getters and setters
}
Address.java
public class Address{
private String street;
private String city;
// getters and setters
}
Mapper class
#Mapper
public interface RelationMapper {
#Mapping(source = "completeAddress", target = "address.get(0).city")
RelationListDTO relationToListDto(Relation relation);
}
But it is not working. Could anyone please help.
What you are trying to do using MapStruct is not possible. Because MapStruct doesn't work with run time objects. MapStruct only generated plain java code for mapping two beans. And I find your requirement is little unique. You have a list of Addresses but want to map only city from source object? You can still do like this
#Mapping( target = "address", source = "completeAddress")
RelationListDTO relationToListDto(Relation relation);
// MapStruct will know to use this method to map between a `String` and `List<Address>`
default List<Address> mapAddress(String relation){
//create new arraylist
// create new AddressObject and set completeAddress to address.city
// add that to list and return list
}
Not sure if this was possible at the time of the accepted answer but I had the same problem as you and ended up doing it this way.
#Mapper(imports = Collections.class)
public interface RelationMapper {
#Mapping(expression = "java(Collections.singletonList(relation.getCompleteAddress()))", target = "address")
RelationListDTO relationToListDto(Relation relation);
}

With Android Room, do I need to add setter and getter for nested object?

In the code below, class Address is nested in Entity User. I wonder if all the attributes of Address are private, do we need getter and setter for each of the field in Address? Notice there is a List<String>, so I'm not sure if Room will work well with #TypeConverter in this case.
public class Address {
public String street;
public String state;
public List<String> city;
#ColumnInfo(name = "post_code")
public int postCode;
}
#Entity
public class User {
#PrimaryKey
public int id;
public String firstName;
#Embedded
public Address address;
}
You can easily add getter/setters with #Ignore annotation and the converter will ignore these methods.
#Ignore
public List<String> getCity() {
return city;
}
You can refer here
Create the entity

How to map ArrayList data to an DTO

As per the requirement, I have parsed an XML file and set data into these two DTO classes:
public class DetailsDTO implements java.io.Serializable {
private String userid;
private String accountnum;
private Customer customer;
// setters and getters
public class Customer implements Serializable
{
private String street;
private String country;
// setters and getters
After adding data of the Customer class to DetailsDTO, I added this DetailsDTO to an ArrayList as shown:
List list = new ArrayList();
// and added these DetailsDTO class to an ArrayList
list.add(detailsDTO)
Now there is a Master DTO called as WholeDetails which consists of all variables defined in various DTO classes as shown.
class WholeDetails
{
private String userid;
private String accountnum;
private String street;
private String country;
}
Now, as you see, all the data is aviable within the ArrayList.
How can I extract the contents from ArrayList and map it to the WholeDetails?
You will have to do the mapping e.g.
List<DetailsDTO> list = new ArrayList<DetailsDTO>();
// and added these DetailsDTO class to an ArrayList
list.add(detailsDTO);
List<WholeDetails> wholeDTOList = new ArrayList<WholeDetails>();
for(DetailsDTO dto:list){
WholeDetails whole = new WholeDetails();
whole.setUserid(dto.getUserid());
whole.setAccountNum(dto.getAccountNum());
whole.setStreet(dto.getCustomer().getStreet());
whole.setCountry(dto.getCustomer().getCountry());
wholeDTOList.add(whole);
}
If you like it to be more short you could create an adapter class that maps the DetailsDTO to the WholeDetailsDTO and add the result object to the list

Categories

Resources