i'm learning how to convert array to json file and import that array with using gson. I have abstract class Shape;
public abstract class Shape {
private Type typeOfShape;
public abstract double countArea();
public abstract double countCircuit();
}
and classes Circle, Rectangle, Square which extends Shape, for example Rectangle
public class Rectangle extends Shape {
private double a;
private double b;
public Rectangle(Type type, int a, int b) {
super(type);
this.a = a;
this.b = b;
}
@Override
public double countArea() {
return a * b;
}
@Override
public double countCircuit() {
return a * 2 + b * 2;
}
and 2 methods:
public static List<Shape> importShapeListFromJsonWithGson(String path) throws IOException {
Type listType = new TypeToken<List<Shape>>() {}.getType();
Gson gson = new GsonBuilder().create();
Reader reader = new FileReader(path);
List<Shape> result = gson.fromJson(reader, listType);
return result;
}
public static void exportShapeListToJsonWithGson(List<Shape> list, String path) throws IOException {
Writer writer = new FileWriter(path);
Gson gson = new GsonBuilder().create();
gson.toJson(list, writer);
writer.close();
}
But i'm getting Exception in thread "main" java.lang.RuntimeException: Failed to invoke public Shape() with no args . Is the problem that, i'm using abstract class? I tried to remove abstract from Shape, and then compile one more time, but i was getting only list with shapes, without for example "a" or "b", only, with type.