Example usage for com.google.gson JsonObject get

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

Introduction

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

Prototype

public JsonElement get(String memberName) 

Source Link

Document

Returns the member with the specified name.

Usage

From source file:cc.alessandro.jpocket.gson.AccessTokenDeserializer.java

License:Open Source License

@Override
public AccessToken deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = (JsonObject) json;
    return new AccessToken(jsonObject.get("access_token").getAsString(),
            jsonObject.get("username").getAsString());
}

From source file:cc.alessandro.jpocket.gson.StatusDeserializer.java

License:Open Source License

@Override
public Status deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    JsonObject jsonObject = (JsonObject) json;

    StatusJSON status = new StatusJSON();
    status.setId(jsonObject.get("item_id").getAsLong());
    status.setResolvedId(jsonObject.get("resolved_id").getAsLong());
    status.setGivenUrl(jsonObject.get("given_url").getAsString());
    status.setTitle(jsonObject.get("given_title").getAsString());
    status.setFavorite(jsonObject.get("favorite").getAsInt() == 1);
    status.setStatus(State.valueOf(jsonObject.get("status").getAsInt()));
    status.setExcerpt(jsonObject.get("excerpt").getAsString());
    status.setArticle(jsonObject.get("is_article").getAsInt() == 1);
    status.setWordCount(jsonObject.get("word_count").getAsLong());

    //TODO images, videos and tags

    return status;
}

From source file:cc.twittertools.corpus.data.Status.java

License:Apache License

public static Status fromJson(String json) {
    JsonObject obj = null;
    try {// ww w  .  j av  a 2  s  .  c om
        obj = (JsonObject) JSON_PARSER.parse(json);
    } catch (Exception e) {
        // Catch any malformed JSON.
        LOG.error("Error parsing: " + json);
        return null;
    }

    if (obj.get("text") == null) {
        return null;
    }

    Status status = new Status();
    status.text = obj.get("text").getAsString();
    status.id = obj.get("id").getAsLong();
    status.screenname = obj.get("user").getAsJsonObject().get("screen_name").getAsString();
    status.createdAt = obj.get("created_at").getAsString();

    try {
        status.epoch = (new SimpleDateFormat(DATE_FORMAT)).parse(status.createdAt).getTime() / 1000;
    } catch (ParseException e) {
        status.epoch = -1L;
    }

    // TODO: trying to fetch fields and then catching exceptions is bad practice, fix!
    try {
        status.inReplyToStatusId = obj.get("in_reply_to_status_id").getAsLong();
    } catch (Exception e) {
        status.inReplyToStatusId = -1L;
    }

    try {
        status.inReplyToUserId = obj.get("in_reply_to_user_id").getAsLong();
    } catch (Exception e) {
        status.inReplyToUserId = -1L;
    }

    try {
        status.retweetStatusId = obj.getAsJsonObject("retweeted_status").get("id").getAsLong();
        status.retweetUserId = obj.getAsJsonObject("retweeted_status").get("user").getAsJsonObject().get("id")
                .getAsLong();
        // retweet_count might say "100+"
        // TODO: This is ugly, come back and fix later.
        status.retweetCount = Integer.parseInt(obj.get("retweet_count").getAsString().replace("+", ""));
    } catch (Exception e) {
        status.retweetStatusId = -1L;
        status.retweetUserId = -1L;
        status.retweetCount = -1;
    }

    try {
        status.inReplyToUserId = obj.get("in_reply_to_user_id").getAsLong();
    } catch (Exception e) {
        status.inReplyToUserId = -1L;
    }

    try {
        status.latitude = obj.getAsJsonObject("coordinates").getAsJsonArray("coordinates").get(1).getAsDouble();
        status.longitude = obj.getAsJsonObject("coordinates").getAsJsonArray("coordinates").get(0)
                .getAsDouble();
    } catch (Exception e) {
        status.latitude = Double.NEGATIVE_INFINITY;
        status.longitude = Double.NEGATIVE_INFINITY;
    }

    try {
        status.lang = obj.get("lang").getAsString();
    } catch (Exception e) {
        status.lang = "unknown";
    }

    status.followersCount = obj.get("user").getAsJsonObject().get("followers_count").getAsInt();
    status.friendsCount = obj.get("user").getAsJsonObject().get("friends_count").getAsInt();
    status.statusesCount = obj.get("user").getAsJsonObject().get("statuses_count").getAsInt();

    status.jsonObject = obj;
    status.jsonString = json;

    return status;
}

From source file:ccm.pay2spawn.configurator.Configurator.java

License:Open Source License

private void setupModels() {
    mainTable.setModel(new AbstractTableModel() {
        @Override//w  w w .j a v a  2 s .co m
        public int getRowCount() {
            return rootArray.size();
        }

        @Override
        public int getColumnCount() {
            return COLUMN_NAMES.length;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            JsonObject jsonObject = rootArray.get(rowIndex).getAsJsonObject();
            if (!jsonObject.has(COLUMN_KEYS[columnIndex]))
                return "";
            switch (columnIndex) {
            default:
                return jsonObject.get(COLUMN_KEYS[columnIndex]).getAsString();
            case 4:
                HashSet<String> types = new HashSet<>();
                for (JsonElement element : jsonObject.getAsJsonArray(COLUMN_KEYS[columnIndex]))
                    types.add(element.getAsJsonObject().get("type").getAsString());
                return JOINER_COMMA_SPACE.join(types);
            }
        }

        @Override
        public String getColumnName(int column) {
            return COLUMN_NAMES[column];
        }
    });

    typeList.setModel(new AbstractListModel<String>() {
        final ArrayList<String> names = TypeRegistry.getNames();

        @Override
        public int getSize() {
            return names.size();
        }

        @Override
        public String getElementAt(int index) {
            return names.get(index);
        }
    });

    rewards.setModel(new AbstractListModel<String>() {
        @Override
        public int getSize() {
            return rewardData == null ? 0 : rewardData.size();
        }

        @Override
        public String getElementAt(int index) {
            if (rewardData.get(index).getAsJsonObject().has("type"))
                return rewardData.get(index).getAsJsonObject().getAsJsonPrimitive("type").getAsString();
            else
                return "ERROR IN CONFIG - No type for reward " + index;
        }
    });
}

From source file:ccm.pay2spawn.permissions.Group.java

License:Open Source License

public Group(JsonObject jsonObject) {
    name = jsonObject.get("name").getAsString();
    if (jsonObject.has("parent"))
        parent = jsonObject.get("parent").getAsString();
    for (JsonElement node : jsonObject.getAsJsonArray("nodes"))
        nodes.add(new Node(node.getAsString()));
}

From source file:ccm.pay2spawn.permissions.Player.java

License:Open Source License

public Player(JsonObject jsonObject) {
    name = jsonObject.get("name").getAsString();
    if (jsonObject.has("groups"))
        for (JsonElement groupName : jsonObject.getAsJsonArray("groups"))
            groups.add(groupName.getAsString());
    if (jsonObject.has("overrideNodes"))
        for (JsonElement node : jsonObject.getAsJsonArray("overrideNodes"))
            overrideNodes.add(new Node(node.getAsString()));
}

From source file:ccm.pay2spawn.types.CommandType.java

License:Open Source License

@Override
public String replaceInTemplate(String id, JsonObject jsonObject) {
    switch (id) {
    case "cmd":
        return jsonObject.get(COMMAND_KEY).getAsString().replace(typeMap.get(COMMAND_KEY) + ":", "");
    }//  w ww . java2 s.  com
    return id;
}

From source file:ccm.pay2spawn.types.CustomEntityType.java

License:Open Source License

@Override
public String replaceInTemplate(String id, JsonObject jsonObject) {
    switch (id) {
    case "entity":
        StringBuilder sb = new StringBuilder();
        sb.append(jsonObject.get("id").getAsString().replace("STRING:", ""));
        while (jsonObject.has(RIDING_KEY)) {
            jsonObject = jsonObject.getAsJsonObject(RIDING_KEY);
            sb.append(" riding a ").append(jsonObject.get("id").getAsString().replace("STRING:", ""));
        }//w ww  . j  av  a 2  s  . c o m
        return sb.toString();
    }
    return id;
}

From source file:ccm.pay2spawn.types.DropItemType.java

License:Open Source License

@Override
public String replaceInTemplate(String id, JsonObject jsonObject) {
    switch (id) {
    case "type":
        switch (Integer.parseInt(jsonObject.get(TYPE_KEY).getAsString().split(":", 2)[1])) {
        case HOLDING_1:
            return "one of the selected items";
        case HOLDING_ALL:
            return "all of the selected items";
        case ALL:
            return "all items";
        case ARMOR:
            return "all the armor worn";
        }//w ww.j  a va2  s  .c  o  m
    }
    return id;
}

From source file:ccm.pay2spawn.types.EntityType.java

License:Open Source License

@Override
public String replaceInTemplate(String id, JsonObject jsonObject) {
    switch (id) {
    case "entity":
        StringBuilder sb = new StringBuilder();
        sb.append(jsonObject.get(ENTITYNAME_KEY).getAsString().replace("STRING:", ""));
        while (jsonObject.has(RIDING_KEY)) {
            jsonObject = jsonObject.getAsJsonObject(RIDING_KEY);
            sb.append(" riding a ").append(jsonObject.get(ENTITYNAME_KEY).getAsString().replace("STRING:", ""));
        }//w w w  .  j a v a2s.c  o m
        return sb.toString();
    }
    return id;
}