Example usage for com.google.gson JsonObject has

List of usage examples for com.google.gson JsonObject has

Introduction

In this page you can find the example usage for com.google.gson JsonObject has.

Prototype

public boolean has(String memberName) 

Source Link

Document

Convenience method to check if a member with the specified name is present in this object.

Usage

From source file:com.apifest.oauth20.persistence.mongodb.MongoDBManager.java

License:Apache License

protected String constructDbId(Object object) {
    Gson gson = new Gson();
    String json = gson.toJson(object);
    JsonParser parser = new JsonParser();
    JsonObject jsonObj = parser.parse(json).getAsJsonObject();
    if (jsonObj.has("id")) {
        String id = jsonObj.get("id").getAsString();
        jsonObj.remove("id");
        jsonObj.addProperty(CLIENTS_ID, id);
    }/*from w  w  w.j a va  2  s  .c om*/
    return jsonObj.toString();
}

From source file:com.apothesource.pillfill.service.drug.impl.DefaultDrugAlertServiceImpl.java

License:Open Source License

private List<DrugAlertType> deserializeDrugAlerts(Collection<PrescriptionType> rxs, String msg) {
    try {/*from  w w w .  j  av a 2  s.  c om*/
        JsonObject returnValue = new JsonParser().parse(msg).getAsJsonObject();
        if (!returnValue.has("fullInteractionTypeGroup")) {
            return Collections.emptyList();
        } else {
            Gson gson = new Gson();
            TypeToken<List<FullInteractionTypeGroup>> interactionGroupListType = new TypeToken<List<FullInteractionTypeGroup>>() {
            };
            List<FullInteractionTypeGroup> group = gson.fromJson(returnValue.get("fullInteractionTypeGroup"),
                    interactionGroupListType.getType());
            ArrayList<DrugAlertType> alerts = processDrugInteractions(rxs, group);
            return alerts;

        }
    } catch (Exception e) {
        log.log(Level.SEVERE, "Invalid drug interaction alert response.", e);
        throw new RuntimeException(e);
    }
}

From source file:com.apothesource.pillfill.service.drug.impl.DefaultDrugServiceImpl.java

License:Open Source License

/**
 * <a href="http://rxnav.nlm.nih.gov/RxNormAPIs.html#">NIH Service</a>: Get avaliable RxNorm IDs associated with an 11 digit National Drug Code (NDC).
 *
 * @param ndc The 11-digit NDC without dashes or spaces
 * @return A list of RxNorm IDs associated with this NDC (should normally return 0 or 1)
 *///from w  ww.ja v a2  s . com
@Override
public Observable<String> getRxNormIdForDrugNdc(String ndc) {
    return subscribeIoObserveImmediate(subscriber -> {
        String urlStr = String.format(URL_RXNORM_BRAND_NAME_BY_NDC, ndc);
        try {
            String responseStr = PFNetworkManager.doPinnedGetForUrl(urlStr);
            JsonParser parser = new JsonParser();
            JsonObject response = parser.parse(responseStr).getAsJsonObject().get("idGroup").getAsJsonObject();
            if (response.has("rxnormId")) {
                JsonArray array = response.getAsJsonArray("rxnormId");
                for (JsonElement e : array) {
                    subscriber.onNext(e.getAsString());
                }
                subscriber.onCompleted();
            } else {
                Timber.e("No rxnormIds found for NDC: %s", ndc);
                subscriber.onError(new RuntimeException("Could not find NDC->RxNorm for NDC."));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    });

}

From source file:com.ARA.DAO.CarDAO.java

/** This method is used to update a specific car with :id.
 * @param request//from www  .j av  a 2  s.  c o  m
 * @param response
 * @return The car specified with :id.
 * @throws IOException
 */
public String updateCar(Request request, Response response) throws IOException {
    try {
        String id = request.params(":id");
        JsonObject jsonObject = (JsonObject) new JsonParser().parse(request.body());

        Car car = getDs().find(Car.class).field("id").equal(id).get();

        if (jsonObject.has("make")) {
            String make = jsonObject.get("make").toString().replaceAll("\"", "");

            car.setMake(make);
        }

        if (jsonObject.has("model")) {
            String model = jsonObject.get("model").toString().replaceAll("\"", "");

            car.setModel(model);
        }

        if (jsonObject.has("license")) {
            String license = jsonObject.get("license").toString().replaceAll("\"", "");

            car.setLicense(license);
        }

        if (jsonObject.has("carType")) {
            String carType = jsonObject.get("carType").toString().replaceAll("\"", "");

            car.setCarType(carType);
        }

        if (jsonObject.has("maxPassengers")) {
            Integer maxPassengers = Integer.valueOf(jsonObject.get("maxPassengers").toString());

            car.setMaxPassengers(maxPassengers);
        }

        if (jsonObject.has("color")) {
            String color = jsonObject.get("color").toString().replaceAll("\"", "");

            car.setColor(color);
        }

        if (jsonObject.has("validRideTypes")) {
            Type listType = new TypeToken<ArrayList<String>>() {
            }.getType();
            List<String> validRideTypes = new Gson().fromJson(jsonObject.get("validRideTypes").getAsJsonArray(),
                    listType);

            car.setValidRideTypes(validRideTypes);
        }
        if (!car.isValidCar()) {
            response.status(400);
            return dataToJson.dataToJsonFormat(new Error(400, 2000, "Invalid data type"));
        }

        getDs().save(car);
        response.status(200);
        return dataToJson.dataToJsonFormat(car);
    } catch (Exception e) {
        response.status(500);
        return dataToJson.dataToJsonFormat(new Error(500, 5000, e.getMessage()));
    }
}

From source file:com.ARA.DAO.DriverDAO.java

/** This method is used to update a specific driver with :id.
 * @param request//from  ww  w  .  j  a  v  a 2 s  .co  m
 * @param response
 * @return The driver specified with :id.
 * @throws IOException
 */
public String updateDriver(Request request, Response response) throws IOException {
    try {
        String id = request.params(":id");
        JsonObject jsonObject = (JsonObject) new JsonParser().parse(request.body());

        Driver driver = getDs().find(Driver.class).field("id").equal(id).get();

        if (jsonObject.has("firstName")) {
            String firstName = jsonObject.get("firstName").toString().replaceAll("\"", "");
            driver.setFirstName(firstName);
        }

        if (jsonObject.has("lastName")) {
            String lastName = jsonObject.get("lastName").toString().replaceAll("\"", "");
            driver.setLastName(lastName);
        }

        if (jsonObject.has("emailAddress")) {
            String emailAddress = jsonObject.get("emailAddress").toString().replaceAll("\"", "");
            if (getDs().find(Driver.class).field("emailAddress").equal(emailAddress).get() != null
                    || getDs().find(Passenger.class).field("emailAddress").equal(emailAddress).get() != null) {
                response.status(400);
                return dataToJson.dataToJsonFormat(new Error(400, 3000, "email address already taken"));
            }
            driver.setEmailAddress(emailAddress);
        }

        if (jsonObject.has("password")) {
            String password = jsonObject.get("password").toString().replaceAll("\"", "");
            if (password.length() > 20 || password.length() < 8) {
                response.status(400);
                return dataToJson.dataToJsonFormat(new Error(400, 2000, "Invalid data type"));
            }
            driver.setPassword(password);
        }

        if (jsonObject.has("addressLine1")) {
            String addressLine1 = jsonObject.get("addressLine1").toString().replaceAll("\"", "");
            driver.setAddressLine1(addressLine1);
        }

        if (jsonObject.has("addressLine2")) {
            String addressLine2 = jsonObject.get("addressLine2").toString().replaceAll("\"", "");
            driver.setAddressLine2(addressLine2);
        }

        if (jsonObject.has("city")) {
            String city = jsonObject.get("city").toString().replaceAll("\"", "");
            driver.setCity(city);
        }

        if (jsonObject.has("state")) {
            String state = jsonObject.get("state").toString().replaceAll("\"", "");
            driver.setState(state);
        }

        if (jsonObject.has("zip")) {
            String zip = jsonObject.get("zip").toString().replaceAll("\"", "");
            driver.setZip(zip);
        }

        if (jsonObject.has("phoneNumber")) {
            String phoneNumber = jsonObject.get("phoneNumber").toString().replaceAll("\"", "");
            driver.setPhoneNumber(phoneNumber);
        }

        if (jsonObject.has("drivingLicense")) {
            String drivingLicense = jsonObject.get("drivingLicense").toString().replaceAll("\"", "");
            driver.setDrivingLicense(drivingLicense);
        }

        if (jsonObject.has("licensedState")) {
            String licensedState = jsonObject.get("licensedState").toString().replaceAll("\"", "");
            driver.setLicensedState(licensedState);
        }

        if (!driver.isValidDriver()) {
            response.status(400);
            return dataToJson.dataToJsonFormat(new Error(400, 2000, "Invalid data type"));
        }

        getDs().save(driver);
        response.status(200);
        return dataToJson.dataToJsonFormat(driver);
    } catch (Exception e) {
        response.status(500);
        return dataToJson.dataToJsonFormat(new Error(500, 5000, e.getMessage()));
    }

}

From source file:com.arangodb.entity.EntityDeserializers.java

License:Apache License

private static <T extends BaseEntity> T deserializeBaseParameter(JsonObject obj, T entity) {
    if (obj.has("error") && obj.getAsJsonPrimitive("error").isBoolean()) {
        entity.error = obj.getAsJsonPrimitive("error").getAsBoolean();
    }/* w  w w  .ja v a2  s.  c o  m*/
    if (obj.has("code") && obj.getAsJsonPrimitive("code").isNumber()) {
        entity.code = obj.getAsJsonPrimitive("code").getAsInt();
    }
    if (obj.has("errorNum") && obj.getAsJsonPrimitive("errorNum").isNumber()) {
        entity.errorNumber = obj.getAsJsonPrimitive("errorNum").getAsInt();
    }
    if (obj.has("errorMessage")) {
        entity.errorMessage = obj.getAsJsonPrimitive("errorMessage").getAsString();
    }
    if (obj.has("etag") && obj.getAsJsonPrimitive("errorNum").isNumber()) {
        entity.etag = obj.getAsJsonPrimitive("etag").getAsLong();
    }

    return entity;
}

From source file:com.arangodb.entity.EntityDeserializers.java

License:Apache License

private static <T extends DocumentHolder> T deserializeDocumentParameter(JsonObject obj, T entity) {

    if (obj.has("_rev")) {
        entity.setDocumentRevision(obj.getAsJsonPrimitive("_rev").getAsLong());
    }//from  w w  w.  j  av a  2s .  co m
    if (obj.has("_id")) {
        entity.setDocumentHandle(obj.getAsJsonPrimitive("_id").getAsString());
    }
    if (obj.has("_key")) {
        entity.setDocumentKey(obj.getAsJsonPrimitive("_key").getAsString());
    }
    if (true) {

    }

    return entity;
}

From source file:com.arangodb.entity.EntityDeserializers.java

License:Apache License

private static JsonObject getFirstResultAsJsonObject(JsonObject obj) {
    if (obj.has("result")) {
        if (obj.get("result").isJsonArray()) {
            JsonArray result = obj.getAsJsonArray("result");

            if (result.size() > 0) {
                JsonElement jsonElement = result.get(0);
                if (jsonElement.isJsonObject()) {
                    return jsonElement.getAsJsonObject();
                }//w  w  w.j  a v a  2 s. c  om
            }
        } else if (obj.get("result").isJsonObject()) {
            return obj.getAsJsonObject("result");
        }

    }
    return null;
}

From source file:com.arangodb.entity.EntityDeserializers.java

License:Apache License

private static List<PathEntity<Object, Object>> getPaths(JsonDeserializationContext context, JsonObject visited,
        Class<?> vertexClazz, Class<?> edgeClazz) {
    List<PathEntity<Object, Object>> pathEntities = new ArrayList<PathEntity<Object, Object>>();
    JsonArray paths = visited.getAsJsonArray("paths");
    if (!paths.equals(null)) {
        for (int i = 0, imax = paths.size(); i < imax; i++) {
            JsonObject path = paths.get(i).getAsJsonObject();
            PathEntity<Object, Object> pathEntity = new PathEntity<Object, Object>();

            if (path.has("edges")) {
                pathEntity.setEdges(getEdges(edgeClazz, context, path.getAsJsonArray("edges")));
            }//from   w  w w. jav a  2s.  co m
            if (path.has("vertices")) {
                pathEntity.setVertices(getVertices(vertexClazz, context, path.getAsJsonArray("vertices")));
            }

            pathEntities.add(pathEntity);
        }
    }
    return pathEntities;
}

From source file:com.atlauncher.data.loaders.forge.DownloadsTypeAdapter.java

License:Open Source License

@Override
public Downloads deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    DownloadsItem artifact = null;/*from  w ww. jav a2 s  .com*/

    final JsonObject rootJsonObject = json.getAsJsonObject();

    if (rootJsonObject.has("artifact")) {
        final JsonObject artifactObject = rootJsonObject.getAsJsonObject("artifact");
        artifact = Gsons.DEFAULT_ALT.fromJson(artifactObject, DownloadsItem.class);
    }

    return new Downloads(artifact);
}