Java Code Examples for com.google.gson.JsonPrimitive#getAsInt()
The following examples show how to use
com.google.gson.JsonPrimitive#getAsInt() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LongPollQueryBuilder.java From vk-java-sdk with MIT License | 6 votes |
@Override public R execute() throws ApiException, ClientException { String textResponse = executeAsString(); JsonReader jsonReader = new JsonReader(new StringReader(textResponse)); JsonObject json = (JsonObject) new JsonParser().parse(jsonReader); if (json.has(FAILED_CODE)) { JsonPrimitive failedParam = json.getAsJsonPrimitive(FAILED_CODE); int code = failedParam.getAsInt(); switch (code) { case INCORRECT_TS_VALUE_ERROR_CODE: int ts = json.getAsJsonPrimitive("ts").getAsInt(); throw new LongPollServerTsException("\'ts\' value is incorrect, minimal value is 1, maximal value is " + ts); case TOKEN_EXPIRED_ERROR_CODE: throw new LongPollServerKeyExpiredException("Try to generate a new key."); default: throw new ClientException("Unknown LongPollServer exception, something went wrong."); } } try { return getGson().fromJson(json, getResponseClass()); } catch (JsonSyntaxException e) { LOG.error("Invalid JSON: " + textResponse, e); throw new ClientException("Can't parse json response"); } }
Example 2
Source File: JSONRequestParser.java From carbon-identity-framework with Apache License 2.0 | 6 votes |
/** * Converts a given <code>{@link JsonElement}</code> to a <code>String</code> DataType * Predicted based on XACML 3.0 JSON profile * * @param element * @return */ private static String jsonElementToDataType(JsonPrimitive element) { if (element.isString()) { return EntitlementEndpointConstants.ATTRIBUTE_DATA_TYPE_STRING; } else if (element.isBoolean()) { return EntitlementEndpointConstants.ATTRIBUTE_DATA_TYPE_BOOLEAN; } else if (element.isNumber()) { double n1 = element.getAsDouble(); int n2 = element.getAsInt(); if (Math.ceil(n1) == n2) { return EntitlementEndpointConstants.ATTRIBUTE_DATA_TYPE_INTEGER; } else { return EntitlementEndpointConstants.ATTRIBUTE_DATA_TYPE_DOUBLE; } } return null; }
Example 3
Source File: GsonBigQueryIoIntegrationTest.java From hadoop-connectors with Apache License 2.0 | 6 votes |
@Override protected Map<String, Object> readRecord(RecordReader<?, JsonObject> recordReader) throws Exception { Map<String, Object> result = new HashMap<>(); JsonObject currentValue = recordReader.getCurrentValue(); for (Map.Entry<String, JsonElement> entry : currentValue.entrySet()) { String key = entry.getKey(); JsonPrimitive primitiveValue = entry.getValue().getAsJsonPrimitive(); Object value; if (COMPANY_NAME_FIELD.getName().equals(key)) { value = primitiveValue.getAsString(); } else if (MARKET_CAP_FIELD.getName().equals(key)) { value = primitiveValue.getAsInt(); } else { throw new IllegalStateException("Cannot handle key " + key); } result.put(key, value); } return result; }
Example 4
Source File: BetterJsonObject.java From Hyperium with GNU Lesser General Public License v3.0 | 5 votes |
/** * The optional int method, returns the default value if * the key is null, empty or the data does not contain * the key. This will also return the default value if * the data value is not a number * * @param key the key the value will be loaded from * @return the value in the json data set or the default if the key cannot be found */ public int optInt(String key, int value) { if (key == null || key.isEmpty() || !has(key)) return value; JsonPrimitive primitive = asPrimitive(get(key)); try { if (primitive != null && primitive.isNumber()) return primitive.getAsInt(); } catch (NumberFormatException ignored) { } return value; }
Example 5
Source File: MCRClassificationEditorResource.java From mycore with GNU General Public License v3.0 | 5 votes |
@Override public int compare(JsonElement jsonElement1, JsonElement jsonElement2) { if (!jsonElement1.isJsonObject()) { return 1; } if (!jsonElement2.isJsonObject()) { return -1; } // compare level first JsonPrimitive depthLevel1 = jsonElement1.getAsJsonObject().getAsJsonPrimitive("depthLevel"); JsonPrimitive depthLevel2 = jsonElement2.getAsJsonObject().getAsJsonPrimitive("depthLevel"); if (depthLevel1 == null) { return 1; } if (depthLevel2 == null) { return -1; } if (depthLevel1.getAsInt() != depthLevel2.getAsInt()) { return Integer.compare(depthLevel1.getAsInt(), depthLevel2.getAsInt()); } // compare index JsonPrimitive index1 = jsonElement1.getAsJsonObject().getAsJsonPrimitive("index"); JsonPrimitive index2 = jsonElement2.getAsJsonObject().getAsJsonPrimitive("index"); if (index1 == null) { return 1; } if (index2 == null) { return -1; } return Integer.compare(index1.getAsInt(), index2.getAsInt()); }
Example 6
Source File: NormalizedNodeToJsonStreamTest.java From yangtools with Eclipse Public License 1.0 | 5 votes |
@Test public void leafNodeInContainer() throws IOException { final String jsonOutput = normalizedNodeToJsonStreamTransformation( TestingNormalizedNodeStructuresCreator.leafNodeInContainer()); final JsonObject cont1 = resolveCont1(jsonOutput); assertNotNull(cont1); final JsonPrimitive lf11 = childPrimitive(cont1, "complexjson:lf11", "lf11"); assertNotNull(lf11); final int asInt = lf11.getAsInt(); assertEquals(453, asInt); }
Example 7
Source File: TaskTypeGsonAdapter.java From YiBo with Apache License 2.0 | 5 votes |
@Override public TaskType deserialize(JsonElement json, final Type type, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonNull()) { return null; } if (!json.isJsonPrimitive()) { throw new JsonParseException("it' not json primitive"); } final JsonPrimitive primitive = (JsonPrimitive)json; int taskTypeNo = primitive.getAsInt(); return TaskType.getTaskType(taskTypeNo); }
Example 8
Source File: Core.java From ice with Eclipse Public License 1.0 | 4 votes |
/** * This private operation creates an instance of the Message class from a * string using a JSON parser. * * This operation is synchronized so that the core can't be overloaded. * * @param messageString * The original message, as a string * @return list list of built messages. */ private ArrayList<Message> buildMessagesFromString(String messageString) { // Create the ArrayList of messages ArrayList<Message> messages = new ArrayList<>(); // Create the parser and gson utility JsonParser parser = new JsonParser(); GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); // Catch any exceptions and return the empty list try { // Make the string a json string JsonElement messageJson = parser.parse(messageString); JsonObject messageJsonObject = messageJson.getAsJsonObject(); // Get the Item id from the json JsonPrimitive itemIdJson = messageJsonObject.getAsJsonPrimitive("item_id"); int itemId = itemIdJson.getAsInt(); // Get the array of posts from the message JsonArray jsonMessagesList = messageJsonObject.getAsJsonArray("posts"); // Load the list for (int i = 0; i < jsonMessagesList.size(); i++) { // Get the message as a json element JsonElement jsonMessage = jsonMessagesList.get(i); // Marshal it into a message Message tmpMessage = gson.fromJson(jsonMessage, Message.class); // Set the item id if (tmpMessage != null) { tmpMessage.setItemId(itemId); // Put it in the list messages.add(tmpMessage); } } } catch (JsonParseException e) { // Log the message String err = "Core Message: " + "JSON parsing failed for message " + messageString; logger.error(getClass().getName() + " Exception!", e); logger.error(err); } return messages; }
Example 9
Source File: JsonUtils.java From yandex-money-sdk-java with MIT License | 2 votes |
/** * Gets nullable Integer from a JSON object. * * @param object json object * @param memberName member's name * @return {@link Integer} value */ public static Integer getInt(JsonObject object, String memberName) { JsonPrimitive primitive = getPrimitiveChecked(object, memberName); return primitive == null ? null : primitive.getAsInt(); }