0

I have the following post body request -

[ { "filters": { "StockQuantity": { "value": [0], "cretiria": 0, "type": 1 }, "Price": { "value": [0], "cretiria": 0, "type": 1 } } } ] 

The thing is that what you see now as StockQuantity and Price can change to alot of parameters with different names and I can't just create a class with these names.

That being sayed, the 3 parameters value, cretiria and type do not change for all types.

I want to create a generic body that has a "filters" type that I can easily add as many filters with different names as needed, all having 3 parameters - value (generic type list), cretiria (int) and type (int)

Here is currently what I have...which is bad, not generic and very hard coded -

private void getProducts() { //stock variables List<Integer> stockValue = new ArrayList<>(); stockValue.add(0); int stockCriteria = 0; int stockType = 1; //price variables List<Integer> priceValue = new ArrayList<>(); priceValue.add(0); int priceCriteria = 0; int priceType = 1; //product name List<String> productValue = new ArrayList<>(); int productCriteria = 0; int productType = 5; productValue.add("floral"); //setting the values of the json send as a body to the server Filters filters = new Filters(); filters.setPrice(new Price(priceCriteria, priceType, priceValue)); filters.setStockQuantity(new StockQuantity(stockCriteria, stockType, stockValue)); //removed product name for test //filters.setProductName(new ProductName(productCriteria, productType, productValue)); ProductAPIBodyRequest productAPIBodyRequest = new ProductAPIBodyRequest(); productAPIBodyRequest.setFilters(filters); ProductAPIBodyRequest[] productAPIBodyRequests = {productAPIBodyRequest}; Call<JsonObject> products = marketApiCalls.getProducts(take, page, productAPIBodyRequests); products.enqueue(new Callback<JsonObject>() { @Override public void onResponse(Call<JsonObject> call, Response<JsonObject> response) { if (!response.isSuccessful()) { return; } JsonObject body = response.body(); FullProductModel fullProductModel = gson.fromJson(body, FullProductModel.class); for (int i = 0; i < fullProductModel.getData().size(); i++) { com.twoverte.models.product.full_product.ResultData resultData = fullProductModel.getData().get(i).getResultData(); MiniProductModel model = new MiniProductModel( resultData.getId(), resultData.getImageUrl(), resultData.getProductName(), resultData.getShortDescription(), resultData.getFullDescription(), resultData.getVendorName(), resultData.getVendorUrl(), resultData.getUrl(), resultData.getPictureList(), resultData.getSku(), resultData.getVendorImageUrl(), resultData.getStockQuantity(), resultData.getFavoritesCount(), resultData.getPrice() ); miniProductModelList.add(model); } productsAdapter.notifyDataSetChanged(); Log.d("allProducts", miniProductModelList.toString()); } @Override public void onFailure(Call<JsonObject> call, Throwable t) { t.getMessage(); } }); } 
1
  • You might be interested in Jackson's JsonAnyGetter for "additional unspecified properties". Commented Nov 18, 2019 at 9:36

1 Answer 1

1

Would you consider converting filters to an Array of objects, and moving the 'StockQuantity', 'Price' or other keys into the body of the object along with the value, type and criteria fields? So something like

{ "filters": [ { "filter": "StockQuantity", "value": [0], "type": 1, "criteria": 1 }, { "filter": "Price", "value": [0], "type": 1, "criteria": 1 } ] } 

From here, your Pojo would be pretty generic

public class Filters { private List<Filter> filters; public List<Filter> getFilters() { return filters; } public void setFilters(List<Filter> filters) { this.filters = filters; } private class Filter { private String filter; private List<Integer> value; private int type; private int criteria; public String getFilter() { return filter; } public void setFilter(String filter) { this.filter = filter; } public List<Integer> getValue() { return value; } public void setValue(List<Integer> value) { this.value = value; } public int getType() { return type; } public void setType(int type) { this.type = type; } public int getCriteria() { return criteria; } public void setCriteria(int criteria) { this.criteria = criteria; } } } 

Doing it this way means you can have whatever filters you want by changing the filter value to whatever string meets your needs.

Sign up to request clarification or add additional context in comments.

1 Comment

When trying to work with your solution at Postman first, I took your json result and tried to use it as a body. The error I got was Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'Verte.NetCore.Web.CRUD.Core.Models.FilterViewModel[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.