Search in sources :

Example 61 with JsonObject

use of org.apache.beam.vendor.grpc.v1p43p2.com.google.gson.JsonObject in project weave by continuuity.

the class WeaveRunnableSpecificationCodec method serialize.

@Override
public JsonElement serialize(WeaveRunnableSpecification src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject json = new JsonObject();
    json.addProperty("classname", src.getClassName());
    json.addProperty("name", src.getName());
    json.add("arguments", context.serialize(src.getConfigs(), new TypeToken<Map<String, String>>() {
    }.getType()));
    return json;
}
Also used : JsonObject(com.google.gson.JsonObject) Map(java.util.Map)

Example 62 with JsonObject

use of org.apache.beam.vendor.grpc.v1p43p2.com.google.gson.JsonObject in project weave by continuuity.

the class WeaveRunnableSpecificationCodec method deserialize.

@Override
public WeaveRunnableSpecification deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();
    String className = jsonObj.get("classname").getAsString();
    String name = jsonObj.get("name").getAsString();
    Map<String, String> arguments = context.deserialize(jsonObj.get("arguments"), new TypeToken<Map<String, String>>() {
    }.getType());
    return new DefaultWeaveRunnableSpecification(className, name, arguments);
}
Also used : TypeToken(com.google.common.reflect.TypeToken) JsonObject(com.google.gson.JsonObject) DefaultWeaveRunnableSpecification(com.continuuity.weave.internal.DefaultWeaveRunnableSpecification)

Example 63 with JsonObject

use of org.apache.beam.vendor.grpc.v1p43p2.com.google.gson.JsonObject in project openhab1-addons by openhab.

the class WithingsApiClient method call.

private JsonObject call(String signedUrl) throws IOException, MalformedURLException, WithingsConnectionException, UnsupportedEncodingException {
    HttpURLConnection httpURLConnection;
    httpURLConnection = (HttpURLConnection) new URL(signedUrl).openConnection();
    httpURLConnection.connect();
    int responseCode = httpURLConnection.getResponseCode();
    if (responseCode != HttpURLConnection.HTTP_OK) {
        throw new WithingsConnectionException("Illegal response code: " + responseCode);
    }
    Reader reader = null;
    try {
        InputStream inputStream = httpURLConnection.getInputStream();
        reader = new InputStreamReader(inputStream, "UTF-8");
        JsonObject jsonObject = (JsonObject) jsonParser.parse(reader);
        return jsonObject;
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ignored) {
            }
        }
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) URL(java.net.URL)

Example 64 with JsonObject

use of org.apache.beam.vendor.grpc.v1p43p2.com.google.gson.JsonObject in project Engine by VoltzEngine-Project.

the class ModelStateJsonProcessor method process.

@Override
public IRenderState process(JsonObject renderStateObject, String stateID, String globalRenderType, String subRenderType) {
    ModelState renderState;
    //Data
    String modelID = null;
    Pos offset = null;
    Pos scale = null;
    EulerAngle rotation = null;
    //Load model ID, child objects may or may not contain this
    if (renderStateObject.has("modelID")) {
        modelID = renderStateObject.get("modelID").getAsString();
    }
    //Loads position offset
    if (renderStateObject.has("offset")) {
        offset = JsonConverterPos.fromJson(renderStateObject.get("offset"));
        if (offset == null) {
            throw new IllegalArgumentException("Unknown value type for offset " + renderStateObject.get("offset"));
        }
    }
    //Loads scale value
    if (renderStateObject.has("scale")) {
        scale = JsonConverterPos.fromJson(renderStateObject.get("scale"));
        if (scale == null) {
            throw new IllegalArgumentException("Unknown value type for scale " + renderStateObject.get("scale"));
        }
    }
    //Loads rotations
    if (renderStateObject.has("rotation")) {
        JsonObject rotationObject = renderStateObject.get("rotation").getAsJsonObject();
        double yaw = 0;
        double pitch = 0;
        double roll = 0;
        if (rotationObject.has("yaw")) {
            yaw = rotationObject.getAsJsonPrimitive("yaw").getAsDouble();
        }
        if (rotationObject.has("pitch")) {
            pitch = rotationObject.getAsJsonPrimitive("pitch").getAsDouble();
        }
        if (rotationObject.has("roll")) {
            roll = rotationObject.getAsJsonPrimitive("roll").getAsDouble();
        }
        rotation = new EulerAngle(yaw, pitch, roll);
    }
    //Creates state object
    if (globalRenderType.equalsIgnoreCase("tile")) {
        renderState = new TileState(stateID, modelID, offset, scale, rotation);
    } else {
        renderState = new ModelState(stateID, modelID, offset, scale, rotation);
    }
    if (renderStateObject.has("renderOnlyParts")) {
        renderState.renderOnlyParts = renderStateObject.get("renderOnlyParts").getAsBoolean();
    }
    if (renderStateObject.has("renderParent")) {
        renderState.renderParent = renderStateObject.get("renderParent").getAsBoolean();
    }
    //Loads parts to render if all is not selected
    if (renderStateObject.has("parts")) {
        String parts = renderStateObject.get("parts").getAsString();
        if (!parts.equals("all")) {
            renderState.parts = parts.split(",");
        }
    }
    return renderState;
}
Also used : TileState(com.builtbroken.mc.client.json.render.tile.TileState) JsonConverterPos(com.builtbroken.mc.lib.json.conversion.JsonConverterPos) Pos(com.builtbroken.mc.imp.transform.vector.Pos) ModelState(com.builtbroken.mc.client.json.render.state.ModelState) JsonObject(com.google.gson.JsonObject) EulerAngle(com.builtbroken.mc.imp.transform.rotation.EulerAngle)

Example 65 with JsonObject

use of org.apache.beam.vendor.grpc.v1p43p2.com.google.gson.JsonObject 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)

Aggregations

JsonObject (com.google.gson.JsonObject)5111 JsonArray (com.google.gson.JsonArray)1162 JsonElement (com.google.gson.JsonElement)1084 JsonParser (com.google.gson.JsonParser)710 Test (org.junit.Test)491 IOException (java.io.IOException)471 JsonPrimitive (com.google.gson.JsonPrimitive)387 ArrayList (java.util.ArrayList)376 Gson (com.google.gson.Gson)369 HashMap (java.util.HashMap)324 Map (java.util.Map)269 Test (org.testng.annotations.Test)208 Test (org.junit.jupiter.api.Test)201 File (java.io.File)146 List (java.util.List)142 InputStreamReader (java.io.InputStreamReader)135 JsonParseException (com.google.gson.JsonParseException)121 GsonBuilder (com.google.gson.GsonBuilder)93 JsonSyntaxException (com.google.gson.JsonSyntaxException)88 Nonnull (javax.annotation.Nonnull)87