I have a "Product" POJO class in my app. A Product object can either be created in-app by the user or by parsing a json that comes from the API.
Example:
JSON productJSON = networkCommunicator.getJSONFromStream();
Product product = new Product();
product.setId(productJSON.getInt("id");
product.setName(productJSON.getString("name");
I want to create a fromJSON constructor in the Product class, but I dont know whether it is a good idea, because it couples the Product class to the API implementation of the JSON structure and the names of the keys in the JSON:
class Product {
private int id;
private String name;
public Product(int id, String name) {
this.id = id;
this.name = name;
}
public static Product fromJSON(JSONObject jsonObject) {
return new Product(jsonObject.getInt("id"), jsonObject.getString("name");
}
}
This will make parsing the JSON coming from server much cleaner.
Aucun commentaire:
Enregistrer un commentaire