Example usage for com.google.gson JsonElement getAsInt

List of usage examples for com.google.gson JsonElement getAsInt

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsInt.

Prototype

public int getAsInt() 

Source Link

Document

convenience method to get this element as a primitive integer value.

Usage

From source file:es.us.isa.aml.parsers.agreements.json.InterfaceAdapterInteger.java

License:Open Source License

@Override
public Integer deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
        throws JsonParseException {
    return je.getAsInt();
}

From source file:ezbake.services.search.utils.BooleanSerializer.java

License:Apache License

@Override
public Boolean deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
        throws JsonParseException {
    if (arg0.getAsJsonPrimitive().isBoolean()) {
        return arg0.getAsBoolean();
    }//from   ww  w  .  j  a  v a2  s. c o  m
    if (arg0.getAsJsonPrimitive().isString()) {
        return Boolean.parseBoolean(arg0.getAsString());
    } else {
        return arg0.getAsInt() == 1 ? true : false;
    }
}

From source file:FileHelper.ExcelHelper.java

private JsonObject CreateJsonOnject(ArrayList<DataInput> listDataInput,
        ArrayList<DataInputLevel2> dataInputLevel2, JsonObject jObjOld, Gson gson,
        ArrayList<NameDynamic> nameDys, int k, String nameAlgro, String rawData)
        throws NoSuchAlgorithmException, UnsupportedEncodingException, SignatureException,
        NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, Exception {
    JsonObject jObjNew = new JsonObject();
    int countObject = 0;
    for (DataInput dataI : listDataInput) {
        JsonElement je = jObjOld.get(dataI.getName());
        if (je != null) {
            if (dataI.getType().equals("String")) {
                String value = je.getAsString();
                if (!nameDys.isEmpty()) {
                    for (NameDynamic nameDy : nameDys) {
                        if (dataI.getName().equals(nameDy.getName())) {
                            value = new String(value + k);
                        }/*from  www . j av a 2s .  c o  m*/
                    }
                }
                jObjNew.addProperty(dataI.getName(), value);
            } else if (dataI.getType().equals("Integer")) {
                int value = je.getAsInt();
                jObjNew.addProperty(dataI.getName(), value);
            } else if (dataI.getType().equals("Object")) {
                JsonObject jsonChild = gson.fromJson(je.toString(), JsonObject.class);
                JsonObject jsonChildNew = new JsonObject();
                DataInputLevel2 dataIL2 = dataInputLevel2.get(countObject);
                for (DataInput dataI2 : dataIL2.getListDataIputLevel2()) {
                    if (dataI2.getType().equals("String")) {
                        je = jsonChild.get(dataI2.getName());
                        if (je != null) {
                            String value = je.getAsString();
                            if (!nameDys.isEmpty()) {
                                for (NameDynamic nameDy : nameDys) {
                                    if (dataI2.getName().equals(nameDy.getName())) {
                                        value = new String(value + k);
                                    }
                                }
                            }
                            jsonChildNew.addProperty(dataI2.getName(), value);
                        }
                    } else if (dataI2.getType().equals("Integer")) {
                        je = jsonChild.get(dataI2.getName());
                        if (je != null) {
                            int value = je.getAsInt();
                            jsonChildNew.addProperty(dataI2.getName(), value);
                        }
                    } else if (dataI2.getType().equals("Object")) {
                        je = jsonChild.get(dataI2.getName());
                        if (je != null) {
                            JsonObject value = je.getAsJsonObject();
                            jsonChildNew.add(dataI2.getName(), value);
                        }
                    }
                }
                jObjNew.add(dataI.getName(), jsonChildNew);
                countObject++;
            }
        }
    }
    // Raw Data
    String[] arr = rawData.split(",");
    String rawDataNew = "";
    char a = '"';
    for (String str : arr) {
        if (str.charAt(0) == a) {
            String value = str.substring(1, str.length() - 1);
            rawDataNew += value;
        } else {
            JsonElement je = jObjNew.get(str);
            if (je.isJsonObject()) {
                String value = je.toString();
                rawDataNew += value;
            } else {
                String value = je.getAsString();
                rawDataNew += value;
            }
        }
    }
    String[] arrS = nameAlgro.split("-");
    if (arrS[0].equals("chksum")) {
        String chksum = CheckSumInquireCard.createCheckSum(nameAlgro, rawDataNew);
        System.out.println("chksum: " + chksum);
        jObjNew.addProperty(listDataInput.get(listDataInput.size() - 1).getName(), chksum);
    } else if (arrS[0].equals("signature")) {
        String signature = RSASHA1Signature.getSignature(nameAlgro, rawDataNew);
        System.out.println("signature: " + signature);
        jObjNew.addProperty(listDataInput.get(listDataInput.size() - 1).getName(), signature);
    }
    return jObjNew;
}

From source file:fm.feed.android.playersdk.service.webservice.adapter.FeedFMErrorDeserializer.java

License:Open Source License

@Override
public FeedFMError deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    JsonElement jsonCode = jsonObject.get("code");
    JsonElement jsonMessage = jsonObject.get("message");
    JsonElement jsonStatus = jsonObject.get("status");

    return new FeedFMError(jsonCode.getAsInt(), jsonMessage.getAsString(), jsonStatus.getAsInt());
}

From source file:Implement.Service.ProviderServiceImpl.java

@Override
public String editProviderResource(String data, int providerID) {
    JsonObject dataJson = gson.fromJson(data, JsonObject.class);
    String name = dataJson.get("name").getAsString();
    int capacity = dataJson.get("capacity").getAsInt();
    int resourceID = dataJson.get("resourceID").getAsInt();

    JsonArray listJsonOfSkill;/*ww w .  ja  v  a 2s.  co m*/
    try {
        listJsonOfSkill = dataJson.get("skills").getAsJsonArray();
    } catch (Exception e) {
        listJsonOfSkill = new JsonArray();
    }
    // edit first and delete old skills
    businessResourceDAO.editOldResource(name, providerID, capacity, resourceID);

    // then insert list of new skills
    for (JsonElement skillJson : listJsonOfSkill) {
        int skillID = skillJson.getAsInt();
        businessResourceDAO.assignNewSkillToResource(resourceID, skillID);
    }
    return "{\"result\": \"success\"}";
}

From source file:Implement.Service.ProviderServiceImpl.java

@Override
public String editTrippSource(String data, int providerID) {
    JsonObject dataJson = gson.fromJson(data, JsonObject.class);
    int trippSourceID = dataJson.get("trippSourceID").getAsInt();
    Integer packageID;/*from  www.  j a  v  a  2  s  . c o  m*/
    try {
        packageID = dataJson.get("packageID").getAsInt();
    } catch (Exception e) {
        packageID = null;
    }

    Integer temporaryPackageID;
    try {
        temporaryPackageID = dataJson.get("temporaryPackageID").getAsInt();
    } catch (Exception e) {
        temporaryPackageID = null;
    }
    int noUnits = dataJson.get("noUnits").getAsInt();
    Integer hours;
    try {
        hours = dataJson.get("hours").getAsInt();
    } catch (Exception e) {
        hours = null;
    }
    Integer minutes;
    try {
        minutes = dataJson.get("minutes").getAsInt();
    } catch (Exception e) {
        minutes = null;
    }
    Integer days;
    try {
        days = dataJson.get("days").getAsInt();
    } catch (Exception e) {
        days = null;
    }
    JsonArray listJsonOfSkill;
    try {
        listJsonOfSkill = dataJson.get("skills").getAsJsonArray();
    } catch (Exception e) {
        listJsonOfSkill = new JsonArray();
    }
    // edit first and delete old skills
    trippSourceDAO.editTrippSource(packageID, temporaryPackageID, providerID, trippSourceID, noUnits, hours,
            minutes, days);

    // then insert list of new skills
    for (JsonElement skillJson : listJsonOfSkill) {
        int skillID = skillJson.getAsInt();
        trippSourceDAO.assignNewSkillToTrippSource(packageID, temporaryPackageID, providerID, trippSourceID,
                skillID);
    }
    return "{\"result\": \"success\"}";
}

From source file:io.flutter.inspector.DiagnosticsPathNode.java

License:Open Source License

/**
 * Returns the index of the child that continues the path if any.
 *///from   w  ww  .j av  a  2 s.  co m
public int getChildIndex() {
    final JsonElement childIndex = json.get("childIndex");
    if (childIndex.isJsonNull()) {
        return -1;
    }
    return childIndex.getAsInt();
}

From source file:io.flutter.logging.FlutterLogEntryParser.java

License:Open Source License

private static FlutterLogEntry parseLoggingEvent(@NotNull Event event) {
    // TODO(pq): parse more robustly; consider more properties (level, error, stackTrace)
    final JsonObject json = event.getJson();
    final JsonObject logRecord = json.get("logRecord").getAsJsonObject();
    final Instance message = new Instance(logRecord.getAsJsonObject().get("message").getAsJsonObject());

    String category = LOG_CATEGORY;
    final JsonObject loggerName = logRecord.getAsJsonObject().get("loggerName").getAsJsonObject();
    if (loggerName != null) {
        final String str = new Instance(loggerName).getValueAsString();
        if (str != null && !str.isEmpty()) {
            category = str;// ww w  .ja v a  2 s.  c o m
        }
    }

    int level = FlutterLogEntry.UNDEFINED_LEVEL.value;
    final JsonElement levelElement = logRecord.getAsJsonObject().get("level");
    if (levelElement instanceof JsonPrimitive) {
        final int setLevel = levelElement.getAsInt();
        // only set level if defined
        if (setLevel > 0) {
            level = setLevel;
        }
    }

    // TODO: If message.getValueAsStringIsTruncated() is true, we'll need to retrieve the full string
    // value and update this entry after creation.
    return new FlutterLogEntry(timestamp(event), category, level, message.getValueAsString());
}

From source file:io.flutter.utils.JsonUtils.java

License:Open Source License

public static int getIntMember(@NotNull JsonObject json, @NotNull String memberName) {
    if (!json.has(memberName))
        return -1;

    final JsonElement value = json.get(memberName);
    return value instanceof JsonNull ? -1 : value.getAsInt();
}

From source file:io.imoji.sdk.objects.json.ImojiDeserializer.java

License:Open Source License

@Override
public Imoji deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject root = json.getAsJsonObject();

    String identifier;//from   w  w  w  . j av  a  2s  .c om
    if (root.has("imojiId")) {
        identifier = root.get("imojiId").getAsString();
    } else {
        identifier = root.get("id").getAsString();
    }

    JsonArray tagsArray = root.getAsJsonArray("tags");
    List<String> tags;

    if (tagsArray != null && tagsArray.size() > 0) {
        tags = new ArrayList<>(tagsArray.size());
        for (JsonElement tag : tagsArray) {
            tags.add(tag.getAsString());
        }
    } else {
        tags = Collections.emptyList();
    }

    Imoji.LicenseStyle licenseStyle = Imoji.LicenseStyle.NonCommercial;
    if (root.has("licenseStyle")) {
        String licenseStyleStr = root.get("licenseStyle").getAsString();
        if ("commercialPrint".equals((licenseStyleStr))) {
            licenseStyle = Imoji.LicenseStyle.CommercialPrint;
        }
    }

    Map<RenderingOptions, Imoji.Metadata> metadataMap = new HashMap<>();

    JsonObject images = root.get("images").getAsJsonObject();

    for (RenderingOptions.BorderStyle borderStyle : RenderingOptions.BorderStyle.values()) {
        for (RenderingOptions.ImageFormat imageFormat : RenderingOptions.ImageFormat.values()) {
            for (RenderingOptions.Size size : RenderingOptions.Size.values()) {
                JsonObject subDocument;

                switch (borderStyle) {
                case Sticker:
                    subDocument = images.getAsJsonObject("bordered");
                    break;
                case None:
                    if (imageFormat == RenderingOptions.ImageFormat.AnimatedGif
                            || imageFormat == RenderingOptions.ImageFormat.AnimatedWebp) {
                        subDocument = images.getAsJsonObject("animated");
                    } else {
                        subDocument = images.getAsJsonObject("unbordered");
                    }

                    break;
                default:
                    subDocument = null;
                    break;
                }

                if (subDocument == null) {
                    continue;
                }

                switch (imageFormat) {
                case Png:
                    subDocument = subDocument.getAsJsonObject("png");
                    break;
                case WebP:
                case AnimatedWebp:
                    subDocument = subDocument.getAsJsonObject("webp");
                    break;
                case AnimatedGif:
                    subDocument = subDocument.getAsJsonObject("gif");
                    break;
                }

                if (subDocument == null) {
                    continue;
                }

                switch (size) {
                case Thumbnail:
                    subDocument = subDocument.getAsJsonObject("150");
                    break;
                case FullResolution:
                    subDocument = subDocument.getAsJsonObject("1200");
                    break;
                case Resolution320:
                    subDocument = subDocument.getAsJsonObject("320");
                    break;
                case Resolution512:
                    subDocument = subDocument.getAsJsonObject("512");
                    break;
                }

                if (subDocument != null) {
                    Uri url = Uri.parse(subDocument.get("url").getAsString());
                    Integer width = null, height = null, fileSize = null;

                    if (subDocument.has("width")) {
                        JsonElement widthObj = subDocument.get("width");
                        if (widthObj.isJsonPrimitive()) {
                            width = widthObj.getAsInt();
                        }
                    }

                    if (subDocument.has("height")) {
                        JsonElement heightObj = subDocument.get("height");
                        if (heightObj.isJsonPrimitive()) {
                            height = heightObj.getAsInt();
                        }
                    }

                    if (subDocument.has("fileSize")) {
                        JsonElement fileSizeObj = subDocument.get("fileSize");
                        if (fileSizeObj.isJsonPrimitive()) {
                            fileSize = fileSizeObj.getAsInt();
                        }
                    }

                    metadataMap.put(new RenderingOptions(borderStyle, imageFormat, size),
                            new Imoji.Metadata(url, width, height, fileSize));
                }
            }
        }
    }

    return new Imoji(identifier, tags, metadataMap, licenseStyle);
}