Search in sources :

Example 46 with JsonArray

use of com.google.gson.JsonArray in project PneumaticCraft by MineMaarten.

the class ProgWidgetConfig method readFromJson.

@Override
protected void readFromJson(JsonObject json) {
    JsonArray array = json.get("blacklist").getAsJsonArray();
    blacklistedPieces.clear();
    for (JsonElement element : array) {
        blacklistedPieces.add(element.getAsString());
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement)

Example 47 with JsonArray

use of com.google.gson.JsonArray in project PneumaticCraft by MineMaarten.

the class JsonToNBTConverter method getTag.

private NBTTagCompound getTag(JsonObject object) {
    NBTTagCompound nbt = new NBTTagCompound();
    for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
        JsonObject keyObject = entry.getValue().getAsJsonObject();
        int type = keyObject.get("type").getAsInt();
        JsonElement element = keyObject.get("value");
        switch(type) {
            case 1:
                nbt.setByte(entry.getKey(), (byte) element.getAsDouble());
                break;
            case 2:
                nbt.setShort(entry.getKey(), (short) element.getAsDouble());
            case 3:
                nbt.setInteger(entry.getKey(), (int) element.getAsDouble());
                break;
            case 4:
                nbt.setLong(entry.getKey(), (long) element.getAsDouble());
                break;
            case 5:
                nbt.setFloat(entry.getKey(), (float) element.getAsDouble());
                break;
            case 6:
                nbt.setDouble(entry.getKey(), element.getAsDouble());
                break;
            //   break;
            case 8:
                nbt.setString(entry.getKey(), element.getAsString());
                break;
            case 9:
                JsonArray array = element.getAsJsonArray();
                NBTTagList tagList = new NBTTagList();
                for (JsonElement e : array) {
                    tagList.appendTag(getTag(e.getAsJsonObject()));
                }
                nbt.setTag(entry.getKey(), tagList);
                break;
            case 10:
                nbt.setTag(entry.getKey(), getTag(element.getAsJsonObject()));
                break;
            case 11:
                array = element.getAsJsonArray();
                int[] intArray = new int[array.size()];
                for (int i = 0; i < array.size(); i++) {
                    intArray[i] = array.get(i).getAsInt();
                }
                nbt.setTag(entry.getKey(), new NBTTagIntArray(intArray));
                break;
            default:
                throw new IllegalArgumentException("NBT type no " + type + " is not supported by the Json to NBT converter!");
        }
    }
    return nbt;
}
Also used : JsonArray(com.google.gson.JsonArray) NBTTagList(net.minecraft.nbt.NBTTagList) JsonElement(com.google.gson.JsonElement) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) JsonObject(com.google.gson.JsonObject) Map(java.util.Map) NBTTagIntArray(net.minecraft.nbt.NBTTagIntArray)

Example 48 with JsonArray

use of com.google.gson.JsonArray in project PneumaticCraft by MineMaarten.

the class NBTToJsonConverter method getObject.

private JsonObject getObject(NBTTagCompound tag) {
    Set<String> keys = tag.func_150296_c();
    JsonObject jsonRoot = new JsonObject();
    for (String key : keys) {
        JsonObject keyObject = new JsonObject();
        jsonRoot.add(key, keyObject);
        NBTBase nbt = tag.getTag(key);
        keyObject.addProperty("type", nbt.getId());
        if (nbt instanceof NBTTagCompound) {
            keyObject.add("value", getObject((NBTTagCompound) nbt));
        } else if (nbt instanceof NBTPrimitive) {
            keyObject.addProperty("value", ((NBTPrimitive) nbt).func_150286_g());
        } else if (nbt instanceof NBTTagString) {
            keyObject.addProperty("value", ((NBTTagString) nbt).func_150285_a_());
        } else if (nbt instanceof NBTTagList) {
            JsonArray array = new JsonArray();
            NBTTagList tagList = (NBTTagList) nbt;
            for (int i = 0; i < tagList.tagCount(); i++) {
                array.add(getObject(tagList.getCompoundTagAt(i)));
            }
            keyObject.add("value", array);
        } else if (nbt instanceof NBTTagIntArray) {
            JsonArray array = new JsonArray();
            NBTTagIntArray intArray = (NBTTagIntArray) nbt;
            for (int i : intArray.func_150302_c()) {
                array.add(new JsonPrimitive(i));
            }
            keyObject.add("value", array);
        } else {
            throw new IllegalArgumentException("NBT to JSON converter doesn't support the nbt tag: " + NBTBase.NBTTypes[nbt.getId()] + ", tag: " + nbt);
        }
    }
    return jsonRoot;
}
Also used : NBTPrimitive(net.minecraft.nbt.NBTBase.NBTPrimitive) JsonPrimitive(com.google.gson.JsonPrimitive) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) JsonObject(com.google.gson.JsonObject) NBTTagString(net.minecraft.nbt.NBTTagString) NBTTagIntArray(net.minecraft.nbt.NBTTagIntArray) NBTTagList(net.minecraft.nbt.NBTTagList) JsonArray(com.google.gson.JsonArray) NBTBase(net.minecraft.nbt.NBTBase) NBTTagString(net.minecraft.nbt.NBTTagString)

Example 49 with JsonArray

use of com.google.gson.JsonArray in project MinecraftForge by MinecraftForge.

the class MetadataCollection method from.

public static MetadataCollection from(@Nullable InputStream inputStream, String sourceName) {
    if (inputStream == null) {
        return new MetadataCollection();
    }
    InputStreamReader reader = new InputStreamReader(inputStream);
    try {
        MetadataCollection collection;
        Gson gson = new GsonBuilder().registerTypeAdapter(ArtifactVersion.class, new ArtifactVersionAdapter()).create();
        JsonParser parser = new JsonParser();
        JsonElement rootElement = parser.parse(reader);
        if (rootElement.isJsonArray()) {
            collection = new MetadataCollection();
            JsonArray jsonList = rootElement.getAsJsonArray();
            collection.modList = new ModMetadata[jsonList.size()];
            int i = 0;
            for (JsonElement mod : jsonList) {
                collection.modList[i++] = gson.fromJson(mod, ModMetadata.class);
            }
        } else {
            collection = gson.fromJson(rootElement, MetadataCollection.class);
        }
        collection.parseModMetadataList();
        return collection;
    } catch (JsonParseException e) {
        FMLLog.log(Level.ERROR, e, "The mcmod.info file in %s cannot be parsed as valid JSON. It will be ignored", sourceName);
        return new MetadataCollection();
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) JsonParseException(com.google.gson.JsonParseException) IOException(java.io.IOException) JsonArray(com.google.gson.JsonArray) ArtifactVersion(net.minecraftforge.fml.common.versioning.ArtifactVersion) JsonElement(com.google.gson.JsonElement) JsonParser(com.google.gson.JsonParser)

Example 50 with JsonArray

use of com.google.gson.JsonArray in project MinecraftForge by MinecraftForge.

the class FMLNetworkHandler method enhanceStatusQuery.

public static void enhanceStatusQuery(JsonObject jsonobject) {
    JsonObject fmlData = new JsonObject();
    fmlData.addProperty("type", "FML");
    JsonArray modList = new JsonArray();
    for (ModContainer mc : Loader.instance().getActiveModList()) {
        JsonObject modData = new JsonObject();
        modData.addProperty("modid", mc.getModId());
        modData.addProperty("version", mc.getVersion());
        modList.add(modData);
    }
    fmlData.add("modList", modList);
    jsonobject.add("modinfo", fmlData);
}
Also used : JsonArray(com.google.gson.JsonArray) ModContainer(net.minecraftforge.fml.common.ModContainer) JsonObject(com.google.gson.JsonObject)

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