Facing issues when using #JsonCreator with nested Json objects - java

I have some old code which is working fine but now i want to use #JsonCreator in domain objects and i am able to re-factor all stuff but i am now stucked in one issue. In one scenario i have Json Structure like:
[{
"name" : "John",
"emailAddress" :"stucked#gmail.com"
}]
But in java backend i have structure like:
class Employee {
public final String name;
public final EmailAddress emailAddress;
Employee(final String name, final EmailAddress emailAddress) {
this.name = name;
this.emailAddress = emailAddress;
}
Employee() {
this(null, null)
}
}
class EmailAddress {
public final String address;
EmailAddress(final String address) {
this.address = address;
}
EmailAddress() {
this(null);
}
}
So I am trying like :
class Employee {
public final String name;
public final EmailAddress emailAddress;
#JsonCreator
Employee(#JsonProperty("name")final String name,
#JsonProperty("emailAddress")final EmailAddress emailAddress) {
this.name = name;
this.emailAddress = emailAddress;
}
}
class EmailAddress {
public final String address;
#JsonCreator
EmailAddress(#JsonProperty("address")final String address) {
this.address = address;
}
}
Now the problem is in using #JsonProperty with "emailAddress" field in Employee domain object and then #JsonProperty inside EmailAddress Object with "address" because in Json i dont have any "address" property and due to which JsonParsing error is coming.
Well problem can be easily solved if i declare emailAddress as String instead of EmailAddress and remove EmailAddress object but address field of EmailAddress object is used at many other places, modification of which require lot of effort and there are lots of such cases. Is there any other solution??

So what exactly is your problem here? If the goal is to allow both of these:
"stucked#gmail.com"
{ "address" : "stucked#gmail.com" }
it should already work: Jackson figures out that single-String constructor may be used if JSON String is found; and setters/fields if a JSON Object is found. You just need to ensure names match ("address" vs "emailAddress").

Related

Indicate certain parameters in method as mandatory

I am making a SDK using java where a typical method exposed to client would look like this
public void createUser(String name, String email ,int age, String bio, String address, String phoneNumber){
/*
body
*/
}
So the problem is that only name and email fields are mandatory (not null or empty) and the rest are just optional (null values are allowed). Other than overriding and throwing a run time exception inside the method, Is there any way to properly indicate to the client calling the sdk function that the name and email fields are mandatory ?
A widely used library for that is Project Lombok and the provided annotation #NonNull. Your method would look as follows.
public void createUser(#NonNull String name, #NonNull String email ,int age, String bio, String address, String phoneNumber){
/*
body
*/
}
If the method is called with null values for those fields a compilation error will occur.
FYI: For your case I would consider using the Builder Design Pattern as not all of your parameters are mandatory. This will make your "SDK" nicer to use.
Java unfortunately doesn't support optional parameters. One solution to this might be to use a Builder Pattern. You create a separate UserBuilder class, which only takes the required arguments in its constructor. Other arguments are passed through set functions. This also allows you to give some of the parameters default values. When all the parameters are set, you call UserBuilder.getUser() and your User object is created for you. Typically you also make sure the User constructor is not available elsewhere.
class User{
String name, email, bio, address, phoneNumber;
int age;
User(String name, String email, int age, String bio, String address, String phoneNumber){
this.name = name;
this.email = email;
this.age = age;
this.bio = bio;
this.address = address;
this.phoneNumber = phoneNumber;
}
}
class UserBuilder{
String name, email, bio, address, phoneNumber;
int age;
public UserBuilder(String name, String email){
this.name = name;
this.email = email;
}
public void setBio(String bio) {
this.bio = bio;
}
public void setAddress(String address) {
this.address = address;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public void setAge(int age) {
this.age = age;
}
public User getUser(){
return new User(name, email,age,bio,address,phoneNumber);
}
}
For example, this is how you would create a User object with the builder class if you only wanted to set name, age, and email:
UserBuilder builder = new UserBuilder(name,email);
builder.setAge(38);
builder.getUser();
You can add #NonNull annotation before name and e-mail id.
#NonNull – The compiler can determine cases where a code path might receive a null value, without ever having to debug a NullPointerException.

ReST resource/model with optional fields

I am trying to learn how to implement a CRUD ReST API. I created a simple application using JAX-RS and am testing my HTTP methods using Postman. My question is, how do I define optional fields for a model for POST method?
ex. Person contains firstName, lastName as required fields, and age, gender as optional fields.
#XmlRootElement
public class Person {
private long id;
private String firstName;
private String lastName;
public Person() {}
public Person(long id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
//getter and setters omitted
Above is a sub-resource to another resource, and below addPerson service method is used to POST to a HashMap.
public Person addPerson(long userId, Person person) {
Map<Long, Person> persons = users.get(userId).getPersons();
person.setId(persons.size() + 1);
persons.put(person.getId(), person);
return person;
}
I thought of constructing a model whose constructor has multiple different combination of parameters to instantiate a model class, but that doesn't seem very efficient. Can somebody advise?
FURTHER EDIT: This needs to be done while processing JSON. Given two JSON formatted request:
{
"firstName": "Jay",
"lastName": "Kay",
"age": "13"
}
and
{
"firstName": "Jay",
"lastName": "Kay"
}
both should be processable since age(as well as gender) is an "optional attributes" so it may be either omitted, or have a value. I'm coming across JSON parsers and trying to read through them since that may be an answer to processing such requests.
Add gender and age to your Person class
#XmlRootElement
public class Person {
private long id;
private String firstName;
private String lastName;
private int age;
private String gender;
public Person() {}
public Person(long id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
}
While making request from Postman, include the age and gender as keys in x-www-form-urlencoded.
By default, the value of integer is 0 and value for String is empty string.
In your method to handle the request, check the values for gender and age.
If the values are provided in the request, then use the setters of Person class to set the values accordingly.
Suppose the following is your method to handle the request :
#POST
#Path("/AddPerson")
#Produces(MediaType.TEXT_PLAIN)
public String addPerson(#FormParam("id") long id,
#FormParam("firstName") String firstName,
#FormParam("lastName") String lastName,
#FormParam("age") int age,
#FormParam("gender") String gender) {
Person person = new Person(id,firstName,lastName);
if(age != 0) {
person.setAge(age);
}
if(!gender.equals("")) {
person.setGender(gender)
}
// Assuming that PersonService class has the addPerson method
PersonService.addPerson(1,person)
return "Person with id "+ id + " added";
}

Gson - Read a value with two different keys

In my Android project I have two types of response where both response are identical except two keys.
Response 1
{"fullName":"William Sherlock Scott Holmes","address":"221B Baker Street, London, England, UK","downloads":642,"rating":3,"repos":["https://link1","https://link2","https://link3"]}
Response 2
{"name":"Sherlock","city":"London","downloads":642,"rating":3,"repos":["https://link1","https://link2","https://link3"]}
If you see the responses only two key names are changing fullName/name and address/city
I don't want to create one more pojo for other response. My question is: is it possible to use only one Pojo to read both responses?
public class AccountInfo {
private String name;
private String city;
//other objects
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
//other setters and getters
}
Any help will be appreciated...
You can annotate the members to accept values from two different json names using the #SerializedName annotation:
#SerializedName(value = "name", alternate = {"fullName"})
private String name;
#SerializedName(value = "city", alternate = {"address"})
private String city;
Either named element can then be placed into the members that are annotated like this.
UPDATED :
#SerializedName alternate names when deserializing is added in Version 2.4
Yes, you can totally use one POJO class for deserializing both responses. Your POJO class will contain keys from both responses.
public class Response {
private String name;
private String city;
private String fullName;
private String address;
private Integer downloads;
private Integer rating;
private List<String> repos ;
}
But when using the Response class, be careful that for first response, the name and city will be null, and for the second one, the address and fullname.
Yeah you can do that in a single POJO. Try this:
public class POJO {
#SerializedName("name")
public String name;
#SerializedName("city")
public String city;
#SerializedName("fullName")
public String fullName;
#SerializedName("address")
public String address;
#SerializedName("downloads")
public Integer downloads;
#SerializedName("rating")
public Integer rating;
#SerializedName("repos")
public List<String> repos = new ArrayList<String>();
}
While parsing you have to check values for null. For eg -
While Parsing Response 1: name and city variables will be null
While Parsing Response 2: fullname and address will be null
Note : Try checking values for null before using else you'll get nullpointerexception
Define all possible fields in your POJO Class like
public class AccountInfo {
private String name;
private String city;
private String fullname;
private String address;
}
While performing operation check for null in those feilds

Java trouble serializing a field of an object

I'm trying to learn about serialization and encountered the following problem:
I have an implementation of a customer that looks somewhat like this.
private static customerCount = 0;
private String customerID;
private String name;
private String street;
private String city;
private String postcode;
private String type;
I'm trying to serialize / deserialize an Arraylist
In the constructor, the ID will be created like this:
private Customer(...){
this.customerID = "ID" + customerCount;
customerCount++;
}
The serialization process works, however, all the IDs are set to ID0 when I deserialize.
Can anyone help resolve this problem?
Update: Alright, I just found out that static fields wont be serialized. How can I "model" the ID of a customer so I can serialize it? I need to have a unique value to create IDs for customers.
Here's a solution that combines the factory with the list that keeps track of customer count.
The customer class has a protected constructor, forcing you to build them through another means within the same package.
public class Customer implements Serializable {
private String customerID;
private String name;
private String street;
private String city;
private String postcode;
private String type;
protected Customer(String customerID,
String name,
String street,
String city,
String postcode,
String type) {
this.customerID = customerID;
this.name = name;
this.street = street;
this.city = city;
this.postcode = postcode;
this.type = type;
}
}
Now within the package, create a list wrapper like this:
public class CustomerList {
private int customerCount = 0;
private List<Customer> customers = new ArrayList<>();
public boolean addCustomer(String name,
String street,
String city,
String postcode,
String type) {
Customer customer = new Customer("ID" + customerCount++,
name,
street,
city,
postcode,
type);
return customers.add(customer);
}
}
This class then takes care of constructing the new customer, and provides a unique ID.
Edit: Just noticed that you now also have the upside of making the CustomerList class serializable as well. Then you can load it and still have an accurate customer count for adding additional uniquely ID-ed customers.
Usually, you would like to serialize only attributes and their values, not the logic from the class. Logic should happen before serialization or after deserialization.

What GSON deserializers do I need?

Evening,
I retrieve JSON data from a server in this format:
Json-Book:
{
_id: String,
isbn: String,
owner: String username,
rentedTo: Array[String usernames]
}
Json-Crowd:
{
_id: String,
isbn: String,
owner: String username,
availableForRent: Integer,
rentedTo: Array[String usernames]
}
Json-User:
{
username: String,
books: Array[Book],
crowds: Array[Crowd]
}
Similarly, I have three classes:
public class Book{
private String _id;
private String isbn;
private User owner;
private ArrayList<User> rentedTo;
public Book(String _id, String isbn, User owner, ArrayList<User> rentedTo) {
this._id = _id;
this.isbn = isbn;
this.owner = owner;
this.rentedTo = rentedTo;
}
}
public class Crowd {
private String _id;
private String name;
private User owner;
private ArrayList<User> members;
public Crowd(String _id, String name, User owner, ArrayList<User> members) {
this._id = _id;
this.name = name;
this.owner = owner;
this.members = members;
}
}
public class User {
private String name;
private Shelf shelf;
private ArrayList<Book> books;
private ArrayList<Crowd> crowds;
public User(String name, ArrayList<Book> books, ArrayList<Crowd> crowds) {
this.name = name;
this.booksOwned = books;
this.crowds = crowds;
}
}
As you can see, all the field names match up, but not all of the types. Users in the keys owner and rentedTo in Json-book and Json-crowd have values of type String and ArrayList<String> respectively, where the strings are unique usernames. In the classes, these fields are of type User and ArrayList<User>. In and of itself, this is no problem, because I can get the User object from the string with this method:
public User getUser(String username) {
return users.get(username);
}
Now, I'm a bit confused as to what deserializers I need in order to get proper objects from the JSON data. How would the, uh, architecture of this look? Deserializers for each class Book, Crowd and User, each of which fetches one field at a time from the Json-data and calls the constructor?
I can't wrap my head around how this would work together. The deserializer for User would need to use/reference the deserializer for Crowd, but do I another deserializer since the objects are in array?
I assume I would need an opposite set of serializers in order to get the classes into Json-data of the correct format.

Categories