Example #1
0
  // получит все продукты в виде спсика
  public static List<ProductREST> getAll() throws IOException, JsonValidationException {
    List<ProductREST> products = new LinkedList<>();
    String data = readAll();
    for (JsonElement e : getElements(data)) {
      JsonObject obj = e.getAsJsonObject();
      Iterator<Map.Entry<String, JsonElement>> elements = obj.entrySet().iterator();
      int i = 0;
      ProductREST current = new ProductREST();
      while (elements.hasNext()) {
        switch (i++) {
          case 0:
            current.setName(elements.next().getValue().getAsString());
            break;
          case 1:
            current.setPrice(elements.next().getValue().getAsDouble());
            break;
          case 2:
            current.setWeight(elements.next().getValue().getAsDouble());
            break;
          case 3:
            current.setManufacturer(
                ProductREST.getManufacturer(elements.next().getValue().getAsString()));
            break;
          case 4:
            current.setCategory(elements.next().getValue().getAsString());
            break;
          case 5:
            current.setId(elements.next().getValue().getAsInt());
            break;
          default:
            throw new JsonValidationException("cannot validate json database, check it!");
        }
      }
      products.add(current);
    }

    return products;
  }