Search in sources :

Example 1 with JSONArray

use of com.alibaba.fastjson.JSONArray in project Mycat-Server by MyCATApache.

the class SwitchCommitListener method modifyZkRules.

private static CuratorTransactionFinal modifyZkRules(CuratorTransactionFinal transactionFinal, String ruleName, List<String> newDataNodes) throws Exception {
    CuratorFramework client = ZKUtils.getConnection();
    String rulePath = ZKUtils.getZKBasePath() + "rules/function";
    JSONArray jsonArray = JSON.parseArray(new String(client.getData().forPath(rulePath), "UTF-8"));
    for (Object obj : jsonArray) {
        JSONObject func = (JSONObject) obj;
        if (ruleName.equalsIgnoreCase(func.getString("name"))) {
            JSONArray property = func.getJSONArray("property");
            for (Object o : property) {
                JSONObject count = (JSONObject) o;
                if ("count".equals(count.getString("name"))) {
                    Integer xcount = Integer.parseInt(count.getString("value"));
                    count.put("value", String.valueOf(xcount + newDataNodes.size()));
                    if (transactionFinal == null) {
                        transactionFinal = ZKUtils.getConnection().inTransaction().setData().forPath(rulePath, JSON.toJSONBytes(jsonArray)).and();
                    } else {
                        transactionFinal.setData().forPath(rulePath, JSON.toJSONBytes(jsonArray));
                    }
                }
            }
        }
    }
    return transactionFinal;
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) JSONObject(com.alibaba.fastjson.JSONObject)

Example 2 with JSONArray

use of com.alibaba.fastjson.JSONArray in project Mycat-Server by MyCATApache.

the class SwitchCommitListener method modifyTableConfigRules.

private static CuratorTransactionFinal modifyTableConfigRules(CuratorTransactionFinal transactionFinal, String schemal, String table, List<String> newDataNodes) throws Exception {
    CuratorFramework client = ZKUtils.getConnection();
    String rulePath = ZKUtils.getZKBasePath() + "schema/schema";
    JSONArray jsonArray = JSON.parseArray(new String(client.getData().forPath(rulePath), "UTF-8"));
    for (Object obj : jsonArray) {
        JSONObject func = (JSONObject) obj;
        if (schemal.equalsIgnoreCase(func.getString("name"))) {
            JSONArray property = func.getJSONArray("table");
            for (Object o : property) {
                JSONObject tt = (JSONObject) o;
                String tableName = tt.getString("name");
                String dataNode = tt.getString("dataNode");
                if (table.equalsIgnoreCase(tableName)) {
                    List<String> allDataNodes = new ArrayList<>();
                    allDataNodes.add(dataNode);
                    allDataNodes.addAll(newDataNodes);
                    tt.put("dataNode", Joiner.on(",").join(allDataNodes));
                    if (transactionFinal == null) {
                        transactionFinal = ZKUtils.getConnection().inTransaction().setData().forPath(rulePath, JSON.toJSONBytes(jsonArray)).and();
                    } else {
                        transactionFinal.setData().forPath(rulePath, JSON.toJSONBytes(jsonArray));
                    }
                }
            }
        }
    }
    return transactionFinal;
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) JSONObject(com.alibaba.fastjson.JSONObject)

Example 3 with JSONArray

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

the class ObjectArrayCodec method toObjectArray.

@SuppressWarnings("unchecked")
private <T> T toObjectArray(DefaultJSONParser parser, Class<?> componentType, JSONArray array) {
    if (array == null) {
        return null;
    }
    int size = array.size();
    Object objArray = Array.newInstance(componentType, size);
    for (int i = 0; i < size; ++i) {
        Object value = array.get(i);
        if (value == array) {
            Array.set(objArray, i, objArray);
            continue;
        }
        if (componentType.isArray()) {
            Object element;
            if (componentType.isInstance(value)) {
                element = value;
            } else {
                element = toObjectArray(parser, componentType, (JSONArray) value);
            }
            Array.set(objArray, i, element);
        } else {
            Object element = null;
            if (value instanceof JSONArray) {
                boolean contains = false;
                JSONArray valueArray = (JSONArray) value;
                int valueArraySize = valueArray.size();
                for (int y = 0; y < valueArraySize; ++y) {
                    Object valueItem = valueArray.get(y);
                    if (valueItem == array) {
                        valueArray.set(i, objArray);
                        contains = true;
                    }
                }
                if (contains) {
                    element = valueArray.toArray();
                }
            }
            if (element == null) {
                element = TypeUtils.cast(value, componentType, parser.getConfig());
            }
            Array.set(objArray, i, element);
        }
    }
    array.setRelatedArray(objArray);
    array.setComponentType(componentType);
    // TODO
    return (T) objArray;
}
Also used : JSONArray(com.alibaba.fastjson.JSONArray)

Example 4 with JSONArray

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

the class AtomicCodec method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
    if (parser.lexer.token() == JSONToken.NULL) {
        parser.lexer.nextToken(JSONToken.COMMA);
        return null;
    }
    JSONArray array = new JSONArray();
    parser.parseArray(array);
    if (clazz == AtomicIntegerArray.class) {
        AtomicIntegerArray atomicArray = new AtomicIntegerArray(array.size());
        for (int i = 0; i < array.size(); ++i) {
            atomicArray.set(i, array.getInteger(i));
        }
        return (T) atomicArray;
    }
    AtomicLongArray atomicArray = new AtomicLongArray(array.size());
    for (int i = 0; i < array.size(); ++i) {
        atomicArray.set(i, array.getLong(i));
    }
    return (T) atomicArray;
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) JSONArray(com.alibaba.fastjson.JSONArray) AtomicLongArray(java.util.concurrent.atomic.AtomicLongArray)

Example 5 with JSONArray

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

the class CollectionCodec method deserialze.

@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    if (parser.lexer.token() == JSONToken.NULL) {
        parser.lexer.nextToken(JSONToken.COMMA);
        return null;
    }
    if (type == JSONArray.class) {
        JSONArray array = new JSONArray();
        parser.parseArray(array);
        return (T) array;
    }
    Collection list = TypeUtils.createCollection(type);
    Type itemType = TypeUtils.getCollectionItemType(type);
    parser.parseArray(itemType, list, fieldName);
    return (T) list;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) JSONArray(com.alibaba.fastjson.JSONArray) Collection(java.util.Collection)

Aggregations

JSONArray (com.alibaba.fastjson.JSONArray)93 JSONObject (com.alibaba.fastjson.JSONObject)37 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)8 List (java.util.List)7 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 HashMap (java.util.HashMap)4 Test (org.testng.annotations.Test)4 IOException (java.io.IOException)3 BigDecimal (java.math.BigDecimal)3 ParseException (java.text.ParseException)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 Type (java.lang.reflect.Type)2