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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations