Example usage for com.google.gson JsonObject getAsJsonObject

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

Introduction

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

Prototype

public JsonObject getAsJsonObject(String memberName) 

Source Link

Document

Convenience method to get the specified member as a JsonObject.

Usage

From source file:com.gst.portfolio.loanaccount.loanschedule.service.LoanScheduleAssembler.java

License:Apache License

private void extractLoanTermVariations(final Loan loan, final String json,
        final List<LoanTermVariations> loanTermVariations) {
    final JsonElement element = this.fromApiJsonHelper.parse(json);
    if (loan.loanProduct().allowVariabeInstallments()) {
        if (element.isJsonObject()
                && this.fromApiJsonHelper.parameterExists(LoanApiConstants.exceptionParamName, element)) {
            final JsonObject topLevelJsonElement = element.getAsJsonObject();
            final String dateFormat = this.fromApiJsonHelper.extractDateFormatParameter(topLevelJsonElement);
            final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement);
            final JsonObject exceptionObject = topLevelJsonElement
                    .getAsJsonObject(LoanApiConstants.exceptionParamName);
            if (this.fromApiJsonHelper.parameterExists(LoanApiConstants.modifiedinstallmentsParamName,
                    exceptionObject)/*from  www  . j a  va  2 s  . co m*/
                    && exceptionObject.get(LoanApiConstants.modifiedinstallmentsParamName).isJsonArray()) {
                final JsonArray modificationsArray = exceptionObject
                        .get(LoanApiConstants.modifiedinstallmentsParamName).getAsJsonArray();
                extractLoanTermVariations(loan, dateFormat, locale, modificationsArray, false, false,
                        loanTermVariations);
            }
            if (this.fromApiJsonHelper.parameterExists(LoanApiConstants.newinstallmentsParamName,
                    exceptionObject)
                    && exceptionObject.get(LoanApiConstants.newinstallmentsParamName).isJsonArray()) {
                final JsonArray array = exceptionObject.get(LoanApiConstants.newinstallmentsParamName)
                        .getAsJsonArray();
                extractLoanTermVariations(loan, dateFormat, locale, array, true, false, loanTermVariations);
            }
            if (this.fromApiJsonHelper.parameterExists(LoanApiConstants.deletedinstallmentsParamName,
                    exceptionObject)
                    && exceptionObject.get(LoanApiConstants.deletedinstallmentsParamName).isJsonArray()) {
                final JsonArray array = exceptionObject.get(LoanApiConstants.deletedinstallmentsParamName)
                        .getAsJsonArray();
                extractLoanTermVariations(loan, dateFormat, locale, array, false, true, loanTermVariations);
            }
        }
    }
}

From source file:com.gst.portfolio.loanaccount.serialization.VariableLoanScheduleFromApiJsonValidator.java

License:Apache License

public void validateSchedule(final String json, final Loan loan) {
    if (StringUtils.isBlank(json)) {
        throw new InvalidJsonException();
    }/*from w  ww  . j av a  2 s  .c om*/

    final Type typeOfMap = new TypeToken<Map<String, Object>>() {
    }.getType();
    this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json,
            this.variableSchedulesupportedParameters);

    final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
    final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors)
            .resource("loan");

    if (!loan.isSubmittedAndPendingApproval()) {
        baseDataValidator.reset().failWithCodeNoParameterAddedToErrorCode(
                "account.is.not.submitted.and.pending.state", "Loan is not in submited state");
        throw new PlatformApiDataValidationException(dataValidationErrors);
    }

    final JsonElement element = this.fromApiJsonHelper.parse(json);
    if (loan.loanProduct().allowVariabeInstallments()) {
        if (element.isJsonObject()
                && this.fromApiJsonHelper.parameterExists(LoanApiConstants.exceptionParamName, element)) {
            final JsonObject topLevelJsonElement = element.getAsJsonObject();
            final String dateFormat = this.fromApiJsonHelper.extractDateFormatParameter(topLevelJsonElement);
            final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement);
            final JsonObject exceptionObject = topLevelJsonElement
                    .getAsJsonObject(LoanApiConstants.exceptionParamName);
            this.fromApiJsonHelper.checkForUnsupportedParameters(exceptionObject,
                    this.variableSchedulesupportedArrayParameters);
            if (this.fromApiJsonHelper.parameterExists(LoanApiConstants.modifiedinstallmentsParamName,
                    exceptionObject)
                    && exceptionObject.get(LoanApiConstants.modifiedinstallmentsParamName).isJsonArray()) {
                final JsonArray modificationsArray = exceptionObject
                        .get(LoanApiConstants.modifiedinstallmentsParamName).getAsJsonArray();
                validateLoanTermVariations(loan, baseDataValidator, dateFormat, locale, modificationsArray,
                        this.variableScheduleModifiedParameters,
                        LoanApiConstants.modifiedinstallmentsParamName);
            }
            if (this.fromApiJsonHelper.parameterExists(LoanApiConstants.newinstallmentsParamName,
                    exceptionObject)
                    && exceptionObject.get(LoanApiConstants.newinstallmentsParamName).isJsonArray()) {
                final JsonArray array = exceptionObject.get(LoanApiConstants.newinstallmentsParamName)
                        .getAsJsonArray();
                validateLoanTermVariations(loan, baseDataValidator, dateFormat, locale, array,
                        this.variableScheduleNewInstallmentParameters,
                        LoanApiConstants.newinstallmentsParamName);
            }
            if (this.fromApiJsonHelper.parameterExists(LoanApiConstants.deletedinstallmentsParamName,
                    exceptionObject)
                    && exceptionObject.get(LoanApiConstants.deletedinstallmentsParamName).isJsonArray()) {
                final JsonArray array = exceptionObject.get(LoanApiConstants.deletedinstallmentsParamName)
                        .getAsJsonArray();
                validateLoanTermVariations(loan, baseDataValidator, dateFormat, locale, array,
                        this.variableScheduleDeleteInstallmentParameters,
                        LoanApiConstants.deletedinstallmentsParamName);
            }
        }
    } else {
        baseDataValidator.reset().failWithCodeNoParameterAddedToErrorCode("variable.schedule.not.supported",
                "Loan schedule modification not allowed");
    }
    if (!dataValidationErrors.isEmpty()) {
        throw new PlatformApiDataValidationException(dataValidationErrors);
    }
}

From source file:com.hbm.devices.jet.JetPeer.java

License:Open Source License

private void handleFetch(int fetchId, JsonObject object) {
    synchronized (openFetches) {
        FetchEventCallback callback = openFetches.get(fetchId);
        if (callback != null) {
            JsonObject params = object.getAsJsonObject("params");
            if (params != null) {
                callback.onFetchEvent(params);
            }/*w ww .  ja v  a2  s. c  o m*/
        }
    }
}

From source file:com.hbm.devices.jet.JetPeer.java

License:Open Source License

private boolean handleStateCallback(JsonObject object, String path) throws JsonRpcException {
    boolean stateFound;

    synchronized (stateCallbacks) {
        stateFound = stateCallbacks.containsKey(path);
    }//from   ww  w .j a v a 2  s. c o  m

    if (stateFound) {
        StateCallback callback;
        synchronized (stateCallbacks) {
            callback = stateCallbacks.get(path);
        }
        if (callback == null) {
            throw new JsonRpcException(JsonRpcException.INVALID_REQUEST, "state is readonly");
        }

        JsonObject parameters = object.getAsJsonObject("params");
        if (parameters == null) {
            throw new JsonRpcException(JsonRpcException.INVALID_PARAMS, "no parameters in json");
        }

        JsonElement value = parameters.get("value");
        if (value == null) {
            throw new JsonRpcException(JsonRpcException.INVALID_PARAMS, "no value in parameter");
        }

        JsonElement notifyValue = callback.onStateSet(path, value);
        if (notifyValue != null) {
            this.change(path, notifyValue, null, 0);
        }

        JsonObject result = new JsonObject();
        result.addProperty("result", true);
        sendResponse(object, result);
        return true;

    } else {
        return false;
    }
}

From source file:com.headswilllol.mineflat.gui.GuiParser.java

License:Open Source License

/**
 * Parses a JSON file into a {@link GuiElement} object.
 * @param path the path of the file to parse
 * @return the constructed {@link GuiElement}
 *//*  w  w  w.  j a  v a  2  s  . c o m*/
public static GuiElement parseFile(String path) {
    InputStream is = GuiParser.class.getResourceAsStream(path);
    if (is != null) {
        JsonParser parser = new JsonParser();
        JsonObject json = parser.parse(new InputStreamReader(is)).getAsJsonObject();
        JsonObject menus = json.getAsJsonObject("menus");
        GuiElement gui = parseElement("gui", menus, Optional.<GuiElement>absent());
        //TODO: this is a terrible way of accomplishing this and should be reconsidered
        for (GuiElement e : elementsToCenter) {
            e.setPosition(new Vector2i(
                    ((e.getParent().isPresent() ? e.getParent().get().getSize().getX() : Display.getWidth()) / 2
                            - (e.getSize().getX()) / 2),
                    e.getPosition().getY()));
        }
        return gui;
    }
    throw new IllegalArgumentException("Cannot find resource " + path + "!");
}

From source file:com.ibasco.agql.protocols.valve.csgo.webapi.CsgoWebApiInterface.java

License:Open Source License

protected JsonObject getResult(JsonObject root) {
    if (root.has("result")) {
        return root.getAsJsonObject("result");
    }/*ww  w. ja v  a2  s .  co m*/
    throw new JsonElementNotFoundException(root, "Did not find 'result' section from the response");
}

From source file:com.ibasco.agql.protocols.valve.dota2.webapi.Dota2WebApiInterface.java

License:Open Source License

protected JsonObject getValidResult(JsonObject json) {
    JsonObject result = json.getAsJsonObject("result");
    if (result != null)
        return result;
    throw new Dota2WebException(String.format(
            "The response either did not contain the desired result or the server responded in error. (JSON = %s)",
            json));/*from  www.j a va2  s.  co m*/
}

From source file:com.ibasco.agql.protocols.valve.steam.webapi.interfaces.SteamStorefront.java

License:Open Source License

public CompletableFuture<StoreAppDetails> getAppDetails(int appId, String countryCode, String language) {
    CompletableFuture<JsonObject> json = sendRequest(
            new GetAppDetails(VERSION_1, appId, countryCode, language));
    return json.thenApply(root -> {
        JsonObject appObject = root.getAsJsonObject(String.valueOf(appId));
        if (appObject.getAsJsonPrimitive("success").getAsBoolean()) {
            JsonObject appData = appObject.getAsJsonObject("data");
            return fromJson(appData, StoreAppDetails.class);
        }/* ww w .java  2s.  co m*/
        return null;
    });
}

From source file:com.ibasco.agql.protocols.valve.steam.webapi.interfaces.SteamStorefront.java

License:Open Source License

public CompletableFuture<StorePackageDetails> getPackageDetails(int packageId, String countryCode,
        String language) {/*from w ww.  j  av a2s. co  m*/
    CompletableFuture<JsonObject> json = sendRequest(
            new GetPackageDetails(VERSION_1, packageId, countryCode, language));
    return json.thenApply(root -> {
        JsonObject packageObj = root.getAsJsonObject(String.valueOf(packageId));
        JsonObject packageData = packageObj.getAsJsonObject("data");
        return fromJson(packageData, StorePackageDetails.class);
    });
}

From source file:com.ibasco.agql.protocols.valve.steam.webapi.interfaces.SteamUser.java

License:Open Source License

public CompletableFuture<List<SteamGroupId>> getUserGroupList(long steamId) {
    CompletableFuture<JsonObject> json = sendRequest(new GetUserGroupList(VERSION_1, steamId));
    return json.thenApply((JsonObject root) -> {
        JsonArray groups = root.getAsJsonObject("response").getAsJsonArray("groups");
        Type type = new TypeToken<List<SteamGroupId>>() {
        }.getType();//from   ww  w .  j  av a  2 s .  c  om
        return builder().fromJson(groups, type);
    });
}