Search in sources :

Example 56 with JSONArray

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

the class ParseUtil method main.

public static void main(String[] args) {
    //     String sql = "alter table sql_perf add index `idx_instance_8` (`host`,`port`,`hashcode`,`item`,`time`,`value`);";
    /*  String sql = "CREATE INDEX PersonIndex\n" +
                "ON Person (LastName) ";*/
    //   String sql = "CREATE TABLE t (id int );";
    // String sql = "ALTER TABLE `app_api_dup_control`\n\tDROP INDEX `idx_url_uuid`,\n\tADD UNIQUE KEY `uk_url_uuid` (uuid, url)";
    /*  String sql = "ALTER TABLE `push_seed_0000`\n" +
                "\tADD KEY `idx_betstatus_gmtcreate` (bet_status, gmt_create),\n" +
                "\tADD KEY `idx_winstatus_gmtcreate` (win_status, gmt_create)";*/
    //  System.out.println(getIdxInfo(sql, "AA", null));
    //   System.out.println(DateTimeUtils.toYyyyMMddhhmmss(new Date()));
    /*String ip = "xx";
        System.out.println(IP_PATTERN.matcher(ip).find());*/
    /* String sql = "\nalter table `dms_sign_info_0222` modify column `station_id` bigint comment '分拣流水中记录的分拨中心ID'";

        List<IdxFlagInfo> list = getIdxInfo(sql,"XX",null);
        System.out.println(list);*/
    // String sql = "select * from task where status = 1 and valid = 1 and type <> 100 and type <> 99 order by priority desc,gmt_create limit 0,500";
    //  System.out.println(parseSQL(sql));
    /*String sql = "SELECT IFNULL(SUM(CASE WHEN `tms_waybill_detail`.`dissendout_status` = ? AND DATE_ADD(`tms_waybill_detail`.`rdc_accept_time`, INTERVAL ? HOUR) < `tms_waybill_detail`.`dissendout_time` OR `tms_waybill_detail`.`dissendout_status` = ? AND DATE_ADD(`tms_waybill_detail`.`rdc_accept_time`, INTERVAL ? HOUR) < NOW() THEN ? ELSE ? END), ?) AS `count` FROM tms_waybill_detail `tms_waybill_detail` WHERE `tms_waybill_detail`.`rdc_accept_time` >= ? AND `tms_waybill_detail`.`rdc_accept_time` < ? AND `tms_waybill_detail`.`districenter_code` = ? AND `tms_waybill_detail`.`schedule_code` = ?";

        String table = "[\"tms_waybill_detail_0014\"]";
        String params = "[1,24,0,24,1,0,0,\"2017-01-15 00:00:00\",\"2017-01-15 17:32:03.558\",686,\"10102\"]";

        System.out.println("----- : "+restore(sql,table,params));*/
    String sql = "/* 0bba613214845441110397435e/0.4.6.25// */select `f`.`id`,`f`.`biz_id`,`f`.`user_id`,`f`.`file_name`,`f`.`parent_id`,`f`.`length`,`f`.`type`,`f`.`stream_key`,`f`.`biz_status`,`f`.`mark`,`f`.`content_modified`,`f`.`status`,`f`.`gmt_create`,`f`.`gmt_modified`,`f`.`md5`,`f`.`extra_str1`,`f`.`extra_str2`,`f`.`extra_str3`,`f`.`extra_num1`,`f`.`extra_num2`,`f`.`extra_num3`,`f`.`safe`,`f`.`open_status`,`f`.`inner_mark`,`f`.`sys_extra`,`f`.`feature`,`f`.`domain_option`,`f`.`version`,`f`.`reference_type`,`f`.`dentry_type`,`f`.`space_id`,`f`.`extension`,`f`.`creator_id`,`f`.`modifier_id`,`f`.`store_type`,`f`.`link_mark`,`f`.`content_type` from  ( select `vfs_dentry_2664`.`id` from `vfs_dentry_2664` FORCE INDEX (idx_gmt) where ((`vfs_dentry_2664`.`extra_str1` = '97d45a25df387b4460e5b4151daeb452') AND (`vfs_dentry_2664`.`biz_id` = 62) AND (`vfs_dentry_2664`.`status` = 0) AND (`vfs_dentry_2664`.`user_id` = '11168360') AND (`vfs_dentry_2664`.`dentry_type` = 1)) limit 0,50 )  `t`  join `vfs_dentry_2664` `f` on `t`.`id` = `f`.`id` where ((`t`.`id` = `f`.`id`) AND (`f`.`user_id` = 11168360))";
    // SQLStatement sqlStatement = getStatement(sql);
    String sqlTempalte = parseSQL(sql);
    JSONArray array = new JSONArray();
    array.add("VFS_DENTRY_001");
    System.out.println(restore(sqlTempalte, array.toJSONString(), new JSONArray().toJSONString()));
}
Also used : JSONArray(com.alibaba.fastjson.JSONArray)

Example 57 with JSONArray

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

the class ParseUtil method restore.

public static String restore(String sql, String table, String params) /*JSONArray paramsArray, JSONArray destArray*/
{
    JSONArray destArray = JSON.parseArray(table.replaceAll("''", "'"));
    JSONArray paramsArray = JSON.parseArray(params.replaceAll("''", "'"));
    String 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 = Lists.newArrayList(paramsArray);
    visitor.setParameters(paramsList);
    JSONArray srcArray = getSrcArray(sql);
    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.fastjson.JSONArray) SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 58 with JSONArray

use of com.alibaba.fastjson.JSONArray in project fastjson by alibaba.

the class ResolveFieldDeserializer method setValue.

@SuppressWarnings("unchecked")
public void setValue(Object object, Object value) {
    if (map != null) {
        map.put(key, value);
        return;
    }
    if (collection != null) {
        collection.add(value);
        return;
    }
    list.set(index, value);
    if (list instanceof JSONArray) {
        JSONArray jsonArray = (JSONArray) list;
        Object array = jsonArray.getRelatedArray();
        if (array != null) {
            int arrayLength = Array.getLength(array);
            if (arrayLength > index) {
                Object item;
                if (jsonArray.getComponentType() != null) {
                    item = TypeUtils.cast(value, jsonArray.getComponentType(), parser.getConfig());
                } else {
                    item = value;
                }
                Array.set(array, index, item);
            }
        }
    }
}
Also used : JSONArray(com.alibaba.fastjson.JSONArray)

Example 59 with JSONArray

use of com.alibaba.fastjson.JSONArray in project fastjson by alibaba.

the class ObjectArrayCodec method deserialze.

@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    final JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.NULL) {
        lexer.nextToken(JSONToken.COMMA);
        return null;
    }
    if (lexer.token() == JSONToken.LITERAL_STRING) {
        byte[] bytes = lexer.bytesValue();
        lexer.nextToken(JSONToken.COMMA);
        return (T) bytes;
    }
    Class componentClass;
    Type componentType;
    if (type instanceof GenericArrayType) {
        GenericArrayType clazz = (GenericArrayType) type;
        componentType = clazz.getGenericComponentType();
        if (componentType instanceof TypeVariable) {
            TypeVariable typeVar = (TypeVariable) componentType;
            Type objType = parser.getContext().type;
            if (objType instanceof ParameterizedType) {
                ParameterizedType objParamType = (ParameterizedType) objType;
                Type objRawType = objParamType.getRawType();
                Type actualType = null;
                if (objRawType instanceof Class) {
                    TypeVariable[] objTypeParams = ((Class) objRawType).getTypeParameters();
                    for (int i = 0; i < objTypeParams.length; ++i) {
                        if (objTypeParams[i].getName().equals(typeVar.getName())) {
                            actualType = objParamType.getActualTypeArguments()[i];
                        }
                    }
                }
                if (actualType instanceof Class) {
                    componentClass = (Class) actualType;
                } else {
                    componentClass = Object.class;
                }
            } else {
                componentClass = TypeUtils.getClass(typeVar.getBounds()[0]);
            }
        } else {
            componentClass = TypeUtils.getClass(componentType);
        }
    } else {
        Class clazz = (Class) type;
        componentType = componentClass = clazz.getComponentType();
    }
    JSONArray array = new JSONArray();
    parser.parseArray(componentType, array, fieldName);
    return (T) toObjectArray(parser, componentClass, array);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) JSONArray(com.alibaba.fastjson.JSONArray) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 60 with JSONArray

use of com.alibaba.fastjson.JSONArray in project fastjson by alibaba.

the class CastTest method test_0.

public void test_0() throws Exception {
    String text;
    {
        List<Object> list = new ArrayList<Object>();
        list.add(new Header());
        Body body = new Body("张三");
        body.getItems().add(new Item());
        list.add(body);
        text = JSON.toJSONString(list);
        System.out.println(text);
    }
    JSONArray array = JSON.parseArray(text);
//        Body body = array.getObject(1, Body.class);
//        Assert.assertEquals(1, body.getItems().size());
//        Assert.assertEquals("张三", body.getName());
}
Also used : JSONArray(com.alibaba.fastjson.JSONArray) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

JSONArray (com.alibaba.fastjson.JSONArray)95 JSONObject (com.alibaba.fastjson.JSONObject)39 ArrayList (java.util.ArrayList)13 List (java.util.List)9 Test (org.junit.Test)8 StringReader (java.io.StringReader)6 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)5 JSONReaderScanner (com.alibaba.fastjson.parser.JSONReaderScanner)5 Reader (java.io.Reader)5 WXSDKInstanceTest (com.taobao.weex.WXSDKInstanceTest)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Test (org.testng.annotations.Test)4 BigDecimal (java.math.BigDecimal)3 ParseException (java.text.ParseException)3 Map (java.util.Map)3 Setting (org.aisen.android.common.setting.Setting)3 JSONReader (com.alibaba.fastjson.JSONReader)2 InputStream (java.io.InputStream)2 ParameterizedType (java.lang.reflect.ParameterizedType)2