exchange.apexpro.connector.impl.utils.JsonWrapperArray Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of apexpro-connector-java Show documentation
Show all versions of apexpro-connector-java Show documentation
A lightweight library to ApeX-Protocol
package exchange.apexpro.connector.impl.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import exchange.apexpro.connector.exception.ApexProApiException;
import java.math.BigDecimal;
import java.util.List;
import java.util.LinkedList;
public class JsonWrapperArray {
private JSONArray array = null;
public JsonWrapperArray(JSONArray array) {
this.array = array;
}
public JsonWrapper getJsonObjectAt(int index) {
if (array != null && array.size() > index) {
JSONObject object = (JSONObject) array.get(index);
if (object == null) {
throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
"[Json] Cannot get object at index " + index + " in array");
}
return new JsonWrapper(object);
} else {
throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
"[Json] Index is out of bound or array is null");
}
}
public void add(JSON val) {
this.array.add(val);
}
public JsonWrapperArray getArrayAt(int index) {
if (array != null && array.size() > index) {
JSONArray newArray = (JSONArray) array.get(index);
if (newArray == null) {
throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
"[Json] Cannot get array at index " + index + " in array");
}
return new JsonWrapperArray(newArray);
} else {
throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
"[Json] Index is out of bound or array is null");
}
}
private Object getObjectAt(int index) {
if (array != null && array.size() > index) {
return array.get(index);
} else {
throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
"[Json] Index is out of bound or array is null");
}
}
public long getLongAt(int index) {
try {
return (Long) getObjectAt(index);
} catch (Exception e) {
throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
"[Json] Cannot get long at index " + index + " in array: " + e.getMessage());
}
}
public Integer getIntegerAt(int index) {
try {
return (Integer) getObjectAt(index);
} catch (Exception e) {
throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
"[Json] Cannot get integer at index " + index + " in array: " + e.getMessage());
}
}
public BigDecimal getBigDecimalAt(int index) {
try {
return new BigDecimal(new BigDecimal(getStringAt(index)).stripTrailingZeros().toPlainString());
} catch (RuntimeException e) {
throw new ApexProApiException(null, e.getMessage());
}
}
public String getStringAt(int index) {
try {
return (String) getObjectAt(index);
} catch (RuntimeException e) {
throw new ApexProApiException(null, e.getMessage());
}
}
public void forEach(Handler objectHandler) {
array.forEach((object) -> {
if (!(object instanceof JSONObject)) {
throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR, "[Json] Parse array error in forEach");
}
objectHandler.handle(new JsonWrapper((JSONObject) object));
});
}
public void forEachAsArray(Handler objectHandler) {
array.forEach((object) -> {
if (!(object instanceof JSONArray)) {
throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
"[Json] Parse array error in forEachAsArray");
}
objectHandler.handle(new JsonWrapperArray((JSONArray) object));
});
}
public void forEachAsString(Handler objectHandler) {
array.forEach((object) -> {
if (!(object instanceof String)) {
throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
"[Json] Parse array error in forEachAsString");
}
objectHandler.handle((String) object);
});
}
public List convert2StringList() {
List result = new LinkedList<>();
this.forEachAsString((item) -> {
result.add(item);
});
return result;
}
}