I get json which I need to parse to object with GSON:
"product":{
"product_type":"assignment",
"id":717,
"product_profile":{
"title":"new Order from java",
"info":"Some special info",
"dtl_expl":true,
"special_info":""
}
}
"product_profile" will be different object depend on what I get in "product_type". I created classes for each of that objects - Assignment and Writing. They are child of ProductType class. I implement interface in Product class which should return right object - Assignment or Writing depend on what value is in "product_type". My Product class
public class Product implements IProductType{
ProductAssignment prodAss;
ProductWriting prodWr;
ProductType returnState;
@SerializedName("id")
int id;
@SerializedName("product_type")
String product_type;
@SerializedName("product_profile")
ProductType product_profile;
public Product()
{
}
public Product(int id, String product_type, ProductType product_profile)
{
this.id = id;
this.product_type = product_type;
this.product_profile = product_profile;
}
public int getProductId()
{
return this.id;
}
public String getProductType()
{
return this.product_type;
}
public ProductType getProduct()
{
return this.returnObject(product_type);
}
public void setProductId(int id)
{
this.id = id;
}
public void setProductTitle(String product_type)
{
this.product_type = product_type;
}
public void setProduct(ProductType product_profile)
{
this.product_profile = this.returnObject(product_type);
}
@Override
public String toString() {
return "id=" + id + " " + "title=" + product_type
+ " " + "profile=" + product_profile + "}";
}
@Override
public ProductType returnObject(String res)
{
System.out.println("Product");
System.out.println(product_profile);
if (res.equals("assignment"))
{
this.product_profile = new ProductAssignment();
System.out.println("I'm going to parse Assignment");
}
else if (res.equals("writing"))
this.product_profile = new ProductWriting();
return product_profile;
}
}
But when I try to parse json to object like this:
Gson gson = new Gson();
Product product = gson.fromJson(res,Product.class);
I get such a product object:
id=447 title=assignment profile=ProductType@e49f9fa
so "id" and "product_type" parse correctly, but it is not Assignment object in ProdutType.
My ProductType class:
public class ProductType implements IProductType{
String product;
static ProductType productType;
static ProductAssignment productAssignment;
static ProductWriting productWriting;
IProductType component;
ProductType returnState;
ProductAssignment prodAss;
ProductWriting prodWr;
public ProductType()
{
}
public ProductType(IProductType c)
{
component = c;
}
public ProductType getProductType()
{
return this.returnState;
}
public void setProductType(ProductType returnState)
{
this.returnState = returnState;
}
@Override
public ProductType returnObject(String product_type)
{
if (product_type.equals("assignment"))
{
this.returnState = new ProductAssignment();
System.out.println("I'm going to parse Assignment");
}
else if (product_type.equals("writing"))
this.returnState = new ProductWriting();
return returnState;
}
@Override
public String toString()
{
return returnState.getClass().getName();
}
}