Search in sources :

Example 56 with JsonPrimitive

use of com.google.gson.JsonPrimitive 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 57 with JsonPrimitive

use of com.google.gson.JsonPrimitive in project Engine by VoltzEngine-Project.

the class JsonConverterPos method fromJsonArray.

/**
     * Creates a pos from a json array containing x, y, z fields
     *
     * @param offsetObject
     * @return
     */
public static Pos fromJsonArray(JsonArray offsetObject) {
    double x = 0, y = 0, z = 0;
    JsonElement one = offsetObject.get(0);
    if (one.isJsonPrimitive()) {
        JsonPrimitive p = one.getAsJsonPrimitive();
        if (p.isNumber()) {
            x = one.getAsDouble();
        } else if (p.isString()) {
            //TODO parse
            throw new IllegalStateException("Loading json array using strings is not supported yet for pos conversion. Data: " + offsetObject);
        }
    }
    JsonElement two = offsetObject.get(1);
    if (two.isJsonPrimitive()) {
        JsonPrimitive p = two.getAsJsonPrimitive();
        if (p.isNumber()) {
            y = two.getAsDouble();
        } else if (p.isString()) {
            //TODO parse
            throw new IllegalStateException("Loading json array using strings is not supported yet for pos conversion. Data: " + offsetObject);
        }
    }
    JsonElement there = offsetObject.get(2);
    if (there.isJsonPrimitive()) {
        JsonPrimitive p = there.getAsJsonPrimitive();
        if (p.isNumber()) {
            x = there.getAsDouble();
        } else if (p.isString()) {
            //TODO parse
            throw new IllegalStateException("Loading json array using strings is not supported yet for pos conversion. Data: " + offsetObject);
        }
    }
    return new Pos(x, y, z);
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) Pos(com.builtbroken.mc.imp.transform.vector.Pos) JsonElement(com.google.gson.JsonElement)

Example 58 with JsonPrimitive

use of com.google.gson.JsonPrimitive in project Engine by VoltzEngine-Project.

the class JsonProcessorInjectionMap method handle.

public boolean handle(O objectToInjection, String keyValue, Object valueToInject, boolean override, String overrideType) {
    try {
        final String injectionKeyID = keyValue.toLowerCase();
        if (supports(injectionKeyID, override, overrideType)) {
            if (valueToInject instanceof JsonElement) {
                if (valueToInject instanceof JsonPrimitive) {
                    if (((JsonPrimitive) valueToInject).isBoolean()) {
                        Boolean bool = ((JsonPrimitive) valueToInject).getAsBoolean();
                        if (injectionFields.containsKey(injectionKeyID)) {
                            Field field = injectionFields.get(injectionKeyID);
                            try {
                                field.setAccessible(true);
                                field.setBoolean(objectToInjection, bool);
                                return true;
                            } catch (IllegalAccessException e) {
                                throw new RuntimeException("Failed to access field " + field, e);
                            } catch (Exception e) {
                                throw new RuntimeException("Unexpected error setting " + field + " with " + bool, e);
                            }
                        } else {
                            Method method = injectionMethods.get(injectionKeyID);
                            try {
                                method.setAccessible(true);
                                method.invoke(objectToInjection, bool);
                                return true;
                            } catch (InvocationTargetException e) {
                                throw new RuntimeException("Failed to invoke method " + method + " with data " + bool, e);
                            } catch (IllegalAccessException e) {
                                throw new RuntimeException("Failed to access method " + method, e);
                            } catch (Exception e) {
                                throw new RuntimeException("Unexpected error invoking " + method + " with " + bool, e);
                            }
                        }
                    } else if (((JsonPrimitive) valueToInject).isNumber()) {
                        if (injectionFields.containsKey(injectionKeyID)) {
                            Field field = injectionFields.get(injectionKeyID);
                            try {
                                field.setAccessible(true);
                                String type = injectionTypes.get(injectionKeyID);
                                if (type != null) {
                                    if (type.equals("int") || type.equals("integer")) {
                                        field.setInt(objectToInjection, ((JsonPrimitive) valueToInject).getAsInt());
                                    } else if (type.equals("byte")) {
                                        field.setByte(objectToInjection, ((JsonPrimitive) valueToInject).getAsByte());
                                    } else if (type.equals("short")) {
                                        field.setShort(objectToInjection, ((JsonPrimitive) valueToInject).getAsShort());
                                    } else if (type.equals("double")) {
                                        field.setDouble(objectToInjection, ((JsonPrimitive) valueToInject).getAsDouble());
                                    } else if (type.equals("float")) {
                                        field.setFloat(objectToInjection, ((JsonPrimitive) valueToInject).getAsFloat());
                                    } else if (type.equals("long")) {
                                        field.setLong(objectToInjection, ((JsonPrimitive) valueToInject).getAsLong());
                                    } else {
                                        throw new RuntimeException("Unknown number type for " + field);
                                    }
                                    return true;
                                } else {
                                    throw new RuntimeException("Failed to get number type for " + field);
                                }
                            } catch (IllegalAccessException e) {
                                throw new RuntimeException("Failed to access field " + field, e);
                            }
                        } else {
                            Method method = injectionMethods.get(injectionKeyID);
                            try {
                                method.setAccessible(true);
                                String type = injectionTypes.get(injectionKeyID);
                                if (type != null) {
                                    if (type.equals("int") || type.equals("integer")) {
                                        method.invoke(objectToInjection, (int) ((JsonPrimitive) valueToInject).getAsInt());
                                    } else if (type.equals("byte")) {
                                        method.invoke(objectToInjection, (byte) ((JsonPrimitive) valueToInject).getAsByte());
                                    } else if (type.equals("short")) {
                                        method.invoke(objectToInjection, (short) ((JsonPrimitive) valueToInject).getAsShort());
                                    } else if (type.equals("double")) {
                                        method.invoke(objectToInjection, (double) ((JsonPrimitive) valueToInject).getAsDouble());
                                    } else if (type.equals("float")) {
                                        method.invoke(objectToInjection, (float) ((JsonPrimitive) valueToInject).getAsFloat());
                                    } else if (type.equals("long")) {
                                        method.invoke(objectToInjection, (long) ((JsonPrimitive) valueToInject).getAsLong());
                                    } else {
                                        throw new RuntimeException("Unknown number type " + type);
                                    }
                                    return true;
                                } else {
                                    throw new RuntimeException("Failed to get type");
                                }
                            } catch (InvocationTargetException e) {
                                throw new RuntimeException("Failed to invoke method " + method + " with data " + valueToInject, e);
                            } catch (IllegalAccessException e) {
                                throw new RuntimeException("Failed to access method " + method, e);
                            } catch (Exception e) {
                                throw new RuntimeException("Error injecting " + valueToInject + " into method " + method, e);
                            }
                        }
                    } else if (((JsonPrimitive) valueToInject).isString()) {
                        String string = ((JsonPrimitive) valueToInject).getAsString();
                        return handle(objectToInjection, keyValue, string);
                    }
                } else {
                    if (injectionFields.containsKey(injectionKeyID)) {
                        Field field = injectionFields.get(injectionKeyID);
                        field.setAccessible(true);
                        try {
                            String type = injectionTypes.get(injectionKeyID);
                            if (type != null && JsonLoader.conversionHandlers.containsKey(type.toLowerCase())) {
                                JsonConverter converter = JsonLoader.conversionHandlers.get(type.toLowerCase());
                                if (converter != null) {
                                    Object conversion = converter.convert((JsonElement) valueToInject);
                                    if (conversion != null) {
                                        field.set(objectToInjection, conversion);
                                    } else {
                                        throw new IllegalArgumentException("Field was marked as type[" + type + "] but could not be converted to inject into " + field + ", data: " + objectToInjection);
                                    }
                                } else {
                                    throw new IllegalArgumentException("Field was marked as type[" + type + "] but a converter could not be found to use with " + field + ", data: " + objectToInjection);
                                }
                            } else {
                                field.set(objectToInjection, valueToInject);
                            }
                            return true;
                        } catch (IllegalAccessException e) {
                            throw new RuntimeException("Failed to access field " + field, e);
                        } catch (Exception e) {
                            throw new RuntimeException("Unexpected error setting " + field + " with " + valueToInject, e);
                        }
                    } else {
                        Method method = injectionMethods.get(injectionKeyID);
                        try {
                            method.setAccessible(true);
                            String type = injectionTypes.get(injectionKeyID);
                            if (type != null && JsonLoader.conversionHandlers.containsKey(type.toLowerCase())) {
                                JsonConverter converter = JsonLoader.conversionHandlers.get(type.toLowerCase());
                                if (converter != null) {
                                    Object conversion = converter.convert((JsonElement) valueToInject);
                                    if (conversion != null) {
                                        method.invoke(objectToInjection, conversion);
                                    } else {
                                        throw new IllegalArgumentException("Method was marked as type[" + type + "] but could not be converted to inject into " + method + ", data: " + objectToInjection);
                                    }
                                } else {
                                    throw new IllegalArgumentException("Method was marked as type[" + type + "] but a converter could not be found to use with " + method + ", data: " + objectToInjection);
                                }
                            } else {
                                method.invoke(objectToInjection, valueToInject);
                            }
                            return true;
                        } catch (InvocationTargetException e) {
                            throw new RuntimeException("Failed to invoke method " + method + " with data " + valueToInject, e);
                        } catch (IllegalAccessException e) {
                            throw new RuntimeException("Failed to access method " + method, e);
                        } catch (Exception e) {
                            throw new RuntimeException("Unexpected error invoking " + method + " with " + valueToInject, e);
                        }
                    }
                }
            } else if (valueToInject instanceof String) {
                if (injectionFields.containsKey(injectionKeyID)) {
                    Field field = injectionFields.get(injectionKeyID);
                    try {
                        field.setAccessible(true);
                        field.set(objectToInjection, valueToInject);
                        return true;
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException("Failed to access field " + field, e);
                    } catch (Exception e) {
                        throw new RuntimeException("Unexpected error setting " + field + " with " + valueToInject, e);
                    }
                } else {
                    Method method = injectionMethods.get(injectionKeyID);
                    try {
                        method.setAccessible(true);
                        method.invoke(objectToInjection, valueToInject);
                        return true;
                    } catch (InvocationTargetException e) {
                        throw new RuntimeException("Failed to invoke method " + method + " with data " + valueToInject, e);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException("Failed to access method " + method, e);
                    } catch (Exception e) {
                        throw new RuntimeException("Unexpected error invoking " + method + " with " + valueToInject, e);
                    }
                }
            } else if (injectionFields.containsKey(injectionKeyID)) {
                Field field = injectionFields.get(injectionKeyID);
                try {
                    field.setAccessible(true);
                    field.set(objectToInjection, valueToInject);
                    return true;
                } catch (IllegalAccessException e) {
                    throw new RuntimeException("Failed to access field " + field, e);
                } catch (Exception e) {
                    throw new RuntimeException("Unexpected error setting " + field + " with " + valueToInject, e);
                }
            } else {
                Method method = injectionMethods.get(injectionKeyID);
                try {
                    method.setAccessible(true);
                    method.invoke(objectToInjection, valueToInject);
                    return true;
                } catch (InvocationTargetException e) {
                    throw new RuntimeException("Failed to invoke method " + method + " with data " + valueToInject, e);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException("Failed to access method " + method, e);
                } catch (Exception e) {
                    throw new RuntimeException("Unexpected error invoking " + method + " with " + valueToInject, e);
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Failed to inject data " + valueToInject + " into " + objectToInjection, e);
    }
    return false;
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonConverter(com.builtbroken.mc.lib.json.conversion.JsonConverter) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) JsonElement(com.google.gson.JsonElement)

Example 59 with JsonPrimitive

use of com.google.gson.JsonPrimitive in project Engine by VoltzEngine-Project.

the class TestProcessorInjection method testJsonDouble.

@Test
public void testJsonDouble() {
    JsonProcessorInjectionMap map = new JsonProcessorInjectionMap(InjectionTestClass.class);
    InjectionTestClass object = new InjectionTestClass();
    assertTrue(map.handle(object, "d", new JsonPrimitive(1.1)));
    assertEquals(1.1, object.d);
    assertTrue(map.handle(object, "d2", new JsonPrimitive(2.2)));
    assertEquals(2.2, object.d);
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonProcessorInjectionMap(com.builtbroken.mc.lib.json.loading.JsonProcessorInjectionMap) Test(org.junit.Test)

Example 60 with JsonPrimitive

use of com.google.gson.JsonPrimitive in project Engine by VoltzEngine-Project.

the class TestProcessorInjection method testJsonFloat.

@Test
public void testJsonFloat() {
    JsonProcessorInjectionMap map = new JsonProcessorInjectionMap(InjectionTestClass.class);
    InjectionTestClass object = new InjectionTestClass();
    assertTrue(map.handle(object, "f", new JsonPrimitive(1f)));
    assertEquals(1f, object.f);
    assertTrue(map.handle(object, "f2", new JsonPrimitive(2f)));
    assertEquals(2f, object.f);
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonProcessorInjectionMap(com.builtbroken.mc.lib.json.loading.JsonProcessorInjectionMap) Test(org.junit.Test)

Aggregations

JsonPrimitive (com.google.gson.JsonPrimitive)230 JsonObject (com.google.gson.JsonObject)137 JsonArray (com.google.gson.JsonArray)118 JsonElement (com.google.gson.JsonElement)106 Test (org.testng.annotations.Test)56 Test (org.junit.Test)12 Map (java.util.Map)10 Matchers.anyString (org.mockito.Matchers.anyString)8 JsonProcessorInjectionMap (com.builtbroken.mc.lib.json.loading.JsonProcessorInjectionMap)7 Gson (com.google.gson.Gson)6 HashMap (java.util.HashMap)5 GsonBuilder (com.google.gson.GsonBuilder)4 JsonParser (com.google.gson.JsonParser)4 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 Type (java.lang.reflect.Type)3 HashSet (java.util.HashSet)3 Matcher (java.util.regex.Matcher)3 LobWrapper (angularBeans.io.LobWrapper)2 DatasetCreationSpec (co.cask.cdap.internal.dataset.DatasetCreationSpec)2