Example usage for com.google.gson JsonElement getAsJsonObject

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

Introduction

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

Prototype

public JsonObject getAsJsonObject() 

Source Link

Document

convenience method to get this element as a JsonObject .

Usage

From source file:com.cloudant.client.internal.util.IndexDeserializer.java

License:Open Source License

public List<Index> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    final List<Index> indices = new ArrayList<Index>();

    final JsonObject jsonObject = json.getAsJsonObject();
    JsonArray indArray = jsonObject.get("indexes").getAsJsonArray();
    for (int i = 0; i < indArray.size(); i++) {
        JsonObject ind = indArray.get(i).getAsJsonObject();
        String ddoc = null;/*w  ww. java  2s.  c om*/
        if (!ind.get("ddoc").isJsonNull()) { // ddoc is optional
            ddoc = ind.get("ddoc").getAsString();
        }
        Index idx = new Index(ddoc, ind.get("name").getAsString(), ind.get("type").getAsString());
        JsonArray fldArray = ind.get("def").getAsJsonObject().get("fields").getAsJsonArray();
        for (int j = 0; j < fldArray.size(); j++) {
            Set<Map.Entry<String, JsonElement>> fld = fldArray.get(j).getAsJsonObject().entrySet();
            for (Map.Entry<String, JsonElement> entry : fld) {
                idx.addIndexField(entry.getKey(), IndexField.SortOrder.valueOf(entry.getValue().getAsString()));
            }
        } //end fldArray
        indices.add(idx);

    } // end indexes

    return indices;
}

From source file:com.cloudant.client.internal.util.SecurityDeserializer.java

License:Open Source License

public Map<String, EnumSet<Permissions>> deserialize(JsonElement json, Type typeOfT,
        JsonDeserializationContext context) throws JsonParseException {

    Map<String, EnumSet<Permissions>> perms = new HashMap<String, EnumSet<Permissions>>();
    JsonElement elem = json.getAsJsonObject().get("cloudant");
    if (elem == null) {
        return perms;
    }//from   w  w w  .  ja  v a  2  s .com
    Set<Map.Entry<String, JsonElement>> permList = elem.getAsJsonObject().entrySet();
    for (Map.Entry<String, JsonElement> entry : permList) {
        String user = entry.getKey();
        EnumSet<Permissions> p = context.deserialize(entry.getValue(), DeserializationTypes.PERMISSIONS);
        perms.put(user, p);
    }
    return perms;

}

From source file:com.cloudant.client.internal.util.ShardDeserializer.java

License:Open Source License

public List<Shard> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    final List<Shard> shards = new ArrayList<Shard>();

    final JsonObject jsonObject = json.getAsJsonObject();
    Set<Map.Entry<String, JsonElement>> shardsObj = jsonObject.get("shards").getAsJsonObject().entrySet();

    for (Map.Entry<String, JsonElement> entry : shardsObj) {
        String range = entry.getKey();
        List<String> nodeNames = context.deserialize(entry.getValue(), DeserializationTypes.STRINGS);
        shards.add(new Shard(range, nodeNames));
    }/*from ww w  . j av a  2s . co m*/

    return shards;
}

From source file:com.cloudant.client.internal.views.RowImpl.java

License:Open Source License

RowImpl(ViewQueryParameters<K, V> parameters, JsonElement row) {
    this.parameters = parameters;
    this.gson = parameters.getClient().getGson();
    if (row.isJsonObject()) {
        this.row = row.getAsJsonObject();
    } else {/* www  . java 2s  .c  o  m*/
        this.row = new JsonObject();
    }
}

From source file:com.cloudant.client.org.lightcouch.internal.CouchDbUtil.java

License:Open Source License

public static <T> T JsonToObject(Gson gson, JsonElement elem, String key, Class<T> classType) {
    if (elem != null && !elem.isJsonNull()) {
        JsonElement keyElem = elem.getAsJsonObject().get(key);
        if (keyElem != null && !keyElem.isJsonNull()) {
            return gson.fromJson(elem.getAsJsonObject().get(key), classType);
        } else {/*  w w w.  j  a v a  2 s.  c o m*/
            return null;
        }
    } else {
        return null;
    }
}

From source file:com.cloudant.client.org.lightcouch.internal.GsonHelper.java

License:Open Source License

/**
 * Builds {@link Gson} and registers any required serializer/deserializer.
 *
 * @return {@link Gson} instance/* w  w w .  j av a2s.  c  om*/
 */
public static GsonBuilder initGson(GsonBuilder gsonBuilder) {
    gsonBuilder.registerTypeAdapter(JsonObject.class, new JsonDeserializer<JsonObject>() {
        public JsonObject deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return json.getAsJsonObject();
        }
    });
    gsonBuilder.registerTypeAdapter(JsonObject.class, new JsonSerializer<JsonObject>() {
        public JsonElement serialize(JsonObject src, Type typeOfSrc, JsonSerializationContext context) {
            return src.getAsJsonObject();
        }

    });

    return gsonBuilder;
}

From source file:com.cloudant.client.org.lightcouch.Replicator.java

License:Open Source License

/**
 * Finds all documents in the replicator database.
 */// www  . j a  v a2  s.  co m
public List<ReplicatorDocument> findAll() {
    InputStream instream = null;
    try {
        final URI uri = buildUri(dbURI).path("_all_docs").query("include_docs", "true").build();
        final Reader reader = new InputStreamReader(instream = client.get(uri), "UTF-8");
        final JsonArray jsonArray = new JsonParser().parse(reader).getAsJsonObject().getAsJsonArray("rows");
        final List<ReplicatorDocument> list = new ArrayList<ReplicatorDocument>();
        for (JsonElement jsonElem : jsonArray) {
            JsonElement elem = jsonElem.getAsJsonObject().get("doc");
            if (!getAsString(elem.getAsJsonObject(), "_id").startsWith("_design")) { // skip
                // design docs
                ReplicatorDocument rd = client.getGson().fromJson(elem, ReplicatorDocument.class);
                list.add(rd);
            }
        }
        return list;
    } catch (UnsupportedEncodingException e) {
        // This should never happen as every implementation of the java platform is required
        // to support UTF-8.
        throw new RuntimeException(e);
    } finally {
        close(instream);
    }
}

From source file:com.cloudbase.CBNaturalDeserializer.java

License:Open Source License

public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
    if (json.isJsonNull())
        return null;
    else if (json.isJsonPrimitive())
        return handlePrimitive(json.getAsJsonPrimitive());
    else if (json.isJsonArray())
        return handleArray(json.getAsJsonArray(), context);
    else//from   ww  w.  j  ava2s  .co m
        return handleObject(json.getAsJsonObject(), context);
}

From source file:com.clover.cfp.examples.objects.PayloadMessage.java

License:Apache License

public static PayloadMessage fromJsonString(String m) {
    JsonElement je = PARSER.parse(m);
    JsonObject jo = je.getAsJsonObject();
    String payloadClassName = jo.get("payloadClassName").getAsString();
    Class<? extends PayloadMessage> cls = null;
    try {//from w w  w .j  ava2s .  com
        cls = (Class<? extends PayloadMessage>) Class
                .forName("com.clover.cfp.examples.objects." + payloadClassName);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return GSON.fromJson(jo, cls);
}

From source file:com.cn.bean.OrderInfo.java

public void setProductLineStructJson(String productLineStructJson) {
    //        System.out.println(productLineStructJson);
    JsonParser parser = new JsonParser();
    JsonElement element = parser.parse(productLineStructJson);
    JsonObject object = element.getAsJsonObject();
    ProductLineStruct struct = new ProductLineStruct();
    struct.setFieldName(object.get("fieldName").getAsString());
    struct.setFieldValue(object.get("fieldValue").getAsString());
    struct.setChildRowNum(object.get("childRowNum").getAsInt());
    struct.setViewType(object.get("viewType").getAsInt());
    struct.setFieldType(object.get("fieldType").getAsInt());
    struct.setTextHorizon(object.get("textHorizon").getAsBoolean());
    struct.setWidth(object.get("width").getAsInt());

    for (int i = 0; i < struct.getChildRowNum(); i++) {
        String fieldName = object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject()
                .get("fieldName").getAsString();
        //System.out.println("fieldName:" + fieldName);
        switch (fieldName) {
        case "ProductZlValue": {
            object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue",
                    (null == productCommand) ? ("") : (productCommand));
            break;
        }//from w  w w  .  jav  a  2 s.  com
        case "ProductNameValue": {
            object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue",
                    ((null == productName) ? ("") : (productName)) + "/"
                            + ((null == graphicCode) ? ("") : (graphicCode)));
            break;
        }
        case "ProductCodeValue": {
            object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue",
                    (null == productCode) ? ("") : (productCode));
            break;
        }
        case "CustomerNameValue": {
            object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue",
                    (null == customerName) ? ("") : (customerName));
            break;
        }
        case "PlanNumValue": {
            object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue",
                    planNum);
            break;
        }
        case "ProductGraphicValue": {
            object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue",
                    (null == graphicCode) ? ("") : (graphicCode));
            if (!Units.strIsEmpty(graphicCode))
                object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject()
                        .addProperty("viewType", 1);
            break;
        }
        case "YaZhuangOrHuaXianValue": {
            /*
            if (null != yaZhuangOrHuaXian && yaZhuangOrHuaXian.compareTo("true") == 0)
                yaZhuangOrHuaXian = "";
            if (null != yaZhuangOrHuaXian && yaZhuangOrHuaXian.compareTo("false") == 0)
                yaZhuangOrHuaXian = "";
            */
            object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue",
                    (null == yaZhuangOrHuaXian) ? ("") : (yaZhuangOrHuaXian));
            if (!Units.strIsEmpty(yaZhuangOrHuaXian))
                object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject()
                        .addProperty("viewType", 1);
            break;
        }
        case "YaZhuangOrHuaXianSortValue": {
            /*
            System.out.println(yaZhuangOrHuaXianSort);
            if (null != yaZhuangOrHuaXianSort && yaZhuangOrHuaXianSort.compareTo("true") == 0)
                yaZhuangOrHuaXianSort = "";
            if (null != yaZhuangOrHuaXianSort && yaZhuangOrHuaXianSort.compareTo("false") == 0)
                yaZhuangOrHuaXianSort = "";
            */
            object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue",
                    (null == yaZhuangOrHuaXianSort) ? ("") : (yaZhuangOrHuaXianSort));
            if (!Units.strIsEmpty(yaZhuangOrHuaXianSort))
                object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject()
                        .addProperty("viewType", 1);
            break;
        }
        case "ProductStandardValue": {
            object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue",
                    (null == productStandard) ? ("") : (productStandard));
            if (!Units.strIsEmpty(productStandard))
                object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject()
                        .addProperty("viewType", 1);
            break;
        }
        case "ProductBatchValue": {
            //System.out.println("productBatch:" + productBatch);
            object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue",
                    (null == productBatch) ? ("") : (productBatch));
            if (!Units.strIsEmpty(productBatch))
                object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject()
                        .addProperty("viewType", 1);
            break;
        }
        case "ProductLengthValue": {
            object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue",
                    (null == productLength) ? ("") : (productLength));
            if (!Units.strIsEmpty(productLength))
                object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject()
                        .addProperty("viewType", 1);
            break;
        }
        case "MarkColorValue": {
            object.get("rowData" + (i + 1)).getAsJsonArray().get(1).getAsJsonObject().addProperty("fieldValue",
                    (null == productColor) ? ("") : (productColor));
            object.get("rowData" + (i + 1)).getAsJsonArray().get(2).getAsJsonObject().addProperty("fieldValue",
                    (null == productColor1) ? ("") : (productColor1));
            break;
        }
        case "MarkColorValue1": {
            break;
        }
        }
    }

    Gson gson = new Gson();
    /*
    if (isGuanShu == 1) {
    object.addProperty("childRowNum", 7);
    JsonElement serialElement = parser.parse("[{\"fieldName\":\"FinishedSerial\", \"fieldValue\":\"???\", \"viewType\":1,\"fieldType\":3,\"childRowNum\":1,\"textHorizon\":true,\"width\":1},"
            + "{\"fieldName\":\"FinishedSerialValue\", \"fieldValue\":\"%s\", \"viewType\":1,\"fieldType\":3,\"childRowNum\":1,\"textHorizon\":true,\"width\":1},"
            + "{\"fieldName\":\"CurSerial\", \"fieldValue\":\"???\", \"viewType\":1,\"fieldType\":3,\"childRowNum\":1,\"textHorizon\":true,\"width\":1},"
            + "{\"fieldName\":\"GuanShuSerial\", \"fieldValue\":\"\", \"viewType\":2,\"fieldType\":1,\"childRowNum\":1,\"textHorizon\":true,\"width\":1}]");
    object.add("rowData7", serialElement);
    }
        */
    this.productLineStructJson = gson.toJson(object);
    //        System.out.println("productLineStructJson" + this.productLineStructJson);
}