Search in sources :

Example 86 with JsonArray

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;
}
Also used : JsonArray(com.google.gson.JsonArray) Group(me.perrycate.groupmeutils.data.Group) JsonElement(com.google.gson.JsonElement)

Example 87 with JsonArray

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;
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) TileRenderData(com.builtbroken.mc.client.json.render.tile.TileRenderData) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) RenderData(com.builtbroken.mc.client.json.render.RenderData) TileRenderData(com.builtbroken.mc.client.json.render.tile.TileRenderData) JsonElement(com.google.gson.JsonElement) IRenderState(com.builtbroken.mc.client.json.imp.IRenderState) HashMap(java.util.HashMap) Map(java.util.Map)

Example 88 with JsonArray

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);
            }
        }
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) ITileEventListener(com.builtbroken.mc.api.tile.listeners.ITileEventListener) JsonObject(com.google.gson.JsonObject) JsonProcessorInjectionMap(com.builtbroken.mc.lib.json.loading.JsonProcessorInjectionMap) HashMap(java.util.HashMap) JsonProcessorInjectionMap(com.builtbroken.mc.lib.json.loading.JsonProcessorInjectionMap) Map(java.util.Map)

Example 89 with JsonArray

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());
        }
    }
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) Map(java.util.Map)

Example 90 with JsonArray

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();
}
Also used : Utils.getJsonArray(org.stepik.api.Utils.getJsonArray) JsonArray(com.google.gson.JsonArray) ArrayList(java.util.ArrayList) Utils.getString(org.stepik.api.Utils.getString) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

JsonArray (com.google.gson.JsonArray)424 JsonObject (com.google.gson.JsonObject)290 JsonElement (com.google.gson.JsonElement)167 JsonPrimitive (com.google.gson.JsonPrimitive)118 Test (org.testng.annotations.Test)59 JsonParser (com.google.gson.JsonParser)57 ArrayList (java.util.ArrayList)50 HashMap (java.util.HashMap)34 Map (java.util.Map)34 Gson (com.google.gson.Gson)22 IOException (java.io.IOException)17 Test (org.junit.Test)16 Type (java.lang.reflect.Type)12 List (java.util.List)11 Matchers.anyString (org.mockito.Matchers.anyString)10 TextView (android.widget.TextView)9 Point (android.graphics.Point)8 JsonParseException (com.google.gson.JsonParseException)7 GsonUtilities.jboolean (com.ibm.streamsx.topology.internal.gson.GsonUtilities.jboolean)7 KcaUtils.getStringFromException (com.antest1.kcanotify.KcaUtils.getStringFromException)6