List of usage examples for com.google.gson JsonElement getAsInt
public int getAsInt()
From source file:com.microsoft.windowsazure.mobileservices.MobileServiceTableBase.java
License:Open Source License
/** * Gets the id property from a given element * //from w w w . ja v a 2 s . c o m * @param element * The element to use * @return The id of the element */ protected int getObjectId(Object element) { if (element == null) { throw new InvalidParameterException("Element cannot be null"); } else if (element instanceof Integer) { return ((Integer) element).intValue(); } JsonObject jsonElement; if (element instanceof JsonObject) { jsonElement = (JsonObject) element; } else { jsonElement = mClient.getGsonBuilder().create().toJsonTree(element).getAsJsonObject(); } JsonElement idProperty = jsonElement.get("id"); if (idProperty instanceof JsonNull || idProperty == null) { throw new InvalidParameterException("Element must contain id property"); } return idProperty.getAsInt(); }
From source file:com.palechip.hudpixelmod.games.ComponentInstanceConfiguration.java
License:Open Source License
public ComponentInstanceConfiguration(Class component, JsonObject instance) { try {/*from w w w. j a v a2s . com*/ this.compenent = component; ids = new HashSet<Integer>(); // get all ids for (JsonElement id : instance.get("ids").getAsJsonArray()) { ids.add(id.getAsInt()); } // get all parameters ArrayList<Class> types = new ArrayList<Class>(); ArrayList<Object> values = new ArrayList<Object>(); for (JsonElement paramElement : instance.get("params").getAsJsonArray()) { // it should be an Object JsonObject param = paramElement.getAsJsonObject(); // get the strings saved in the object String typeStr = param.get("type").getAsString(); String valueStr = param.get("value").getAsString(); Class type; Object value; // go through the possible types if (typeStr.equalsIgnoreCase("boolean") || typeStr.equalsIgnoreCase("bool")) { // boolean type = boolean.class; value = Boolean.parseBoolean(valueStr); } else if (typeStr.equalsIgnoreCase("integer") || typeStr.equalsIgnoreCase("int")) { // integer type = int.class; value = Integer.parseInt(valueStr); } else if (typeStr.equalsIgnoreCase("string")) { // string type = String.class; value = valueStr; } else { // inner static classes of the component. (e.g. enums) MUST provide a function called valueOf(String) // get the class type = Class.forName(component.getName() + "$" + typeStr); // now use the valueOf method to get the value value = type.getMethod("valueOf", String.class).invoke(null, valueStr); } // add the parameter to the temporary lists types.add(type); values.add(value); } // get the constructor based of the types this.constructor = component.getConstructor(types.toArray(new Class[types.size()])); // save the values as an Object array to pass it on instatiation this.paramValues = values.toArray(); } catch (Exception e) { HudPixelMod.instance().logError( "Failed to create a component instance configuration. This instance will be left out."); e.printStackTrace(); // make sure it isn't used by clearing the ids set this.ids.clear(); } }
From source file:com.paysafe.common.impl.BooleanAdapter.java
License:Open Source License
/** * Some api transactions will return 0 or 1 instead of the expected true or false. * * @param el the el// w w w. j a v a2s .c o m * @param typeOfT the type of t * @param context the context * @return Boolean * @throws JsonParseException the json parse exception */ @Override public Boolean deserialize(JsonElement el, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (((Class<?>) typeOfT).equals(Boolean.class)) { return el.getAsBoolean(); } else if (((Class<?>) typeOfT).isInstance(Integer.class)) { final int elAsInt = el.getAsInt(); if (elAsInt < 0 || elAsInt > 1) { throw new JsonParseException("Boolean out of range: " + elAsInt); } return elAsInt == 1; } else { throw new JsonParseException("Unexpected type: " + typeOfT); } }
From source file:com.popdeem.sdk.core.deserializer.PDIntDeserializer.java
License:Open Source License
@Override public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String s = json.getAsString(); if (s.equalsIgnoreCase("") || s.equalsIgnoreCase("no limit")) { return -1; } else {/*from w ww . j a v a 2 s . c o m*/ return json.getAsInt(); } }
From source file:com.prayer.vakit.times.gson.BooleanSerializer.java
License:Apache License
@Override public Boolean deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return arg0.getAsInt() == 1; }
From source file:com.remediatetheflag.global.actions.auth.management.admin.AddOrganizationAction.java
License:Apache License
@Override public void doAction(HttpServletRequest request, HttpServletResponse response) throws Exception { JsonObject json = (JsonObject) request.getAttribute(Constants.REQUEST_JSON); User sessionUser = (User) request.getSession().getAttribute(Constants.ATTRIBUTE_SECURITY_CONTEXT); JsonElement nameElement = json.get(Constants.ACTION_PARAM_NAME); JsonElement contactEmailElement = json.get(Constants.ACTION_PARAM_EMAIL); JsonElement maxUsersElement = json.get(Constants.ACTION_PARAM_MAX_USERS); String name = nameElement.getAsString(); String contactEmail = contactEmailElement.getAsString(); Integer maxUsers = maxUsersElement.getAsInt(); Organization o = new Organization(); Calendar c = Calendar.getInstance(); c.setTime(new Date()); o.setDateJoined(c.getTime());/*w ww . j a v a 2s.c o m*/ o.setName(name); o.setEmail(contactEmail); o.setCreatedByUser(sessionUser.getIdUser()); o.setMaxUsers(maxUsers); o.setStatus(OrganizationStatus.ACTIVE); Integer id = hpc.addOrganization(o); if (null != id) { Organization oDB = hpc.getOrganizationById(id); sessionUser.getManagedOrganizations().add(oDB); request.getSession().setAttribute(Constants.ATTRIBUTE_SECURITY_CONTEXT, sessionUser); hpc.addManagedOrganization(sessionUser, id); MessageGenerator.sendSuccessMessage(response); } else { MessageGenerator.sendErrorMessage("Failed", response); } }
From source file:com.remediatetheflag.global.actions.auth.management.admin.DisableAvailableExerciseForOrganizationAction.java
License:Apache License
@Override public void doAction(HttpServletRequest request, HttpServletResponse response) throws Exception { User sessionUser = (User) request.getSession().getAttribute(Constants.ATTRIBUTE_SECURITY_CONTEXT); JsonObject json = (JsonObject) request.getAttribute(Constants.REQUEST_JSON); JsonElement idOrganizationElement = json.get(Constants.ACTION_PARAM_ORG_ID); Integer idOrganization = idOrganizationElement.getAsInt(); Organization org = hpc.getOrganizationById(idOrganization); if (!isManagingOrg(sessionUser, org)) { MessageGenerator.sendErrorMessage("NotFound", response); return;//w w w.j a v a 2 s .c o m } JsonElement idExerciseElement = json.get(Constants.ACTION_PARAM_EXERCISE); Integer idExercise = idExerciseElement.getAsInt(); AvailableExercise ex = hpc.getAvailableExercise(idExercise); if (null == ex) { MessageGenerator.sendErrorMessage("NotFound", response); return; } hpc.removeAvailableExerciseForOrganization(org, ex); MessageGenerator.sendSuccessMessage(response); }
From source file:com.remediatetheflag.global.actions.auth.management.admin.EnableAvailableExerciseForOrganizationAction.java
License:Apache License
@Override public void doAction(HttpServletRequest request, HttpServletResponse response) throws Exception { User sessionUser = (User) request.getSession().getAttribute(Constants.ATTRIBUTE_SECURITY_CONTEXT); JsonObject json = (JsonObject) request.getAttribute(Constants.REQUEST_JSON); JsonElement idOrganizationElement = json.get(Constants.ACTION_PARAM_ORG_ID); Integer idOrganization = idOrganizationElement.getAsInt(); Organization org = hpc.getOrganizationById(idOrganization); if (!isManagingOrg(sessionUser, org)) { MessageGenerator.sendErrorMessage("NotFound", response); return;//from ww w . j a v a 2s .c o m } JsonElement idExerciseElement = json.get(Constants.ACTION_PARAM_EXERCISE); Integer idExercise = idExerciseElement.getAsInt(); AvailableExercise ex = hpc.getAvailableExercise(idExercise); if (null == ex) { MessageGenerator.sendErrorMessage("NotFound", response); return; } hpc.addAvailableExerciseForOrganization(org, ex); MessageGenerator.sendSuccessMessage(response); }
From source file:com.remediatetheflag.global.actions.auth.management.admin.GenerateInvitationCodeAction.java
License:Apache License
@Override public void doAction(HttpServletRequest request, HttpServletResponse response) throws Exception { JsonObject json = (JsonObject) request.getAttribute(Constants.REQUEST_JSON); User sessionUser = (User) request.getSession().getAttribute(Constants.ATTRIBUTE_SECURITY_CONTEXT); JsonElement orgIdElement = json.get(Constants.ACTION_PARAM_ORG_ID); Integer orgId = orgIdElement.getAsInt(); JsonElement maxRedeemsElement = json.get(Constants.ACTION_PARAM_MAX_USERS); Integer maxRedeems = maxRedeemsElement.getAsInt(); String redeem = RandomGenerator.getNextString(10); Organization org = hpc.getOrganizationById(orgId); if (!isManagingOrg(sessionUser, org)) { MessageGenerator.sendErrorMessage("NotFound", response); return;//from w w w . j a va 2 s .com } InvitationCodeForOrganization invitation = new InvitationCodeForOrganization(); invitation.setCode(redeem); invitation.setLeftToRedeem(maxRedeems); invitation.setOrganization(org); Boolean result = hpc.addInvitationCode(invitation); if (result) MessageGenerator.sendSuccessMessage(response); else MessageGenerator.sendErrorMessage("Failed", response); }
From source file:com.remediatetheflag.global.actions.auth.management.admin.GetInvitationCodesAction.java
License:Apache License
@Override public void doAction(HttpServletRequest request, HttpServletResponse response) throws Exception { JsonObject json = (JsonObject) request.getAttribute(Constants.REQUEST_JSON); User sessionUser = (User) request.getSession().getAttribute(Constants.ATTRIBUTE_SECURITY_CONTEXT); JsonElement orgIdElement = json.get(Constants.ACTION_PARAM_ORG_ID); Integer orgId = orgIdElement.getAsInt(); Organization org = hpc.getOrganizationById(orgId); if (!isManagingOrg(sessionUser, org)) { MessageGenerator.sendErrorMessage("NotFound", response); return;//from w w w . ja v a2 s . c o m } List<InvitationCodeForOrganization> codes = hpc.getInvitationCodesForOrganization(orgId); MessageGenerator.sendInvitationCodesMessage(codes, response); }