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.adobe.acs.commons.workflow.bulk.execution.impl.servlets.InitFormServlet.java

License:Apache License

private JsonObject accumulate(JsonObject obj, String key, JsonElement value) {
    if (obj.has(key)) {
        JsonElement existingValue = obj.get(key);
        if (existingValue instanceof JsonArray) {
            ((JsonArray) existingValue).add(value);
        } else {//from w  ww.  j a  v  a2s .co m
            JsonArray array = new JsonArray();
            array.add(existingValue);
            obj.add(key, array);
        }
    } else {
        JsonArray array = new JsonArray();
        array.add(value);
        obj.add(key, array);
    }
    return obj;
}

From source file:com.adssets.servlet.MarketServlet.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*www  .  j a v a  2s .  com*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setHeader("Access-Control-Allow-Origin", "*");
    ;
    try (PrintWriter out = response.getWriter()) {
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            BufferedReader reader = request.getReader();
            while ((line = reader.readLine()) != null)
                sb.append(line);
        } catch (Exception e) {
            System.out.println(
                    "com.adssets.servlet.MarketServlet.processRequestPost()" + " Could not read POST body");
        }

        JsonObject jsonObject = (new JsonParser()).parse(sb.toString()).getAsJsonObject();

        if (jsonObject.has("name") && jsonObject.has("description")) {
            String res = data.createMarket(jsonObject.toString());
            out.println(res);
        } else {
            System.out.println(
                    "com.adssets.servlet.MarketServlet.processRequestPost()" + " Missing fields in body");

        }

    }

}

From source file:com.adssets.servlet.ObjectServlet.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request// ww  w . ja v  a 2 s  .  c  o m
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setHeader("Access-Control-Allow-Origin", "*");
    try (PrintWriter out = response.getWriter()) {
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            BufferedReader reader = request.getReader();
            while ((line = reader.readLine()) != null)
                sb.append(line);
        } catch (Exception e) {
            System.out.println(
                    "com.adssets.servlet.ObjectServlet.processRequestPost()" + " Could not read POST body");
        }

        JsonObject jsonObject = (new JsonParser()).parse(sb.toString()).getAsJsonObject();

        if (jsonObject.has("marketId") && jsonObject.has("json")) {
            System.out.println(jsonObject.toString());
            String res = data.createFeed(jsonObject.toString());
            out.println(res);
        } else {
            System.out.println(
                    "com.adssets.servlet.ObjectServlet.processRequestPost()" + " Missing fields in body");

        }

    }
}

From source file:com.agwego.common.GsonHelper.java

License:Open Source License

/**
 * Short hand, encapsulate the check if the object has the key and return the appropriate String otherwise null
 *
 * @param jObject the JsonObject in question
 * @param key the key to lookup//  w  ww. ja  v a2s.  com
 * @param otherwise value to return if no key is found
 * @return the value for key as a String
 */
public static String getAsString(final JsonObject jObject, final String key, final String otherwise) {
    return jObject.has(key) ? jObject.get(key).getAsString() : otherwise;
}

From source file:com.agwego.common.GsonHelper.java

License:Open Source License

/**
 * Short hand, encapsulate the check if the object has the key and return the appropriate Boolean, otherwise the
 * default value/*from w  w  w .j  av a2s.  c  om*/
 *
 * @param jObject the JsonObject in question
 * @param key the key to lookup
 * @param otherwise - if the key doesn't exist
 * @return the value for key as a Boolean
 */
public static Boolean getAsBoolean(final JsonObject jObject, final String key, final Boolean otherwise) {
    return jObject.has(key) ? jObject.get(key).getAsBoolean() : otherwise;
}

From source file:com.agwego.common.GsonHelper.java

License:Open Source License

/**
 * Short hand, check if the object is not null and the key is presence return the result as a JsonArray
 *
 * @param jObject the JsonObject in question
 * @param key the key to lookup//from  ww w.j a  v a  2 s.c om
 * @return the JsonArray for this key or a new empty JsonArray
 */
public static JsonArray getAsArray(final JsonObject jObject, final String key) {
    if (jObject != null && jObject.has(key)) {
        try {
            return jObject.getAsJsonArray(key);
        } catch (ClassCastException ex) {
            // fall through
        }
    }

    return new JsonArray();
}

From source file:com.ai2020lab.aiutils.common.JsonUtils.java

License:Apache License

/**
 * ?JSONObject??Stringvalue/*from  www  .j  av a  2s  . com*/
 *
 * @param key ??
 * @return ??value
 */
public String getObjStringValue(String key, JsonObject obj) {
    if (key == null || key.equals("")) {
        // Key
        throw new IllegalArgumentException("?key?");
    }
    if (obj == null || !obj.isJsonObject()) {
        // JsonObject??JsonObject
        throw new IllegalArgumentException("?obj?JsonObject?");
    }
    if (!obj.has(key)) {
        // Key
        LogUtils.e(TAG, "objkey'" + key + "' ");
        return null;
    }
    return obj.get(key).getAsString();
}

From source file:com.ai2020lab.aiutils.common.JsonUtils.java

License:Apache License

/**
 * ?JSONObject??Integervalue//  ww  w.  ja v a2 s. co m
 *
 * @param key ??
 * @return ??value
 */
public Integer getObjIntValue(String key, JsonObject obj) {
    if (key == null || key.equals("")) {
        // Key
        throw new IllegalArgumentException("?key?");
    }
    if (obj == null || !obj.isJsonObject()) {
        // JsonObject??JsonObject
        throw new IllegalArgumentException("?obj?JsonObject?");
    }
    if (!obj.has(key)) {
        // Key
        LogUtils.e(TAG, "objkey'" + key + "' ");
        return null;
    }
    return obj.get(key).getAsInt();
}

From source file:com.alfresco.client.api.core.model.deserializer.FavoriteEntryDeserializer.java

License:Apache License

@Override
public FavoriteRepresentation deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
        throws JsonParseException {
    JsonElement entry = je;/*ww w  . j  av  a2s  . c  om*/
    if (je.getAsJsonObject().has(PublicAPIConstant.ENTRY_VALUE)) {
        entry = je.getAsJsonObject().get(PublicAPIConstant.ENTRY_VALUE);
    }

    FavoriteRepresentation favorite = null;
    JsonObject target = entry.getAsJsonObject().get("target").getAsJsonObject();
    if (target.has("file")) {
        favorite = jdc.deserialize(entry, FavoriteNodeRepresentation.class);
        NodeRepresentation file = jdc.deserialize(target.get("file").getAsJsonObject(),
                NodeRepresentation.class);
        favorite.setTarget(file);
    } else if (target.has("folder")) {
        favorite = jdc.deserialize(entry, FavoriteNodeRepresentation.class);
        NodeRepresentation file = jdc.deserialize(target.get("folder").getAsJsonObject(),
                NodeRepresentation.class);
        favorite.setTarget(file);
    } else if (target.has("site")) {
        favorite = jdc.deserialize(entry, FavoriteSiteRepresentation.class);
        SiteRepresentation site = jdc.deserialize(target.get("site").getAsJsonObject(),
                SiteRepresentation.class);
        favorite.setTarget(site);
    }

    return favorite;
}

From source file:com.apifest.oauth20.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(ID_NAME, id);
    }// w w  w.ja v a2s.  c o  m
    return jsonObj.toString();
}