Search in sources :

Example 1 with JSONArray

use of com.alibaba.fastjson2.JSONArray in project canal by alibaba.

the class CanalClusterController method clustersAndServers.

@GetMapping(value = "/clustersAndServers")
public BaseModel<List<?>> clustersAndServers(@PathVariable String env) {
    List<CanalCluster> clusters = canalClusterServic.findList(new CanalCluster());
    JSONObject group = new JSONObject();
    group.put("label", "集群");
    JSONArray jsonArray = new JSONArray();
    clusters.forEach(cluster -> {
        JSONObject item = new JSONObject();
        item.put("label", cluster.getName());
        item.put("value", "cluster:" + cluster.getId());
        jsonArray.add(item);
    });
    group.put("options", jsonArray);
    NodeServer param = new NodeServer();
    param.setClusterId(-1L);
    // 取所有standalone的节点
    List<NodeServer> servers = nodeServerService.findAll(param);
    JSONObject group2 = new JSONObject();
    group2.put("label", "单机主机");
    JSONArray jsonArray2 = new JSONArray();
    servers.forEach(server -> {
        JSONObject item = new JSONObject();
        item.put("label", server.getName());
        item.put("value", "server:" + server.getId());
        jsonArray2.add(item);
    });
    group2.put("options", jsonArray2);
    List<JSONObject> result = new ArrayList<>();
    result.add(group);
    result.add(group2);
    return BaseModel.getInstance(result);
}
Also used : JSONObject(com.alibaba.fastjson2.JSONObject) JSONArray(com.alibaba.fastjson2.JSONArray) ArrayList(java.util.ArrayList) CanalCluster(com.alibaba.otter.canal.admin.model.CanalCluster) NodeServer(com.alibaba.otter.canal.admin.model.NodeServer) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 2 with JSONArray

use of com.alibaba.fastjson2.JSONArray in project SpringBoot-Hello by ruiyeclub.

the class FastJson2ApplicationTests method testParseArray.

/**
 * JSON字符串转换成数组
 */
@Test
public void testParseArray() {
    String str = "[\"id\", 123]";
    JSONArray objects = JSON.parseArray(str);
    String idStr = objects.getString(0);
    System.out.println(idStr);
}
Also used : JSONArray(com.alibaba.fastjson2.JSONArray) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 3 with JSONArray

use of com.alibaba.fastjson2.JSONArray in project druid by alibaba.

the class MonitorStatService method getWebURIStatDataList.

@SuppressWarnings("unchecked")
private String getWebURIStatDataList(Map<String, String> parameters) {
    Map<String, ServiceNode> allNodeMap = getServiceAllNodeMap(parameters);
    List<Map<String, Object>> arrayMap = new ArrayList<>();
    for (String nodeKey : allNodeMap.keySet()) {
        ServiceNode serviceNode = allNodeMap.get(nodeKey);
        String url = getRequestUrl(parameters, serviceNode, "/druid/weburi.json");
        WebResult dataSourceResult = HttpUtil.get(url, WebResult.class);
        if (dataSourceResult != null) {
            List<WebResult.ContentBean> nodeContent = dataSourceResult.getContent();
            if (nodeContent != null) {
                for (WebResult.ContentBean contentBean : nodeContent) {
                    Map<String, Object> map = JSONObject.parseObject(JSONObject.toJSONString(contentBean), Map.class);
                    arrayMap.add(map);
                }
            }
        }
    }
    List<Map<String, Object>> maps = comparatorOrderBy(arrayMap, parameters);
    String jsonString = JSON.toJSONString(maps);
    JSONArray objects = JSON.parseArray(jsonString);
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("ResultCode", RESULT_CODE_SUCCESS);
    jsonObject.put("Content", objects);
    return jsonObject.toJSONString();
}
Also used : ServiceNode(com.alibaba.druid.admin.model.ServiceNode) ArrayList(java.util.ArrayList) JSONArray(com.alibaba.fastjson2.JSONArray) JSONObject(com.alibaba.fastjson2.JSONObject) JSONObject(com.alibaba.fastjson2.JSONObject) WebResult(com.alibaba.druid.admin.model.dto.WebResult) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 4 with JSONArray

use of com.alibaba.fastjson2.JSONArray in project druid by alibaba.

the class MySqlParameterizedOutputVisitorTest_43 method restore.

public static String restore(String sql, String table, String params) /*JSONArray paramsArray, JSONArray destArray*/
{
    JSONArray destArray = JSON.parseArray(table.replaceAll("''", "'"));
    params = StringUtils.replace(params.replaceAll("''", "'"), "\\\"", "\"");
    JSONArray paramsArray = JSON.parseArray(params);
    DbType dbType = JdbcConstants.MYSQL;
    List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
    SQLStatement stmt = stmtList.get(0);
    StringBuilder out = new StringBuilder();
    SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, dbType);
    List<Object> paramsList = new ArrayList<Object>(paramsArray);
    visitor.setParameters(paramsList);
    SchemaStatVisitor schemaStatVisitor = new MySqlSchemaStatVisitor();
    stmt.accept(schemaStatVisitor);
    JSONArray srcArray = new JSONArray();
    for (Map.Entry<TableStat.Name, TableStat> entry : schemaStatVisitor.getTables().entrySet()) {
        System.out.println(entry.getKey().getName());
        srcArray.add(entry.getKey().getName());
    }
    for (int i = 0; i < srcArray.size(); i++) {
        visitor.addTableMapping(srcArray.getString(i), destArray.getString(i));
    }
    stmt.accept(visitor);
    return out.toString();
}
Also used : JSONArray(com.alibaba.fastjson2.JSONArray) ArrayList(java.util.ArrayList) TableStat(com.alibaba.druid.stat.TableStat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType) MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor) Map(java.util.Map) MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 5 with JSONArray

use of com.alibaba.fastjson2.JSONArray in project druid by alibaba.

the class MySqlParameterizedOutputVisitorTest_44 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "select 1 from a where c1 in (date_format(date_add(curdate(), INTERVAL -7 DAY), '%Y%m%d'))";
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    List<SQLStatement> stmtList = parser.parseStatementList();
    SQLStatement statement = stmtList.get(0);
    StringBuilder out = new StringBuilder();
    SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.MYSQL);
    List<Object> parameters = new ArrayList<Object>();
    visitor.setParameterized(true);
    visitor.setParameterizedMergeInList(true);
    visitor.setParameters(parameters);
    /*visitor.setPrettyFormat(false);*/
    statement.accept(visitor);
    /* JSONArray array = new JSONArray();
        for(String table : visitor.getTables()){
            array.add(table.replaceAll("`",""));
        }*/
    String psql = out.toString();
    System.out.println(psql);
    assertEquals("SELECT ?\n" + "FROM a\n" + "WHERE c1 IN (date_format(date_add(curdate(), INTERVAL ? DAY), '%Y%m%d'))", psql);
    String params_json = JSON.toJSONString(parameters, JSONWriter.Feature.WriteClassName);
    System.out.println(params_json);
    JSONArray jsonArray = JSON.parseArray(params_json);
    String json = JSON.toJSONString(jsonArray, JSONWriter.Feature.WriteClassName);
    assertEquals("[1,-7]", json);
    String rsql = SQLUtils.toSQLString(SQLUtils.parseStatements(psql, dbType), dbType, jsonArray);
    assertEquals("SELECT 1\n" + "FROM a\n" + "WHERE c1 IN (date_format(date_add(curdate(), INTERVAL -7 DAY), '%Y%m%d'))", rsql);
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) ArrayList(java.util.ArrayList) JSONArray(com.alibaba.fastjson2.JSONArray) SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType)

Aggregations

JSONArray (com.alibaba.fastjson2.JSONArray)16 ArrayList (java.util.ArrayList)13 DbType (com.alibaba.druid.DbType)10 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)10 SQLASTOutputVisitor (com.alibaba.druid.sql.visitor.SQLASTOutputVisitor)10 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)8 JSONObject (com.alibaba.fastjson2.JSONObject)4 Map (java.util.Map)3 ServiceNode (com.alibaba.druid.admin.model.ServiceNode)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 SqlListResult (com.alibaba.druid.admin.model.dto.SqlListResult)1 WebResult (com.alibaba.druid.admin.model.dto.WebResult)1 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)1 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)1 TableStat (com.alibaba.druid.stat.TableStat)1 JSONException (com.alibaba.fastjson2.JSONException)1 CanalCluster (com.alibaba.otter.canal.admin.model.CanalCluster)1 NodeServer (com.alibaba.otter.canal.admin.model.NodeServer)1 ESException (com.sakura.common.es.exception.ESException)1