Example usage for com.google.gson JsonElement isJsonObject

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

Introduction

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

Prototype

public boolean isJsonObject() 

Source Link

Document

provides check for verifying if this element is a Json object or not.

Usage

From source file:com.cybozu.kintone.database.JsonParser.java

License:Apache License

/**
 * Converts the json string to the resultset object.
 * @param con//from w w w. ja v a2s.c om
 *            a connection object
 * @param json
 *            a json string
 * @return resultset object
 * @throws IOException
 */
public ResultSet jsonToResultSet(Connection con, String json) throws IOException {

    ResultSet rs = new ResultSet(con);
    com.google.gson.JsonParser parser = new com.google.gson.JsonParser();
    JsonElement root = parser.parse(json);

    if (root.isJsonObject()) {
        JsonObject rootObject = root.getAsJsonObject();
        JsonArray records = rootObject.get("records").getAsJsonArray();
        for (JsonElement elem : records) {
            Record record = readRecord(elem);
            if (record != null) {
                rs.add(record);
            }
        }

        if (!rootObject.get("totalCount").isJsonNull()) {
            rs.setTotalCount(rootObject.get("totalCount").getAsLong());

        }
    }

    return rs;
}

From source file:com.cybozu.kintone.database.JsonParser.java

License:Apache License

/**
 * Reads and parses each record element.
 * @param elem/* w  w w.  j  ava  2  s  . c om*/
 *            a json element represents a record object
 * @return the record object created
 * @throws IOException
 */
private Record readRecord(JsonElement elem) throws IOException {

    Record record = new Record();

    if (elem.isJsonObject()) {
        JsonObject obj = elem.getAsJsonObject();
        Set<Map.Entry<String, JsonElement>> set = obj.entrySet();
        for (Map.Entry<String, JsonElement> entry : set) {
            Field field = readField(entry.getKey(), entry.getValue());
            if (field != null) {
                record.addField(field.getName(), field);
            }
        }
    }

    return record;
}

From source file:com.cybozu.kintone.database.JsonParser.java

License:Apache License

/**
 * Reads and parses each field element./*from   w w w.j a v a  2 s . c o m*/
 * @param fieldName
 *            the field name
 * @param elem
 *            a json element represents a record object
 * @return the field object created
 * @throws IOException
 */
private Field readField(String fieldName, JsonElement fieldElem) throws IOException {

    Field field = null;

    if (!fieldElem.isJsonObject())
        return null;
    JsonObject obj = fieldElem.getAsJsonObject();

    FieldType type = FieldType.getEnum(obj.get("type").getAsString());
    JsonElement element = obj.get("value");

    Object object = null;
    String strVal = null;
    if (type == null || element == null)
        return null;

    if (!element.isJsonNull()) {
        switch (type) {
        case SINGLE_LINE_TEXT:
        case CALC:
        case MULTI_LINE_TEXT:
        case RICH_TEXT:
        case RADIO_BUTTON:
        case DROP_DOWN:
        case LINK:
        case STATUS:
        case RECORD_NUMBER:
        case NUMBER:
            object = element.getAsString();
            break;
        case __ID__:
        case __REVISION__:
            strVal = element.getAsString();
            try {
                object = Long.valueOf(strVal);
            } catch (NumberFormatException e) {
            }
            break;
        case DATE:
        case TIME:
        case DATETIME:
        case CREATED_TIME:
        case UPDATED_TIME:
            object = element.getAsString();
            break;
        case CHECK_BOX:
        case MULTI_SELECT:
        case CATEGORY:
            object = jsonToStringArray(element);
            break;
        case FILE:
            object = jsonToFileArray(element);
            break;
        case CREATOR:
        case MODIFIER:
            if (element.isJsonObject()) {
                Gson gson = new Gson();
                object = gson.fromJson(element, UserDto.class);
            }
            break;
        case USER_SELECT:
        case STATUS_ASSIGNEE:
            object = jsonToUserArray(element);
            break;
        case SUBTABLE:
            object = jsonToSubtable(element);
            break;
        }
    }
    field = new Field(fieldName, type, object);

    return field;
}

From source file:com.cybozu.kintone.database.JsonParser.java

License:Apache License

/**
 * Converts json element to the sub table object.
 * @param element json element/*from w  w  w .  java 2 s  . c  om*/
 * @return sub table object
 * @throws IOException
 */
private List<Record> jsonToSubtable(JsonElement element) throws IOException {
    List<Record> rs = new ArrayList<Record>();

    if (!element.isJsonArray())
        return null;

    JsonArray records = element.getAsJsonArray();
    for (JsonElement elem : records) {
        if (elem.isJsonObject()) {
            JsonObject obj = elem.getAsJsonObject();
            String id = obj.get("id").getAsString();
            JsonElement value = obj.get("value");
            Record record = readRecord(value);
            if (record != null) {
                try {
                    record.setId(Long.valueOf(id));
                } catch (NumberFormatException e) {
                }
                rs.add(record);
            }
        }
    }

    return rs;
}

From source file:com.cybozu.kintone.database.JsonParser.java

License:Apache License

/**
 * Retrieves the array of the ids from json string.
 * @param json//from   w  ww  .j a v a 2s  . c  o  m
 *            a json string
 * @return the array of the id
 * @throws IOException
 */
public List<Long> jsonToIDs(String json) throws IOException {
    com.google.gson.JsonParser parser = new com.google.gson.JsonParser();
    JsonElement root = parser.parse(json);

    List<Long> ids = new ArrayList<Long>();
    if (root.isJsonObject()) {
        JsonArray jsonIds = root.getAsJsonObject().get("ids").getAsJsonArray();
        for (JsonElement elem : jsonIds) {
            Long id = new Long(elem.getAsString());
            ids.add(id);
        }
    }

    return ids;
}

From source file:com.cybozu.kintone.database.JsonParser.java

License:Apache License

/**
 * Retrieves the file key string from json string.
 * @param json//  w  w  w. j ava2  s .  com
 *            a json string
 * @return the file key
 * @throws IOException
 */
public String jsonToFileKey(String json) throws IOException {
    com.google.gson.JsonParser parser = new com.google.gson.JsonParser();
    JsonElement root = parser.parse(json);

    String fileKey = null;
    if (root.isJsonObject()) {
        fileKey = root.getAsJsonObject().get("fileKey").getAsString();
    }

    return fileKey;
}

From source file:com.cybozu.kintone.database.JsonParser.java

License:Apache License

/**
 * Retrieves the revision string from json string.
 * @param json/*from ww  w. j  a v  a  2s .  c  o  m*/
 *            a json string
 * @return the revision number
 * @throws IOException
 */
public long jsonToRevision(String json) throws IOException {
    com.google.gson.JsonParser parser = new com.google.gson.JsonParser();
    JsonElement root = parser.parse(json);

    String revision = null;
    if (root.isJsonObject()) {
        revision = root.getAsJsonObject().get("revision").getAsString();
    }

    return Long.valueOf(revision);
}

From source file:com.cybozu.kintone.database.JsonParser.java

License:Apache License

/**
 * Retrieves the id string from json string.
 * @param json/*from www. j  ava2 s .  c  o m*/
 *            a json string
 * @return the id number
 * @throws IOException
 */
public long jsonToId(String json) throws IOException {
    com.google.gson.JsonParser parser = new com.google.gson.JsonParser();
    JsonElement root = parser.parse(json);

    String id = null;
    if (root.isJsonObject()) {
        id = root.getAsJsonObject().get("id").getAsString();
    }

    return Long.valueOf(id);
}

From source file:com.cybozu.kintone.database.JsonParser.java

License:Apache License

/**
 * Convert json string to AppDto array.//  w w  w  .j  av a 2s.  c  o m
 * @param json
 *            a json string
 * @return the list of app object
 * @throws IOException
 */
public List<AppDto> jsonToApps(String json) throws IOException {
    com.google.gson.JsonParser parser = new com.google.gson.JsonParser();
    JsonElement root = parser.parse(json);

    if (!root.isJsonObject())
        return null;

    JsonArray apps = root.getAsJsonObject().get("apps").getAsJsonArray();

    if (!apps.isJsonArray())
        return null;

    Type collectionType = new TypeToken<Collection<AppDto>>() {
    }.getType();
    Gson gson = new Gson();

    return gson.fromJson(apps, collectionType);
}

From source file:com.cybozu.kintone.database.JsonParser.java

License:Apache License

/**
 * Converts the json string to the commentset object.
 * @param json/* ww  w . j  a v  a  2 s.c  o  m*/
 *            a json string
 * @return commentset object
 * @throws IOException
 */
public CommentSet jsonToCommentSet(Connection con, String json) throws IOException {

    CommentSet cs = new CommentSet();
    com.google.gson.JsonParser parser = new com.google.gson.JsonParser();
    JsonElement root = parser.parse(json);

    if (root.isJsonObject()) {
        JsonObject obj = root.getAsJsonObject();
        JsonArray comments = obj.get("comments").getAsJsonArray();
        for (JsonElement elem : comments) {
            Comment comment = readComment(elem);
            if (comment != null) {
                cs.add(comment);
            }
        }

        cs.setNewer(obj.get("newer").getAsBoolean());
        cs.setOlder(obj.get("older").getAsBoolean());
    }
    return cs;
}