use of com.google.gson.JsonArray in project groupme-utils by TheGuyWithTheFace.
the class GroupArrayDeserializer method deserialize.
public Group[] deserialize(JsonElement jsonElement, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonArray response = getResponse(jsonElement).getAsJsonArray();
// Deserialize one group at a time, add to new groups array.
int numGroups = response.size();
Group[] groups = new Group[numGroups];
for (int i = 0; i < numGroups; i++) {
JsonElement elem = response.get(i);
groups[i] = context.deserialize(elem, Group.class);
}
return groups;
}
use of com.google.gson.JsonArray in project Engine by VoltzEngine-Project.
the class RenderJsonProcessor method process.
@Override
public RenderData process(JsonElement element) {
final JsonObject object = element.getAsJsonObject();
ensureValuesExist(object, "contentID", "states", "type");
String contentID = object.get("contentID").getAsString();
String overAllRenderType = object.get("type").getAsString();
RenderData data;
if (overAllRenderType.equalsIgnoreCase("tile")) {
data = new TileRenderData(this, contentID, overAllRenderType);
if (object.has("tileClass")) {
try {
((TileRenderData) data).tileClass = (Class<? extends TileEntity>) Class.forName(object.get("tileClass").getAsString());
} catch (Exception e) {
throw new IllegalArgumentException("Failed to load class for name '" + object.get("tileClass").getAsString() + "'");
}
}
} else {
data = new RenderData(this, contentID, overAllRenderType);
}
JsonArray array = object.get("states").getAsJsonArray();
for (JsonElement arrayElement : array) {
if (arrayElement instanceof JsonObject) {
JsonObject renderStateObject = arrayElement.getAsJsonObject();
//For loop handling for the lazy
if (renderStateObject.has("for")) {
renderStateObject = renderStateObject.getAsJsonObject("for");
ensureValuesExist(renderStateObject, "start", "end", "state");
int start = renderStateObject.getAsJsonPrimitive("start").getAsInt();
int end = renderStateObject.getAsJsonPrimitive("end").getAsInt();
if (start >= end) {
throw new IllegalArgumentException("Start can not be greater than or equal to end for a for loop.");
}
JsonObject template = renderStateObject.getAsJsonObject("state");
for (int i = start; i <= end; i++) {
JsonObject state = new JsonObject();
//Copy template and rename values as needed
for (Map.Entry<String, JsonElement> entry : template.entrySet()) {
if (entry.getValue() instanceof JsonPrimitive && ((JsonPrimitive) entry.getValue()).isString()) {
String s = entry.getValue().getAsString();
s = s.replace("%number%", "" + i);
state.add(entry.getKey(), new JsonPrimitive(s));
} else {
state.add(entry.getKey(), entry.getValue());
}
}
//Load state
handle(state, data, overAllRenderType);
}
} else {
handle(renderStateObject, data, overAllRenderType);
}
}
}
//Handle post calls
for (IRenderState state : data.renderStatesByName.values()) {
//Handles post processor actions
if (stateToProcessor.containsKey(state)) {
stateToProcessor.get(state).postHandle(state, data);
}
}
//Clear run data
stateToProcessor.clear();
//TODO ensure modelID exists if model state
return data;
}
use of com.google.gson.JsonArray in project Engine by VoltzEngine-Project.
the class JsonBlockListenerProcessor method process.
@Override
public void process(BlockBase block, JsonElement arrayElement, List<IJsonGenObject> objectList) {
JsonArray array = arrayElement.getAsJsonArray();
for (JsonElement element : array) {
String key = null;
JsonObject data = null;
if (element.isJsonPrimitive()) {
key = element.getAsString().toLowerCase();
} else if (element.isJsonObject()) {
JsonObject object = element.getAsJsonObject();
JsonProcessor.ensureValuesExist(object, "id");
key = object.get("id").getAsString().toLowerCase();
data = object;
}
if (builders.containsKey(key)) {
ITileEventListener listener = builders.get(key).createListener(block);
if (listener != null) {
if (data != null) {
if (!injectionMaps.containsKey(listener.getClass())) {
injectionMaps.put(listener.getClass(), new JsonProcessorInjectionMap(listener.getClass()));
}
JsonProcessorInjectionMap injectionMap = injectionMaps.get(listener.getClass());
for (Map.Entry<String, JsonElement> entry : data.entrySet()) {
injectionMap.handle(listener, entry.getKey(), entry.getValue());
}
}
block.addListener(listener);
}
}
}
}
use of com.google.gson.JsonArray in project Engine by VoltzEngine-Project.
the class JsonConverterNBT method handle.
protected void handle(JsonObject object, NBTTagCompound nbt, int depth) {
for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
if (entry.getValue() instanceof JsonObject) {
NBTTagCompound tag = new NBTTagCompound();
//TODO add depth limit
handle((JsonObject) entry.getValue(), tag, depth++);
nbt.setTag(entry.getKey(), tag);
} else if (entry.getValue() instanceof JsonPrimitive) {
JsonPrimitive primitive = (JsonPrimitive) entry.getValue();
if (primitive.isNumber()) {
String[] split = entry.getKey().split(":");
String key = split[0];
String type = split[1].toLowerCase();
if (type.equals("int") || type.equals("integer")) {
nbt.setInteger(key, primitive.getAsInt());
} else if (type.equals("byte")) {
nbt.setByte(key, primitive.getAsByte());
} else if (type.equals("short")) {
nbt.setShort(key, primitive.getAsShort());
} else if (type.equals("double")) {
nbt.setDouble(key, primitive.getAsDouble());
} else if (type.equals("float")) {
nbt.setFloat(key, primitive.getAsFloat());
} else if (type.equals("long")) {
nbt.setLong(key, primitive.getAsLong());
} else {
throw new RuntimeException("Unknown number type for " + type + " while reading " + object);
}
} else if (primitive.isBoolean()) {
nbt.setBoolean(entry.getKey(), primitive.getAsBoolean());
} else if (primitive.isString()) {
nbt.setString(entry.getKey(), primitive.getAsString());
}
} else if (entry.getValue() instanceof JsonArray) {
JsonArray array = (JsonArray) entry.getValue();
if (array.size() > 0) {
JsonElement element = array.get(0);
if (element instanceof JsonPrimitive) {
String[] split = entry.getKey().split(":");
String key = split[0];
String type = split[1].toLowerCase();
if (type.equals("int") || type.equals("integer")) {
int[] ar = new int[array.size()];
for (int i = 0; i < array.size(); i++) {
JsonPrimitive p = array.get(i).getAsJsonPrimitive();
ar[i] = p.getAsInt();
}
nbt.setIntArray(key, ar);
} else if (type.equals("byte")) {
byte[] ar = new byte[array.size()];
for (int i = 0; i < array.size(); i++) {
JsonPrimitive p = array.get(i).getAsJsonPrimitive();
ar[i] = p.getAsByte();
}
nbt.setByteArray(key, ar);
} else {
throw new RuntimeException("Unsupported type of " + type + " for array read");
}
} else if (element instanceof JsonObject) {
NBTTagList list = new NBTTagList();
for (int i = 0; i < array.size(); i++) {
NBTTagCompound tag = new NBTTagCompound();
handle((JsonObject) array.get(i), tag, depth++);
list.appendTag(tag);
}
nbt.setTag(entry.getKey(), list);
}
}
} else {
throw new RuntimeException("Unknown type to convert to NBT -> " + entry.getValue());
}
}
}
use of com.google.gson.JsonArray in project intellij-plugins by StepicOrg.
the class DatasetDeserializer method getStringList.
@NotNull
private static List<String> getStringList(@NotNull JsonObject object, @NotNull String memberName) {
JsonArray jsonArray = getJsonArray(object, memberName);
if (jsonArray != null) {
List<String> array = new ArrayList<>();
jsonArray.forEach(element -> array.add(element.getAsString()));
return array;
}
return Collections.emptyList();
}
Aggregations