I am a newbie to Gson library, not sure why this object to json converter works weird. I have code something similar to the below
public class A implements Serializable{
@Expose
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class B extends A implements Serializable{
@Expose
private int x;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
}
public class MainclassTester{
public static void main(String[] args){
public B b = new B();
b.setName("Point");
b.setX(2);
final Gson gson = new GsonBuilder().create();
System.out.println(gson.toJson(b));
}
}
I want the output to be something like this
{
"name":'Point',
"x":2
}
but I get the output
{
"name":'Point'
}
My subclass variables are not serialized properly.