SOLVED.
I have a question regarding the writing/reading list of objects with Gson. I have a Product class:
public class Product {
private String name;
private double price;
private int quantity;
public Product(String name, double price, int quantity) {
this.name = name;
this.price = price;
this.quantity = quantity;
}
......
Then I have a class for ProductList:
public class ProductMenu {
public List<Product> productList;
public ProductMenu() {
productList = new ArrayList<>();
}
......
I initialize my product list in another class:
public class ShoppingOperations {
ProductUpdater import = new ProductUpdaterFromJson();
private ProductMenu shopItems = import.importProducts();
.....
What I wanna do, to initialize my product list in the constructor, like this:
public class ProductMenu {
public List<Product> productList;
public ProductMenu() {
ProductUpdater getList = new ProductUpdaterFromJson();
productList = getList.importProducts();
}
......
This is my method which writes to the .json file the list of objects:
public class ProductUpdaterFromJson implements ProductUpdater {
public final static String PATH = "src\\shop\\products\\promenu.json5";
@Override
public void updateWarehouseData(ProductMenu shopItems) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
try {
Writer writer = new FileWriter(PATH);
gson.toJson(shopItems, writer);
writer.flush();
writer.close();
} catch (IOException e) {
System.out.println("file not found");
e.printStackTrace();
}
}
And the .json file looks like that. It's generated with Gson:
{
"productList": [
{
"name": "Cheese",
"price": 1.37,
"quantity": 6
},
{
"name": "Milk",
"price": 1.16,
"quantity": 7
},
{
"name": "Milka",
"price": 0.66,
"quantity": 2
},
{
"name": "Potatoes",
"price": 0.7,
"quantity": 7
},
{
"name": "Tea",
"price": 2.1,
"quantity": 4
}
]
}
But I'm getting an error trying to read this file. The constructor expects List, but my file writer writes ProductMenu (it's the same list with products), but I have this error - Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ And this is the method to read from .json file:
public final static String PATH = "src\\shop\\products\\promenu.json5";
public List<Product> importProducts2() {
Gson gson = new Gson();
Type userListType = new TypeToken<ArrayList<Product>>(){}.getType();
return gson.fromJson(PATH, userListType);
}
How can I solve this problem. Writer writes , but impossible to read the file and initialize list with products.
The solution I found with help of members:
public class ProductUpdaterFromJson implements ProductUpdater {
public final static String PATH = "src\\shop\\products\\promenu.json5";
@Override
public void updateWarehouseData(ProductMenu shopItems) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
try {
Writer writer = new FileWriter(PATH);
gson.toJson(shopItems.productList, writer);
writer.flush();
writer.close();
} catch (IOException e) {
System.out.println("file not found");
e.printStackTrace();
}
}
public List<Product> importProducts() {
Gson gson = new Gson();
List<Product> list = null;
try {
Reader reader = new FileReader(PATH);
Type userListType = new TypeToken<ArrayList<Product>>(){}.getType();
list = gson.fromJson(reader, userListType);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return list;
}
}
And the constructor:
public class ProductMenu {
public List<Product> productList;
public ProductMenu() {
ProductUpdater listFromJson = new ProductUpdaterFromJson();
productList = listFromJson.importProducts();
}