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:cloud.google.oauth2.MyWayAuthentication.java

License:Apache License

/**
 * Just return access token/*from  w  ww  . ja v  a  2s  .  c o m*/
 * */
@Override
public String getAccessToken() {
    try {
        long iat = (System.currentTimeMillis() / 1000) - 60;
        long exp = iat + 3600;
        String urlParameters = "grant_type=" + GCDStatic.getGrant() + "&assertion=" + getAssertion(exp, iat);

        String result = ConnectionService.connect(GCDStatic.getAud()).body(urlParameters)
                .header("Content-Type", "application/x-www-form-urlencoded").post();
        JsonElement jelement = new JsonParser().parse(result);
        JsonObject jobject = jelement.getAsJsonObject();
        jelement = jobject.get("access_token");
        return jelement != null ? jelement.getAsString() : "";
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:cman.CMAN.java

public void get_info(String modname) {
    if (modname == null) {
        System.out.println("Enter mod name: ");
        modname = input.nextLine();// ww w. j a  va  2  s  .  co m
    }

    JsonObject json_data = util.get_json(modname);
    if (json_data != null) {
        String stable = "Unstable";
        if (!json_data.get("Unstable").getAsBoolean()) {
            stable = "Stable";
        }
        String reqs = "None";
        if (json_data.get("Requirements").getAsJsonArray().size() > 0) {
            reqs = "";
            for (int i = 0; i < json_data.get("Requirements").getAsJsonArray().size(); i++) {
                reqs = reqs + json_data.get("Requirements").getAsJsonArray().get(i).getAsString() + ", ";
            }
            reqs = reqs.substring(0, reqs.length() - 2);
        }
        String incomp = "None";
        if (json_data.get("Incompatibilities").getAsJsonArray().size() > 0) {
            incomp = "";
            for (int i = 0; i < json_data.get("Incompatibilities").getAsJsonArray().size(); i++) {
                incomp = incomp + json_data.get("Incompatibilities").getAsJsonArray().get(i).getAsString()
                        + ", ";
            }
            incomp = incomp.substring(0, incomp.length() - 2);
        }
        System.out.println(json_data.get("Name").getAsString() + ":");
        System.out.println(" Version: " + json_data.get("Version").getAsString() + " (" + stable + ")");
        System.out.println(" Author(s): " + json_data.get("Author").getAsString());
        System.out.println(" Description: " + json_data.get("Desc").getAsString());
        System.out.println(" Requirements: " + reqs);
        System.out.println(" Known Incompatibilities: " + incomp);
        System.out.println(" Download Link: " + json_data.get("Link").getAsString());
        System.out.println(" License: " + json_data.get("License").getAsString());
    }
}

From source file:cmput301.f13t01.elasticsearch.InterfaceAdapter.java

License:GNU General Public License

private JsonElement get(final JsonObject wrapper, String memberName) {
    final JsonElement elem = wrapper.get(memberName);
    if (elem == null)
        throw new JsonParseException(
                "no '" + memberName + "' member found in what was expected to be an interface wrapper");
    return elem;//from  w w w.j a  v a2s  .c  o m
}

From source file:cn.savor.small.netty.NettyClientHandler.java

License:Open Source License

private void handleSpecialty(String json, MessageBean response) {
    try {//from  ww  w. j ava2s  .c  o  m
        JsonObject jsonObject = (JsonObject) new JsonParser().parse(json);
        String deviceId = jsonObject.get("deviceId").getAsString();
        String deviceName = jsonObject.get("deviceName").getAsString();
        String specialtyIds = jsonObject.get("specialtyId").getAsString();
        int interval = jsonObject.get("interval").getAsInt();

        ResponseT1<SpecialtyResponseBean> resp = new ResponseT1<>();

        if (TextUtils.isEmpty(specialtyIds)) {
            resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_FAILED);
            resp.setInfo("?");
        } else {

            ArrayList<String> paths = new ArrayList<>();
            String failedIds = null;
            if ("-1".equals(specialtyIds)) {
                List<RstrSpecialty> specialties = DBHelper.get(mContext).findSpecialtyByWhere(null, null);

                if (specialties != null && specialties.size() > 0) {
                    for (RstrSpecialty specialty : specialties) {
                        paths.add(specialty.getMedia_path());
                    }
                }
            } else {
                String[] ids = specialtyIds.split(",");
                failedIds = "";
                for (int i = 0; i < ids.length; i++) {
                    String id = ids[i].trim();

                    String selection = DBHelper.MediaDBInfo.FieldName.FOOD_ID + "=?";
                    String[] selectionArgs = new String[] { id };
                    List<RstrSpecialty> specialties = DBHelper.get(mContext).findSpecialtyByWhere(selection,
                            selectionArgs);

                    if (specialties != null && specialties.size() > 0) {
                        paths.add(specialties.get(0).getMedia_path());
                    } else {
                        failedIds += id + ",";
                    }
                }
                if (!TextUtils.isEmpty(failedIds)) {
                    failedIds = failedIds.substring(0, failedIds.length() - 1);
                }
            }

            if (paths.size() > 0) {
                if (TextUtils.isEmpty(GlobalValues.CURRENT_PROJECT_DEVICE_ID)
                        || deviceId.equals(GlobalValues.CURRENT_PROJECT_DEVICE_ID)
                        || GlobalValues.IS_RSTR_PROJECTION) {
                    boolean isNewDevice = TextUtils.isEmpty(GlobalValues.CURRENT_PROJECT_DEVICE_ID);

                    GlobalValues.CURRENT_PROJECT_DEVICE_ID = deviceId;
                    GlobalValues.CURRENT_PROJECT_DEVICE_NAME = deviceName;
                    GlobalValues.IS_RSTR_PROJECTION = true;
                    GlobalValues.CURRENT_PROJECT_DEVICE_IP = NettyClient.host;
                    AppApi.resetPhoneInterface(GlobalValues.CURRENT_PROJECT_DEVICE_IP);

                    SpecialtyResponseBean specialtyResponseBean = new SpecialtyResponseBean();
                    specialtyResponseBean.setFounded_count(paths.size());
                    specialtyResponseBean.setFailed_ids(failedIds);
                    resp.setContent(specialtyResponseBean);
                    if (TextUtils.isEmpty(failedIds)) {
                        resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_SUCCESS);
                        resp.setInfo("??");
                    } else {
                        resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_SPECIALTY_INCOMPLETE);
                        resp.setInfo(failedIds);
                    }
                    ProjectOperationListener.getInstance(mContext).showSpecialty(paths, interval, isNewDevice);
                } else {
                    resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_FAILED);
                    if (GlobalValues.IS_LOTTERY) {
                        resp.setInfo(
                                "?" + GlobalValues.CURRENT_PROJECT_DEVICE_NAME + " ");
                    } else {
                        resp.setInfo(
                                "?" + GlobalValues.CURRENT_PROJECT_DEVICE_NAME + " ?");
                    }
                }
            } else {
                resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_FAILED);
                resp.setInfo("??");
            }
        }

        response.getContent().add(new Gson().toJson(resp));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:cn.savor.small.netty.NettyClientHandler.java

License:Open Source License

private void handleGreeting(String json, MessageBean response) {
    try {/*from  www.ja  v a2s  . co m*/
        JsonObject jsonObject = (JsonObject) new JsonParser().parse(json);
        String deviceId = jsonObject.get("deviceId").getAsString();
        String deviceName = jsonObject.get("deviceName").getAsString();
        String words = jsonObject.get("word").getAsString();
        int template = jsonObject.get("templateId").getAsInt();

        ResponseT1 resp = new ResponseT1();
        if (TextUtils.isEmpty(GlobalValues.CURRENT_PROJECT_DEVICE_ID)
                || deviceId.equals(GlobalValues.CURRENT_PROJECT_DEVICE_ID) || GlobalValues.IS_RSTR_PROJECTION) {
            boolean isNewDevice = TextUtils.isEmpty(GlobalValues.CURRENT_PROJECT_DEVICE_ID);

            GlobalValues.CURRENT_PROJECT_DEVICE_ID = deviceId;
            GlobalValues.CURRENT_PROJECT_DEVICE_NAME = deviceName;
            GlobalValues.IS_RSTR_PROJECTION = true;
            GlobalValues.CURRENT_PROJECT_DEVICE_IP = NettyClient.host;
            AppApi.resetPhoneInterface(GlobalValues.CURRENT_PROJECT_DEVICE_IP);

            resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_SUCCESS);
            resp.setInfo("??");

            ProjectOperationListener.getInstance(mContext).showGreeting(words, template, 1000 * 60 * 5,
                    isNewDevice);

            //                Intent intent = new Intent(mContext, GreetingService.class);
            //                intent.putExtra(GreetingService.EXTRA_DEVICE_ID, deviceId);
            //                intent.putExtra(GreetingService.EXTRA_DEVICE_NAME, deviceName);
            //                intent.putExtra(GreetingService.EXTRA_WORDS, words);
            //                intent.putExtra(GreetingService.EXTRA_TEMPLATE, template);
            //                try {
            //                    mContext.unbindService(connection);
            //                } catch (Exception e) {
            //                    e.printStackTrace();
            //                }
            //                mContext.bindService(intent, connection, Service.BIND_AUTO_CREATE);
        } else {
            resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_FAILED);
            if (GlobalValues.IS_LOTTERY) {
                resp.setInfo("?" + GlobalValues.CURRENT_PROJECT_DEVICE_NAME + " ");
            } else {
                resp.setInfo("?" + GlobalValues.CURRENT_PROJECT_DEVICE_NAME + " ?");
            }
        }

        response.getContent().add(new Gson().toJson(resp));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:cn.savor.small.netty.NettyClientHandler.java

License:Open Source License

private void handleAdv(String json, MessageBean response) {
    try {/*  w  ww.j  a  va2 s  .co m*/
        JsonObject jsonObject = (JsonObject) new JsonParser().parse(json);
        String deviceId = jsonObject.get("deviceId").getAsString();
        String deviceName = jsonObject.get("deviceName").getAsString();
        String videoIds = jsonObject.get("vid").getAsString();

        String[] ids = videoIds.split(",");
        String failedIds = "";
        ArrayList<String> paths = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            String id = ids[i].trim();

            String selection = null;
            String[] selectionArgs = null;
            if (!"-1".equals(id)) {
                selection = DBHelper.MediaDBInfo.FieldName.VID + "=?";
                selectionArgs = new String[] { id };
            }
            List<MediaLibBean> videos = DBHelper.get(mContext).findPlayListByWhere(selection, selectionArgs);

            if (videos != null && videos.size() > 0) {
                paths.add(videos.get(0).getMediaPath());
            } else {
                failedIds += id + ",";
            }
        }

        if (!TextUtils.isEmpty(failedIds)) {
            failedIds = failedIds.substring(0, failedIds.length() - 1);
        }

        ResponseT1<AdvResponseBean> resp = new ResponseT1();
        if (paths.size() > 0) {
            if (TextUtils.isEmpty(GlobalValues.CURRENT_PROJECT_DEVICE_ID)
                    || deviceId.equals(GlobalValues.CURRENT_PROJECT_DEVICE_ID)
                    || GlobalValues.IS_RSTR_PROJECTION) {
                boolean isNewDevice = TextUtils.isEmpty(GlobalValues.CURRENT_PROJECT_DEVICE_ID);

                GlobalValues.CURRENT_PROJECT_DEVICE_ID = deviceId;
                GlobalValues.CURRENT_PROJECT_DEVICE_NAME = deviceName;
                GlobalValues.IS_RSTR_PROJECTION = true;
                GlobalValues.CURRENT_PROJECT_DEVICE_IP = NettyClient.host;
                AppApi.resetPhoneInterface(GlobalValues.CURRENT_PROJECT_DEVICE_IP);

                AdvResponseBean advResponseBean = new AdvResponseBean();
                advResponseBean.setFailed_ids(failedIds);
                resp.setContent(advResponseBean);
                if (TextUtils.isEmpty(failedIds)) {
                    resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_SUCCESS);
                    resp.setInfo("??");
                } else {
                    resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_SPECIALTY_INCOMPLETE);
                    resp.setInfo(failedIds);
                }
                ProjectOperationListener.getInstance(mContext).showAdv(paths, isNewDevice);
            } else {
                resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_FAILED);
                if (GlobalValues.IS_LOTTERY) {
                    resp.setInfo("?" + GlobalValues.CURRENT_PROJECT_DEVICE_NAME + " ");
                } else {
                    resp.setInfo("?" + GlobalValues.CURRENT_PROJECT_DEVICE_NAME + " ?");
                }
            }
        } else {
            resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_FAILED);
            resp.setInfo("?");
        }

        response.getContent().add(new Gson().toJson(resp));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:cn.savor.small.netty.NettyClientHandler.java

License:Open Source License

private void handleGreetingThenSpecialty(String json, MessageBean response) {
    try {// www. j  a  v  a 2 s . c  o  m
        JsonObject jsonObject = (JsonObject) new JsonParser().parse(json);
        String deviceId = jsonObject.get("deviceId").getAsString();
        String deviceName = jsonObject.get("deviceName").getAsString();
        String words = jsonObject.get("word").getAsString();
        int template = jsonObject.get("templateId").getAsInt();

        ResponseT1<SpecialtyResponseBean> resp = new ResponseT1<>();
        if (TextUtils.isEmpty(GlobalValues.CURRENT_PROJECT_DEVICE_ID)
                || deviceId.equals(GlobalValues.CURRENT_PROJECT_DEVICE_ID) || GlobalValues.IS_RSTR_PROJECTION) {
            boolean isNewDevice = TextUtils.isEmpty(GlobalValues.CURRENT_PROJECT_DEVICE_ID);

            GlobalValues.CURRENT_PROJECT_DEVICE_ID = deviceId;
            GlobalValues.CURRENT_PROJECT_DEVICE_NAME = deviceName;
            GlobalValues.IS_RSTR_PROJECTION = true;
            GlobalValues.CURRENT_PROJECT_DEVICE_IP = NettyClient.host;
            AppApi.resetPhoneInterface(GlobalValues.CURRENT_PROJECT_DEVICE_IP);

            ArrayList<String> paths = new ArrayList<>();
            int interval;
            List<RstrSpecialty> specialties = DBHelper.get(mContext).findSpecialtyByWhere(null, null);

            if (specialties != null && specialties.size() > 0) {
                for (RstrSpecialty specialty : specialties) {
                    paths.add(specialty.getMedia_path());
                }
            }
            if (paths.size() > 1) {
                interval = 10;
            } else {
                interval = 20;
            }

            resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_SUCCESS);
            SpecialtyResponseBean specialtyResponseBean = new SpecialtyResponseBean();
            specialtyResponseBean.setFailed_ids("");
            specialtyResponseBean.setFounded_count(paths.size());
            resp.setContent(specialtyResponseBean);
            resp.setInfo("??");

            ProjectOperationListener.getInstance(mContext).showGreetingThenSpecialty(words, template,
                    1000 * 60 * 5, paths, interval, isNewDevice);

        } else {
            resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_FAILED);
            if (GlobalValues.IS_LOTTERY) {
                resp.setInfo("?" + GlobalValues.CURRENT_PROJECT_DEVICE_NAME + " ");
            } else {
                resp.setInfo("?" + GlobalValues.CURRENT_PROJECT_DEVICE_NAME + " ?");
            }
        }

        response.getContent().add(new Gson().toJson(resp));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:cn.savor.small.netty.NettyClientHandler.java

License:Open Source License

private void handleStopProjection(String json, MessageBean response) {
    try {/*from w  ww.j  a v  a 2s  . c  o  m*/
        JsonObject jsonObject = (JsonObject) new JsonParser().parse(json);
        String deviceId = jsonObject.get("deviceId").getAsString();

        ResponseT1 resp = new ResponseT1();
        if (!TextUtils.isEmpty(deviceId) && deviceId.equals(GlobalValues.CURRENT_PROJECT_DEVICE_ID)
                && GlobalValues.IS_RSTR_PROJECTION) {
            ProjectOperationListener.getInstance(mContext).rstrStop();
            resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_SUCCESS);
            resp.setInfo("??");

            GlobalValues.IS_RSTR_PROJECTION = false;
            GlobalValues.CURRENT_PROJECT_IMAGE_ID = null;
        } else {
            resp.setResult(ConstantValues.SERVER_RESPONSE_CODE_FAILED);
            resp.setInfo("???");
        }

        response.getContent().add(new Gson().toJson(resp));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:co.aurasphere.botmill.kik.util.json.IncomingMessagesDeserializer.java

License:Open Source License

@SuppressWarnings("incomplete-switch")
public MessageCallback deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    MessageCallback messageCallback = new MessageCallback();
    //   The message callback can have multiple message bodies.
    JsonElement jsonMessages = json.getAsJsonObject().get("messages");

    for (int i = 0; i < jsonMessages.getAsJsonArray().size(); i++) {
        //   get type.
        JsonObject jsonMessage = jsonMessages.getAsJsonArray().get(i).getAsJsonObject();
        String typeString = jsonMessage.get("type").getAsString();
        MessageType messageType = MessageType.valueOf(typeString.replace('-', '_').toUpperCase());
        Class<? extends Message> messageClass = null;
        switch (messageType) {
        case TEXT:
            messageClass = TextMessage.class;
            break;
        case PICTURE:
            messageClass = PictureMessage.class;
            break;
        case VIDEO:
            messageClass = VideoMessage.class;
            break;
        case LINK:
            messageClass = LinkMessage.class;
            break;
        case SCAN_DATA:
            messageClass = ScanDataMessage.class;
            break;
        case STICKER:
            messageClass = StickerMessage.class;
            break;
        case FRIEND_PICKER:
            messageClass = FriendPickerMessage.class;
            break;
        case IS_TYPING:
            messageClass = IsTypingMessage.class;
            break;
        case READ_RECEIPT:
            messageClass = ReadReceiptMessage.class;
            break;
        case START_CHATTING:
            messageClass = StartChattingMessage.class;
            break;
        }//  w ww .  java  2 s.com

        Message message = context.deserialize(jsonMessage, messageClass);
        messageCallback.addMessage(message);
    }

    return messageCallback;
}

From source file:co.aurasphere.botmill.kik.util.json.LowerCaseTypeAdapterFactory.java

License:Open Source License

public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
    final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);

    return new TypeAdapter<T>() {

        @Override/*  ww  w  . j a  v a2  s  .  com*/
        public T read(JsonReader in) throws IOException {
            JsonElement tree = elementAdapter.read(in);
            afterRead(tree);
            return delegate.fromJsonTree(tree);
        }

        @Override
        public void write(JsonWriter out, T value) throws IOException {
            JsonElement tree = delegate.toJsonTree(value);
            beforeWrite(value, tree);
            elementAdapter.write(out, tree);
        }

        protected void beforeWrite(T source, JsonElement toSerialize) {
        }

        protected void afterRead(JsonElement deserialized) {
            if (deserialized instanceof JsonObject) {
                JsonObject jsonObject = ((JsonObject) deserialized);
                Set<Map.Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
                for (Map.Entry<String, JsonElement> entry : entrySet) {
                    if (entry.getValue() instanceof JsonElement) {
                        if (entry.getKey().equalsIgnoreCase("type")) {
                            String val = jsonObject.get(entry.getKey()).toString();
                            jsonObject.addProperty(entry.getKey(), val.toLowerCase());
                        }
                    } else {
                        afterRead(entry.getValue());
                    }
                }
            }
        }
    };
}