Example usage for com.google.gson JsonArray add

List of usage examples for com.google.gson JsonArray add

Introduction

In this page you can find the example usage for com.google.gson JsonArray add.

Prototype

public void add(JsonElement element) 

Source Link

Document

Adds the specified element to self.

Usage

From source file:com.gst.infrastructure.core.api.JodaLocalDateAdapter.java

License:Apache License

@SuppressWarnings("unused")
@Override/* www. j a  v  a 2  s  . co m*/
public JsonElement serialize(final LocalDate src, final Type typeOfSrc,
        final JsonSerializationContext context) {

    JsonArray array = null;
    if (src != null) {
        array = new JsonArray();
        array.add(new JsonPrimitive(src.getYearOfEra()));
        array.add(new JsonPrimitive(src.getMonthOfYear()));
        array.add(new JsonPrimitive(src.getDayOfMonth()));
    }

    return array;
}

From source file:com.gst.infrastructure.core.api.JodaMonthDayAdapter.java

License:Apache License

@SuppressWarnings("unused")
@Override/*from  w w w  . ja va2s .  c  o  m*/
public JsonElement serialize(final MonthDay src, final Type typeOfSrc, final JsonSerializationContext context) {

    JsonArray array = null;
    if (src != null) {
        array = new JsonArray();
        array.add(new JsonPrimitive(src.getMonthOfYear()));
        array.add(new JsonPrimitive(src.getDayOfMonth()));
    }

    return array;
}

From source file:com.hangum.tadpole.engine.sql.util.export.JsonExpoter.java

License:Open Source License

public static JsonArray makeContentArray(String tableName, QueryExecuteResultDTO rsDAO, int intLimitCnt) {
    List<Map<Integer, Object>> dataList = rsDAO.getDataList().getData();
    Map<Integer, String> mapLabelName = rsDAO.getColumnLabelName();
    JsonArray jsonArry = new JsonArray();
    for (int i = 0; i < dataList.size(); i++) {
        Map<Integer, Object> mapColumns = dataList.get(i);

        JsonObject jsonObj = new JsonObject();
        for (int j = 1; j < mapLabelName.size(); j++) {
            String columnName = mapLabelName.get(j);

            jsonObj.addProperty(StringUtils.trimToEmpty(columnName), "" + mapColumns.get(j));
        }/*  w w w  . j  av  a  2 s.  com*/
        jsonArry.add(jsonObj);
        if (i == intLimitCnt)
            break;
    }
    return jsonArry;
}

From source file:com.hangum.tadpole.engine.sql.util.export.JsonExpoter.java

License:Open Source License

public static JsonArray makeMetaArray(QueryExecuteResultDTO rsDAO) throws SQLException {
    Map<Integer, String> mapLabelName = rsDAO.getColumnLabelName();

    //ResultSetMetaData rsm = rsDAO.getColumnMetaData();   

    JsonArray jsonMetaArry = new JsonArray();
    for (int j = 1; j < mapLabelName.size(); j++) {

        JsonObject jsonMetaObj = new JsonObject();
        jsonMetaObj.addProperty("position", j);
        jsonMetaObj.addProperty("column_name", mapLabelName.get(j));
        //            jsonMetaObj.addProperty("data_type", rsm.getColumnTypeName(j));
        jsonMetaObj.addProperty("data_type", rsDAO.getColumnType().get(j));
        //            jsonMetaObj.addProperty("column_size", rsm.getColumnDisplaySize(j));

        if (!RDBTypeToJavaTypeUtils.isNumberType(rsDAO.getColumnType().get(j))) {
            jsonMetaObj.addProperty("column_size", 90);
        } else {//w w w . j  a  v a 2 s . c  o m
            jsonMetaObj.addProperty("column_size", 150);
        }

        jsonMetaArry.add(jsonMetaObj);
    }

    return jsonMetaArry;

}

From source file:com.hangum.tadpole.engine.sql.util.QueryUtils.java

License:Open Source License

/**
 * execute DML/*from   w  w w.j  a  va2s  .co m*/
 * 
 * @param userDB
 * @param strQuery
 * @param listParam
 * @param resultType
 * @throws Exception
 */
public static String executeDML(final UserDBDAO userDB, final String strQuery, final List<Object> listParam,
        final String resultType) throws Exception {
    SqlMapClient client = TadpoleSQLManager.getInstance(userDB);
    Object effectObject = runSQLOther(userDB, strQuery, listParam);

    String strReturn = "";
    if (resultType.equals(RESULT_TYPE.CSV.name())) {
        final StringWriter stWriter = new StringWriter();
        CSVWriter csvWriter = new CSVWriter(stWriter, ',');

        String[] arryString = new String[2];
        arryString[0] = "effectrow";
        arryString[1] = String.valueOf(effectObject);
        csvWriter.writeNext(arryString);

        strReturn = stWriter.toString();
    } else if (resultType.equals(RESULT_TYPE.JSON.name())) {
        final JsonArray jsonArry = new JsonArray();
        JsonObject jsonObj = new JsonObject();
        jsonObj.addProperty("effectrow", String.valueOf(effectObject));
        jsonArry.add(jsonObj);

        strReturn = JSONUtil.getPretty(jsonArry.toString());
    } else {//if(resultType.equals(RESULT_TYPE.XML.name())) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        final Document doc = builder.newDocument();
        final Element results = doc.createElement("Results");
        doc.appendChild(results);

        Element row = doc.createElement("Row");
        results.appendChild(row);
        Element node = doc.createElement("effectrow");
        node.appendChild(doc.createTextNode(String.valueOf(effectObject)));
        row.appendChild(node);

        DOMSource domSource = new DOMSource(doc);
        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("indent-number", 4);

        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        final StringWriter stWriter = new StringWriter();
        StreamResult sr = new StreamResult(stWriter);
        transformer.transform(domSource, sr);

        strReturn = stWriter.toString();
    }

    return strReturn;
}

From source file:com.hangum.tadpole.engine.sql.util.QueryUtils.java

License:Open Source License

/**
 * query to json/*from  www. j a va2s  .c  o  m*/
 * 
 * @param userDB
 * @param strQuery
 * @param listParam
 */
@SuppressWarnings("deprecation")
public static JsonArray selectToJson(final UserDBDAO userDB, final String strQuery,
        final List<Object> listParam) throws Exception {
    final JsonArray jsonArry = new JsonArray();

    SqlMapClient client = TadpoleSQLManager.getInstance(userDB);
    QueryRunner qr = new QueryRunner(client.getDataSource());
    qr.query(strQuery, listParam.toArray(), new ResultSetHandler<Object>() {

        @Override
        public Object handle(ResultSet rs) throws SQLException {
            ResultSetMetaData metaData = rs.getMetaData();

            while (rs.next()) {
                JsonObject jsonObj = new JsonObject();
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    String columnName = metaData.getColumnLabel(i);
                    String value = rs.getString(i) == null ? "" : rs.getString(i);

                    jsonObj.addProperty(columnName.toLowerCase(), value);
                }
                jsonArry.add(jsonObj);
            }

            return jsonArry;
        }
    });

    return jsonArry;
}

From source file:com.haulmont.restapi.service.DatatypesControllerManager.java

License:Apache License

public String getDatatypesJson() {
    JsonArray jsonArray = new JsonArray();

    try {/*from   ww  w .ja  va2 s  .com*/
        for (String id : datatypes.getIds()) {
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("id", id);
            jsonObject.addProperty("name", id); // for backward compatibility

            Datatype datatype = datatypes.get(id);
            if (datatype instanceof ParameterizedDatatype) {
                Map<String, Object> parameters = ((ParameterizedDatatype) datatype).getParameters();
                for (Map.Entry<String, Object> entry : parameters.entrySet()) {
                    jsonObject.addProperty(entry.getKey(), entry.getValue().toString());
                }
            }

            jsonArray.add(jsonObject);
        }
    } catch (Exception e) {
        log.error("Fail to get datatype settings", e);
        throw new RestAPIException("Fail to get datatype settings", e.getMessage(),
                HttpStatus.INTERNAL_SERVER_ERROR);
    }

    return jsonArray.toString();
}

From source file:com.hbm.devices.jet.JetPeer.java

License:Open Source License

private JsonObject fillPath(Matcher matcher) {
    JsonObject path = new JsonObject();

    if ((matcher.contains != null && !matcher.contains.isEmpty())) {
        path.addProperty("contains", matcher.contains);

    }/*from   w ww.j a v  a  2  s. co  m*/

    if ((matcher.startsWith != null && !matcher.startsWith.isEmpty())) {
        path.addProperty("startsWith", matcher.startsWith);
    }

    if ((matcher.endsWith != null && !matcher.endsWith.isEmpty())) {
        path.addProperty("endsWith", matcher.endsWith);
    }

    if ((matcher.equals != null && !matcher.equals.isEmpty())) {
        path.addProperty("equals", matcher.equals);
    }

    if ((matcher.equalsNot != null && !matcher.equalsNot.isEmpty())) {
        path.addProperty("equalsNot", matcher.equalsNot);
    }

    if ((matcher.containsAllOf != null) && (matcher.containsAllOf.length > 0)) {
        JsonArray containsArray = new JsonArray();
        for (String element : matcher.containsAllOf) {
            containsArray.add(new JsonPrimitive(element));
        }

        path.add("containsAllOf", containsArray);
    }

    if ((path.entrySet() != null) && (path.entrySet().size() > 0)) {
        return path;
    } else {
        return null;
    }
}

From source file:com.hbm.devices.jet.JetPeer.java

License:Open Source License

private JsonElement createJsonArray(String[] group) {
    final JsonArray array = new JsonArray();
    for (String entry : group) {
        array.add(entry);
    }/*from  w  w w.j a va2s . com*/
    return array;
}