Example usage for com.google.gson JsonElement isJsonNull

List of usage examples for com.google.gson JsonElement isJsonNull

Introduction

In this page you can find the example usage for com.google.gson JsonElement isJsonNull.

Prototype

public boolean isJsonNull() 

Source Link

Document

provides check for verifying if this element represents a null value or not.

Usage

From source file:dao.WorkshopDAO.java

public ArrayList<Workshop> retrieveAllWorkshops(int staffId, String token)
        throws UnsupportedEncodingException, IOException {
    ArrayList<Workshop> allWorkshops = new ArrayList<Workshop>();
    String url = "http://119.81.43.85/erp/workshop/get_all_shop";

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    // add header
    post.setHeader("User-Agent", USER_AGENT);

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("staff_id", staffId + ""));
    urlParameters.add(new BasicNameValuePair("token", token));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpResponse response = client.execute(post);
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);//from   w ww.j  ava  2  s. co  m
    }
    String str = result.toString();
    JsonParser jsonParser = new JsonParser();
    JsonElement element = jsonParser.parse(str);
    JsonObject jobj = element.getAsJsonObject();
    JsonArray arr = (jobj.getAsJsonObject("payload")).getAsJsonArray("shops");

    for (int i = 0; i < arr.size(); i++) {
        JsonElement workshop = arr.get(i);
        JsonObject shop = workshop.getAsJsonObject();
        JsonElement attElement = shop.get("id");
        int id = 0;
        if (!attElement.isJsonNull()) {
            id = attElement.getAsInt();
        }
        attElement = shop.get("name");
        String name = "";
        if (!attElement.isJsonNull()) {
            name = attElement.getAsString();
        }
        attElement = shop.get("email");
        String email = "";
        if (!attElement.isJsonNull()) {
            email = attElement.getAsString();
        }
        attElement = shop.get("description");
        String description = "";
        if (!attElement.isJsonNull()) {
            description = attElement.getAsString();
        }
        attElement = shop.get("website");
        String website = "";
        if (!attElement.isJsonNull()) {
            website = attElement.getAsString();
        }
        attElement = shop.get("address");
        String address = "";
        if (!attElement.isJsonNull()) {
            address = attElement.getAsString();
        }
        attElement = shop.get("opening_hour_full");
        String openingHour = "";
        if (!attElement.isJsonNull()) {
            openingHour = attElement.getAsString();
        }
        attElement = shop.get("opening_hour_format");
        String openingHourFormat = "";
        if (!attElement.isJsonNull()) {
            openingHourFormat = attElement.getAsString();
        }
        attElement = shop.get("latitude");
        double latitude = 0.0;
        if (!attElement.isJsonNull()) {
            latitude = attElement.getAsDouble();
        }
        attElement = shop.get("longitude");
        double longitude = 0.0;
        if (!attElement.isJsonNull()) {
            longitude = attElement.getAsDouble();
        }
        attElement = shop.get("contact");
        String contact = "";
        if (!attElement.isJsonNull()) {
            contact = attElement.getAsString();
        }
        attElement = shop.get("contact2");
        String contact2 = "";
        if (!attElement.isJsonNull()) {
            contact2 = attElement.getAsString();
        }
        attElement = shop.get("location");
        String location = "";
        if (!attElement.isJsonNull()) {
            location = attElement.getAsString();
        }
        attElement = shop.get("specialize");
        String specialize = "";
        if (!attElement.isJsonNull()) {
            specialize = attElement.getAsString();
        }
        attElement = shop.get("category");
        String category = "";
        if (!attElement.isJsonNull()) {
            category = attElement.getAsString();
        }
        attElement = shop.get("brand_carried");
        String brandsCarried = "";
        if (!attElement.isJsonNull()) {
            brandsCarried = attElement.getAsString();
        }
        attElement = shop.get("remark");
        String remark = "";
        if (!attElement.isJsonNull()) {
            remark = attElement.getAsString();
        }
        attElement = shop.get("status");
        int status = 0;
        if (!attElement.isJsonNull()) {
            status = attElement.getAsInt();
        }
        //int status = attElement.getAsInt();
        Workshop ws = new Workshop(id, email, name, description, website, address, openingHour,
                openingHourFormat, latitude, longitude, contact, contact2, location, specialize, category,
                brandsCarried, remark, status);

        allWorkshops.add(ws);
    }
    return allWorkshops;
}

From source file:dao.WorkshopDAO.java

public ArrayList<String> updateWorkshop(int id, String email, String name, String description, String website,
        String address, String openingHour, String openingHourFormat, double latitude, double longitude,
        String contact, String contact2, String location, String specialize, String category,
        String brandsCarried, String remark, int status, int staffId, String token)
        throws UnsupportedEncodingException, IOException {

    String url = "http://119.81.43.85/erp/workshop/edit_shop";

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    // add header
    post.setHeader("User-Agent", USER_AGENT);

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("staff_id", staffId + ""));
    urlParameters.add(new BasicNameValuePair("token", token));
    urlParameters.add(new BasicNameValuePair("id", id + ""));
    urlParameters.add(new BasicNameValuePair("name", name));
    urlParameters.add(new BasicNameValuePair("description", description));
    urlParameters.add(new BasicNameValuePair("website", website));
    urlParameters.add(new BasicNameValuePair("address", address));
    urlParameters.add(new BasicNameValuePair("opening_hour_full", openingHour));
    urlParameters.add(new BasicNameValuePair("opening_hour_format", openingHourFormat));
    urlParameters.add(new BasicNameValuePair("latitude", latitude + ""));
    urlParameters.add(new BasicNameValuePair("longitude", longitude + ""));
    urlParameters.add(new BasicNameValuePair("contact", contact));
    urlParameters.add(new BasicNameValuePair("contact2", contact2));
    urlParameters.add(new BasicNameValuePair("location", location));
    urlParameters.add(new BasicNameValuePair("specialize", specialize));
    urlParameters.add(new BasicNameValuePair("category", category));
    urlParameters.add(new BasicNameValuePair("brand_carried", brandsCarried));
    urlParameters.add(new BasicNameValuePair("remark", remark));
    urlParameters.add(new BasicNameValuePair("email", email));
    urlParameters.add(new BasicNameValuePair("status", 1 + ""));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpResponse response = client.execute(post);
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);/*  w  w w  .j  a  v a  2  s  .  co  m*/
    }

    String str = result.toString();
    System.out.print(str);
    JsonParser jsonParser = new JsonParser();
    JsonElement element = jsonParser.parse(str);
    JsonObject jobj = element.getAsJsonObject();
    String errorMessage = null;
    ArrayList<String> errors = new ArrayList<String>();
    JsonElement isSuccess = jobj.get("is_success");
    if (isSuccess.getAsString().equals("false")) {
        errorMessage = jobj.get("error_message").getAsString();
        errors.add(errorMessage);
        JsonElement fields = jobj.get("payload");
        JsonArray arr;
        if (!fields.isJsonNull()) {
            arr = fields.getAsJsonObject().get("error_field").getAsJsonArray();

            for (int i = 0; i < arr.size(); i++) {
                String f = arr.get(i).getAsString();
                errors.add(f);
            }
        }
    }

    return errors;
}

From source file:dao.WorkshopDAO.java

public ArrayList<String> retrieveAllCarBrands(int staffId, String token)
        throws UnsupportedEncodingException, IOException {
    ArrayList<String> carBrands = new ArrayList<String>();
    String url = "http://119.81.43.85/erp/avalible_car/get_all_avalible_car";

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    // add header
    post.setHeader("User-Agent", USER_AGENT);

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("staff_id", staffId + ""));
    urlParameters.add(new BasicNameValuePair("token", token));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpResponse response = client.execute(post);
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);/*from   w  ww.  j  a  v  a2 s .c  o m*/
    }
    String str = result.toString();
    JsonParser jsonParser = new JsonParser();
    JsonElement element = jsonParser.parse(str);
    JsonObject jobj = element.getAsJsonObject();

    JsonElement brandsElement = jobj.get("payload");
    JsonObject brands = null;
    if (brandsElement.isJsonNull()) {
        return carBrands;
    } else {
        brands = brandsElement.getAsJsonObject();
        JsonArray brandsArr = brands.getAsJsonArray("available_cars");
        for (int i = 0; i < brandsArr.size(); i++) {
            JsonElement brandArr = brandsArr.get(i);
            JsonObject brandObj = brandArr.getAsJsonObject();
            JsonElement attElement = brandObj.get("car_brand");
            String brand = "";
            if (!attElement.isJsonNull()) {
                brand = attElement.getAsString();
                if (!carBrands.contains(brand)) {
                    carBrands.add(brand);
                }
            }

        }
    }
    return carBrands;
}

From source file:dao.WorkshopDAO.java

public String deleteWorkshop(int staffId, String token, int id)
        throws UnsupportedEncodingException, IOException {
    String url = "http://119.81.43.85/erp/workshop/delete_shop";

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    // add header
    post.setHeader("User-Agent", USER_AGENT);

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("staff_id", staffId + ""));
    urlParameters.add(new BasicNameValuePair("token", token));
    urlParameters.add(new BasicNameValuePair("id", id + ""));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpResponse response = client.execute(post);
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);// w  w w  .  j  a v  a 2  s  .c o  m
    }

    String str = result.toString();
    JsonParser jsonParser = new JsonParser();
    JsonElement element = jsonParser.parse(str);
    JsonObject jobj = element.getAsJsonObject();
    JsonElement errMsgEle = jobj.get("error_message");
    String errMsg = "";
    if (errMsgEle != null && !errMsgEle.isJsonNull()) {
        errMsg = errMsgEle.getAsString();
    }
    return errMsg;
}

From source file:de.azapps.mirakel.model.list.SpecialListsWhereDeserializer.java

License:Open Source License

@NonNull
private static Optional<SpecialListsBaseProperty> parseSpecialListWhere(final @NonNull JsonElement obj,
        final int deep) throws IllegalArgumentException {
    if (obj.isJsonObject()) {
        return parseSpecialListsCondition(obj);
    } else if (obj.isJsonArray()) {
        final List<SpecialListsBaseProperty> childs = new ArrayList<>(obj.getAsJsonArray().size());
        for (final JsonElement el : obj.getAsJsonArray()) {
            final Optional<SpecialListsBaseProperty> child = parseSpecialListWhere(el, deep + 1);
            if (child.isPresent()) {
                childs.add(child.get());
            }/*w w w  .  j  a v  a  2  s. co  m*/
        }
        return of((SpecialListsBaseProperty) new SpecialListsConjunctionList(
                ((deep % 2) == 0) ? SpecialListsConjunctionList.CONJUNCTION.AND
                        : SpecialListsConjunctionList.CONJUNCTION.OR,
                childs));
    } else if (obj.isJsonNull()) {
        return absent();
    } else {
        throw new IllegalArgumentException("Unknown json type");
    }
}

From source file:de.hkneissel.oomph.buildshipimport.impl.BuildshipImportTaskImpl.java

License:Open Source License

private Map<String, String> extractEntries(String name, JsonElement jsonElement) {
    Map<String, String> result = new HashMap<String, String>();

    if (jsonElement.isJsonNull()) {
        // ignore
    } else if (jsonElement.isJsonObject()) {
        result.putAll(extractEntries(jsonElement.getAsJsonObject()));
    } else if (jsonElement.isJsonArray()) {

    } else if (name != null) {
        result.put(name, jsonElement.getAsString());
    }/*  w  w  w.  ja va  2 s.  co m*/

    return result;
}

From source file:de.innovationgate.wgpublisher.webtml.utils.JsonUtils.java

License:Open Source License

public Object jsonToJava(JsonElement jsonValue) {
    Object value = null;//from ww w . j a va2s  . c o m
    if (jsonValue.isJsonNull()) {
        value = null;
    } else if (jsonValue.isJsonPrimitive()) {
        JsonPrimitive prim = (JsonPrimitive) jsonValue;
        if (prim.isNumber()) {
            value = prim.getAsDouble();
        } else if (prim.isBoolean()) {
            value = prim.getAsBoolean();
        } else {
            value = prim.getAsString();
        }
        value = jsonToJavaConversions(value);
    } else if (jsonValue.isJsonArray()) {
        JsonArray array = jsonValue.getAsJsonArray();
        List<Object> list = new ArrayList<Object>();
        for (JsonElement element : array) {
            list.add(jsonToJava(element));
        }
    } else if (jsonValue.isJsonObject()) {
        JsonObject obj = jsonValue.getAsJsonObject();
        Map<String, Object> map = new HashMap<String, Object>();
        for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
            map.put(String.valueOf(entry.getKey()), jsonToJava(entry.getValue()));
        }
    }

    return value;
}

From source file:de.sanandrew.mods.sanlib.lib.util.JsonUtils.java

License:Creative Commons License

public static float getFloatVal(JsonElement json) {
    if (json == null || json.isJsonNull()) {
        throw new JsonSyntaxException("Json cannot be null");
    }//from ww  w. ja v  a 2  s  . c o  m

    if (!json.isJsonPrimitive()) {
        throw new JsonSyntaxException("Expected value to be a primitive");
    }

    return json.getAsFloat();
}

From source file:de.sanandrew.mods.sanlib.lib.util.JsonUtils.java

License:Creative Commons License

public static String getStringVal(JsonElement json) {
    if (json == null || json.isJsonNull()) {
        return null;
    }/* ww  w.  ja v  a2 s .  com*/

    if (!json.isJsonPrimitive()) {
        throw new JsonSyntaxException("Expected value to be a primitive");
    }

    return json.getAsString();
}

From source file:de.sanandrew.mods.sanlib.lib.util.JsonUtils.java

License:Creative Commons License

public static int getIntVal(JsonElement json) {
    if (json == null || json.isJsonNull()) {
        throw new JsonSyntaxException("Json cannot be null");
    }//from w ww  .  j av  a  2  s.  c o m

    if (!json.isJsonPrimitive()) {
        throw new JsonSyntaxException("Expected value to be a primitive");
    }

    return json.getAsInt();
}