Example usage for com.google.gson JsonObject getAsJsonPrimitive

List of usage examples for com.google.gson JsonObject getAsJsonPrimitive

Introduction

In this page you can find the example usage for com.google.gson JsonObject getAsJsonPrimitive.

Prototype

public JsonPrimitive getAsJsonPrimitive(String memberName) 

Source Link

Document

Convenience method to get the specified member as a JsonPrimitive element.

Usage

From source file:net.lm.seriesfreak.database.data.properties.IntegerProperty.java

License:Open Source License

@Override
public void deserialize(JsonObject object) {
    this.value = object.getAsJsonPrimitive("Value").getAsInt();
}

From source file:net.lm.seriesfreak.database.data.properties.StringProperty.java

License:Open Source License

@Override
public void deserialize(JsonObject object) {
    this.value = object.getAsJsonPrimitive("Value").getAsString();
}

From source file:net.minecrell.serverlistplus.core.favicon.FaviconHelper.java

License:Open Source License

public static BufferedImage fromSkin(ServerListPlusCore core, UUID uuid, boolean helm) throws IOException {
    JsonObject obj;

    try (BufferedReader reader = new BufferedReader(new InputStreamReader(
            openConnection(core, new URL(SKIN_UUID_URL + toHexString(uuid)), "application/json")))) {
        obj = Helper.JSON.fromJson(reader, JsonObject.class);
    }//from w  ww  .  j  av  a 2  s  . com

    JsonArray arr = obj.getAsJsonArray("properties");
    for (JsonElement ele : arr) {
        if (ele instanceof JsonObject) {
            obj = (JsonObject) ele;
            if (!obj.getAsJsonPrimitive("name").getAsString().equals("textures"))
                continue;
            String json = new String(
                    BaseEncoding.base64().decode(obj.getAsJsonPrimitive("value").getAsString()),
                    StandardCharsets.UTF_8);
            obj = Helper.JSON.fromJson(json, JsonObject.class).getAsJsonObject("textures")
                    .getAsJsonObject("SKIN");
            return fromSkin(core, new URL(obj.getAsJsonPrimitive("url").getAsString()), helm);
        }
    }

    throw new IllegalStateException("Skin not found");
}

From source file:net.rainbowcode.jpixelface.profile.ProfileManager.java

public static Profile getProfileFromUUID(UUID uuid) throws MojangException, IOException {
    String key = RedisKey.PROFILE_UUID.buildKey(uuid.toString());
    if (RedisUtils.exists(key)) {
        return new Profile(RedisUtils.getAsJson(key));
    } else {//from  w w  w .  j a v a2  s .  c o m
        String url = "https://sessionserver.mojang.com/session/minecraft/profile/"
                + uuid.toString().replaceAll("-", "");

        getTicket();

        HttpStringResponse response = HttpUtil.getAsString(url);

        String string = response.getResponse();
        if (response.getCode() != 200) {
            throw new MojangException(url, response.getCode());
        } else {
            JsonParser parser = new JsonParser();
            JsonObject object = parser.parse(string).getAsJsonObject();
            JsonArray properties = object.getAsJsonArray("properties");
            String name = object.getAsJsonPrimitive("name").getAsString();
            JsonObject textures = properties.get(0).getAsJsonObject();
            String value = textures.get("value").getAsString();
            String decoded = new String(Base64.getDecoder().decode(value), "UTF-8");
            JsonObject parse = parser.parse(decoded).getAsJsonObject();
            JsonObject texturesOb = parse.getAsJsonObject("textures");
            final String[] skinUrl = { null };
            final String[] capeUrl = { null };
            if (texturesOb != null) {
                resolve(() -> texturesOb.getAsJsonObject("SKIN").getAsJsonPrimitive("url").getAsString())
                        .ifPresent(x -> skinUrl[0] = x);
                resolve(() -> texturesOb.getAsJsonObject("CAPE").getAsJsonPrimitive("url").getAsString())
                        .ifPresent(x -> capeUrl[0] = x);
            }
            Profile profile = new Profile(name, uuid, skinUrl[0], capeUrl[0]);
            commitProfile(profile);
            return profile;

        }

    }
}

From source file:net.rainbowcode.jpixelface.profile.ProfileManager.java

private static UUID uuidFromName(String name) throws MojangException, IOException, JsonParseException {
    name = name.toLowerCase();//ww  w .  ja va 2s.  c  o m
    String key = RedisKey.UUID_NAME.buildKey(name);
    if (RedisUtils.exists(key)) {
        return UUID.fromString(RedisUtils.getAsString(key));
    } else {
        getTicket();
        String path = "https://api.mojang.com/users/profiles/minecraft/" + name;
        HttpStringResponse response = HttpUtil.getAsString(path);
        String string = response.getResponse();
        JsonParser parser = new JsonParser();
        if (response.getCode() != 200) {
            throw new MojangException(path, response.getCode());
        } else {
            JsonElement parse = parser.parse(string);
            JsonObject object = parse.getAsJsonObject();
            UUID uuid = UUID.fromString(StringUtil.addDashes(object.getAsJsonPrimitive("id").getAsString()));
            RedisUtils.setAndExpire(key, uuid.toString(), 86400);
            return uuid;
        }
    }
}

From source file:nl.ru.ai.projects.parrot.tools.TwitterAccess.java

License:Open Source License

/**
 * Processes the JSON response from uploading the image
 * @param input The JSON object that needs to be processed
 *//*from  w  w w.ja v a2s . c  o  m*/
private String processJSON(String input) {
    String returnString = "";
    JsonObject json = new JsonParser().parse(input).getAsJsonObject().getAsJsonObject("upload")
            .getAsJsonObject("links");
    if (json != null)
        returnString = json.getAsJsonPrimitive("imgur_page").getAsString();

    return returnString;
}

From source file:nl.tue.gale.um.data.EntityValue.java

License:Open Source License

@Override
public EntityValue deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    EntityValue ev = new EntityValue();
    JsonObject obj = (JsonObject) json;
    try {/*  w  ww .j a  v a 2 s. c  o  m*/
        ev.setUriString(obj.getAsJsonPrimitive("uri").getAsString());
        ev.setValueBytes(Base64.decode(obj.getAsJsonPrimitive("value").getAsString()));
    } catch (Exception e) {
        throw new JsonParseException("unable to deserialize EntityValue: " + json.toString(), e);
    }
    return ev;
}

From source file:org.ambraproject.service.crossref.CrossRefLookupServiceImpl.java

License:Apache License

/**
 * Parse the JSON into native types//w w  w.ja  v  a 2  s.c o m
 *
 * @param json the JSON string to convert to a java native type
 *
 * @return a CrossRefResponse object
 */
private CrossRefResponse parseJSON(final String json) {
    return new CrossRefResponse() {
        {
            JsonParser parser = new JsonParser();
            JsonObject responseObject = parser.parse(json).getAsJsonObject();

            queryOK = (responseObject.getAsJsonPrimitive("query_ok")).getAsBoolean();

            List<CrossRefResult> resultTemp = new ArrayList<CrossRefResult>();

            for (final JsonElement resultElement : responseObject.getAsJsonArray("results")) {
                JsonObject resultObj = resultElement.getAsJsonObject();
                CrossRefResult res = new CrossRefResult();

                if (resultObj.getAsJsonPrimitive("text") != null) {
                    res.text = resultObj.getAsJsonPrimitive("text").getAsString();
                }

                if (resultObj.getAsJsonPrimitive("match") != null) {
                    res.match = resultObj.getAsJsonPrimitive("match").getAsBoolean();
                }

                if (resultObj.getAsJsonPrimitive("doi") != null) {
                    res.doi = resultObj.getAsJsonPrimitive("doi").getAsString();
                }

                if (resultObj.getAsJsonPrimitive("score") != null) {
                    res.score = resultObj.getAsJsonPrimitive("score").getAsString();
                }

                //Some results aren't actually valid
                if (res.doi != null) {
                    resultTemp.add(res);
                }
            }

            this.results = resultTemp.toArray(new CrossRefResult[resultTemp.size()]);
        }
    };
}

From source file:org.apache.abdera2.activities.io.gson.BaseAdapter.java

License:Apache License

public ASBase deserialize(JsonElement el, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject obj = (JsonObject) el;
    ASBase.Builder<?, ?> builder;/*from ww w  .ja  va  2  s.  c  o  m*/
    if (type == Collection.class)
        builder = Collection.makeCollection();
    else if (type == Activity.class)
        builder = Activity.makeActivity();
    else if (type == MediaLink.class)
        builder = MediaLink.makeMediaLink();
    else if (type == PlaceObject.class)
        builder = PlaceObject.makePlace();
    else if (type == Mood.class)
        builder = Mood.makeMood();
    else if (type == Address.class)
        builder = Address.makeAddress();
    else {
        JsonPrimitive ot = obj.getAsJsonPrimitive("objectType");
        if (ot != null) {
            String ots = ot.getAsString();
            Class<? extends ASObject.Builder> _class = objsmap.get(ots);
            if (_class != null) {
                builder = Discover.locate(_class, _class.getName());
                try {
                    builder = _class.getConstructor(String.class).newInstance(ots);
                } catch (Throwable t) {
                }

            } else
                builder = ASObject.makeObject(ots);
        } else {
            if (obj.has("verb") && (obj.has("actor") || obj.has("object") || obj.has("target"))) {
                builder = Activity.makeActivity();
            } else if (obj.has("items")) {
                builder = Collection.makeCollection();
            } else {
                builder = ASObject.makeObject(); // anonymous
            }
        }
    }
    for (Entry<String, JsonElement> entry : obj.entrySet()) {
        String name = entry.getKey();
        if (name.equalsIgnoreCase("objectType"))
            continue;
        Class<?> _class = map.get(name);
        JsonElement val = entry.getValue();
        if (val.isJsonPrimitive()) {
            if (_class != null) {
                builder.set(name, context.deserialize(val, _class));
            } else {
                JsonPrimitive prim = val.getAsJsonPrimitive();
                if (prim.isBoolean())
                    builder.set(name, prim.getAsBoolean());
                else if (prim.isNumber())
                    builder.set(name, prim.getAsNumber());
                else {
                    builder.set(name, prim.getAsString());
                }
            }
        } else if (val.isJsonArray()) {
            ImmutableList.Builder<Object> list = ImmutableList.builder();
            JsonArray arr = val.getAsJsonArray();
            processArray(arr, _class, context, list);
            builder.set(name, list.build());
        } else if (val.isJsonObject()) {
            if (map.containsKey(name)) {
                builder.set(name, context.deserialize(val, map.get(name)));
            } else
                builder.set(name, context.deserialize(val, ASObject.class));
        }
    }
    return builder.get();
}

From source file:org.apache.airavata.workflow.model.component.ws.WSComponentApplication.java

License:Apache License

public static WSComponentApplication parse(JsonObject applicationObject) {
    WSComponentApplication wsComponentApplication = new WSComponentApplication();
    wsComponentApplication.description = applicationObject
            .getAsJsonPrimitive(WorkflowConstants.APPLICATION_COMPONENT_DESCRIPTION).getAsString();
    wsComponentApplication.name = applicationObject
            .getAsJsonPrimitive(WorkflowConstants.APPLICATION_COMPONENT_NAME).getAsString();
    wsComponentApplication.applicationId = applicationObject
            .getAsJsonPrimitive(WorkflowConstants.APPLICATION_COMPONENT_APPLICATION).getAsString();

    if (applicationObject.get(WorkflowConstants.APPLICATION_INPUT) != null) {
        JsonArray inputArray = applicationObject.getAsJsonArray(WorkflowConstants.APPLICATION_INPUT);
        WSComponentApplicationParameter inputParameter;
        JsonObject inputObject;//from   www  .ja  va2 s .com
        for (JsonElement jsonElement : inputArray) {
            if (jsonElement instanceof JsonObject) {
                inputObject = (JsonObject) jsonElement;
                inputParameter = new WSComponentApplicationParameter();
                inputParameter.setDefaultValue(inputObject
                        .getAsJsonPrimitive(WorkflowConstants.APPLICATION_DATA_DEFAULT_VALUE).getAsString());
                inputParameter.setDescription(inputObject
                        .getAsJsonPrimitive(WorkflowConstants.APPLICATION_DATA_DESCRIPTION).getAsString());
                inputParameter.setName(
                        inputObject.getAsJsonPrimitive(WorkflowConstants.APPLICATION_DATA_NAME).getAsString());
                inputParameter.setType(DataType.valueOf(inputObject
                        .getAsJsonPrimitive(WorkflowConstants.APPLICATION_DATA_DATA_TYPE).getAsString()));
                inputParameter.setInputOrder(inputObject
                        .getAsJsonPrimitive(WorkflowConstants.APPLICATION_DATA_INPUT_ORDER).getAsInt());
                if (inputObject.getAsJsonPrimitive(WorkflowConstants.APPLICATION_DATA_APP_ARGUMENT) != null) {
                    inputParameter.setApplicationArgument(inputObject
                            .getAsJsonPrimitive(WorkflowConstants.APPLICATION_DATA_APP_ARGUMENT).getAsString());
                }
                wsComponentApplication.addInputParameter(inputParameter);
            }
        }
    }

    if (applicationObject.get(WorkflowConstants.APPLICATION_OUTPUT) != null) {
        JsonArray outputArray = applicationObject.getAsJsonArray(WorkflowConstants.APPLICATION_OUTPUT);
        WSComponentApplicationParameter outputParameter;
        JsonObject outputObject;
        for (JsonElement jsonElement : outputArray) {
            if (jsonElement instanceof JsonObject) {
                outputObject = (JsonObject) jsonElement;
                outputParameter = new WSComponentApplicationParameter();
                outputParameter.setDescription(outputObject
                        .getAsJsonPrimitive(WorkflowConstants.APPLICATION_DATA_DESCRIPTION).getAsString());
                outputParameter.setName(
                        outputObject.getAsJsonPrimitive(WorkflowConstants.APPLICATION_DATA_NAME).getAsString());
                outputParameter.setType(DataType.valueOf(outputObject
                        .getAsJsonPrimitive(WorkflowConstants.APPLICATION_DATA_DATA_TYPE).getAsString()));
                wsComponentApplication.addOutputParameter(outputParameter);
            }
        }
    }

    return wsComponentApplication;
}