protected ExceptionInformation createFacebookResponseTypeAndMessageContainer(JsonObject errorObject, Integer httpStatusCode) { JsonObject innerErrorObject = errorObject.get(ERROR_ATTRIBUTE_NAME).asObject(); // If there's an Integer error code, pluck it out. Integer errorCode = innerErrorObject.get(ERROR_CODE_ATTRIBUTE_NAME) != null ? toInteger(innerErrorObject.get(ERROR_CODE_ATTRIBUTE_NAME).toString()) : null; Integer errorSubcode = innerErrorObject.get(ERROR_SUBCODE_ATTRIBUTE_NAME) != null ? toInteger(innerErrorObject.get(ERROR_SUBCODE_ATTRIBUTE_NAME).toString()) : null; return new ExceptionInformation(errorCode, errorSubcode, httpStatusCode, innerErrorObject.getString(ERROR_TYPE_ATTRIBUTE_NAME, null), innerErrorObject.get(ERROR_MESSAGE_ATTRIBUTE_NAME).asString(), innerErrorObject.getString(ERROR_USER_TITLE_ATTRIBUTE_NAME, null), innerErrorObject.getString(ERROR_USER_MSG_ATTRIBUTE_NAME, null), innerErrorObject.getBoolean(ERROR_IS_TRANSIENT_NAME, false), errorObject); }
/** * Put a key/value pair in the JsonObject, where the value will be a * JsonObject which is produced from a Map. * * @param key A key string. * @param value A Map value. * @return this. * @throws com.restfb.json.JsonException */ public JsonObject put(String key, Map<?, ?> value) throws JsonException { put(key, new JsonObject(value)); return this; }
/** * Get an optional int value associated with a key, or the default if there is * no such key or if the value is not a number. If the value is a string, an * attempt will be made to evaluate it as a number. * * @param key A key string. * @param defaultValue The default. * @return An object which is the value. */ public int optInt(String key, int defaultValue) { try { return getInt(key); } catch (Exception e) { return defaultValue; } }
/** * Get the long value associated with a key. If the number value is too long * for a long, it will be clipped. * * @param key A key string. * @return The long value. * @throws com.restfb.json.JsonException if the key is not found or if the value cannot be converted to a * long. */ public long getLong(String key) throws JsonException { Object o = get(key); return o instanceof Number ? ((Number) o).longValue() : (long) getDouble(key); }
String facebookFieldName) throws JsonException, FacebookJsonMappingException { Class<?> type = fieldWithAnnotation.getField().getType(); Object rawValue = jsonObject.get(facebookFieldName); return new Integer(jsonObject.getInt(facebookFieldName)); if (Boolean.class.equals(type) || Boolean.TYPE.equals(type)) return new Boolean(jsonObject.getBoolean(facebookFieldName)); if (Long.class.equals(type) || Long.TYPE.equals(type)) return new Long(jsonObject.getLong(facebookFieldName)); if (Double.class.equals(type) || Double.TYPE.equals(type)) return new Double(jsonObject.getDouble(facebookFieldName)); if (Float.class.equals(type) || Float.TYPE.equals(type)) return new BigDecimal(jsonObject.getString(facebookFieldName)).floatValue(); if (BigInteger.class.equals(type)) return new BigInteger(jsonObject.getString(facebookFieldName)); if (BigDecimal.class.equals(type)) return new BigDecimal(jsonObject.getString(facebookFieldName)); if (List.class.equals(type)) return toJavaList(rawValue.toString(), getFirstParameterizedTypeArgument(fieldWithAnnotation.getField())); + Comments.class.getSimpleName() + " instance..."); JsonObject workaroundJsonObject = new JsonObject(); workaroundJsonObject.put("count", 0); workaroundJsonObject.put("data", new JsonArray()); rawValueAsString = workaroundJsonObject.toString();
JsonObject normalizedJson = new JsonObject(); jsonObject.get("fql_result_set") instanceof JsonArray ? jsonObject.getJsonArray("fql_result_set") : new JsonArray(); normalizedJson.put(jsonObject.getString("name"), resultsArray); return jsonMapper.toJavaObject(normalizedJson.toString(), resultType);
@SuppressWarnings("unchecked") protected <T> Connection<T> mapToConnection(String connectionJson, Class<T> connectionType) { List<T> data = new ArrayList<T>(); String previous = null; String next = null; try { JsonObject jsonObject = new JsonObject(connectionJson); // Pull out data JsonArray jsonData = jsonObject.getJsonArray("data"); for (int i = 0; i < jsonData.length(); i++) data.add(connectionType.equals(JsonObject.class) ? (T) jsonData.get(i) : jsonMapper.toJavaObject(jsonData .get(i).toString(), connectionType)); // Pull out paging info, if present if (jsonObject.has("paging")) { JsonObject jsonPaging = jsonObject.getJsonObject("paging"); previous = jsonPaging.has("previous") ? jsonPaging.getString("previous") : null; next = jsonPaging.has("next") ? jsonPaging.getString("next") : null; } } catch (JsonException e) { throw new FacebookJsonMappingException("Unable to map connection JSON to Java objects", e); } return new Connection<T>(data, previous, next); }
errorObject = new JsonObject(json); } catch (JsonException e) { if (errorObject == null || !errorObject.has(LEGACY_ERROR_CODE_ATTRIBUTE_NAME)) return; throw new FacebookResponseStatusException(errorObject.getInt(LEGACY_ERROR_CODE_ATTRIBUTE_NAME), errorObject.getString(LEGACY_ERROR_MSG_ATTRIBUTE_NAME)); } catch (JsonException e) { throw new FacebookJsonMappingException("Unable to process the Facebook API response", e);
private List<MessageTag> createTags(String rawTags, JsonMapper jsonMapper) { try { return jsonMapper.toJavaList(rawTags, MessageTag.class); } catch (FacebookJsonMappingException je) { } // message tags not in Graph API 2.5 format, ignore this exception and try another way try { List<MessageTag> resultList = new ArrayList<>(); JsonObject rawMessageTagsObject = jsonMapper.toJavaObject(rawTags, JsonObject.class); for (String key : rawMessageTagsObject.names()) { String tagArrayString = rawMessageTagsObject.get(key).toString(); resultList.addAll(jsonMapper.toJavaList(tagArrayString, MessageTag.class)); } return resultList; } catch (FacebookJsonMappingException je) { return new ArrayList<>(); } }
/** * Get the string associated with a key. * * @param key A key string. * @return A string which is the value. * @throws com.restfb.json.JsonException if the key is not found. */ public String getString(String key) throws JsonException { return get(key).toString(); }
return numberToString((Number) value); return ((JsonObject) value).toString(indentFactor, indent); return new JsonObject((Map<?, ?>) value).toString(indentFactor, indent); return new JsonArray(value).toString(indentFactor, indent); return quote(value.toString());
@Override public void throwBatchFacebookResponseStatusExceptionIfNecessary(String json, Integer httpStatusCode) { try { skipResponseStatusExceptionParsing(json); JsonObject errorObject = silentlyCreateObjectFromString(json); if (errorObject == null || errorObject.get(BATCH_ERROR_ATTRIBUTE_NAME) == null || errorObject.get(BATCH_ERROR_DESCRIPTION_ATTRIBUTE_NAME) == null) return; ExceptionInformation container = new ExceptionInformation(errorObject.getInt(BATCH_ERROR_ATTRIBUTE_NAME, 0), httpStatusCode, errorObject.getString(BATCH_ERROR_DESCRIPTION_ATTRIBUTE_NAME, null), errorObject); throw graphFacebookExceptionMapper.exceptionForTypeAndMessage(container); } catch (ParseException e) { throw new FacebookJsonMappingException("Unable to process the Facebook API response", e); } catch (ResponseErrorJsonParsingException ex) { // do nothing here } }
JsonArray jsonData = jsonObject.get("data").asArray(); List<T> dataItem = new ArrayList<>(jsonData.size()); for (JsonValue jsonValue : jsonData) { if (jsonObject.get("paging") != null) { JsonObject jsonPaging = jsonObject.get("paging").asObject(); previousPageUrl = fixProtocol(jsonPaging.getString("previous", null)); nextPageUrl = fixProtocol(jsonPaging.getString("next", null)); if (jsonPaging.get("cursors") != null) { JsonObject jsonCursors = jsonPaging.get("cursors").asObject(); beforeCursor = jsonCursors.getString("before", null); afterCursor = jsonCursors.getString("after", null); if (jsonObject.get("summary") != null) { JsonObject jsonSummary = jsonObject.get("summary").asObject(); totalCount = jsonSummary.get("total_count") != null ? jsonSummary.getLong("total_count", 0L) : null; } else { totalCount = null;
String facebookFieldName) { Class<?> type = fieldWithAnnotation.getField().getType(); JsonValue rawValue = jsonObject.get(facebookFieldName); rawValueAsString, Comments.class.getSimpleName(), Comments.class.getSimpleName()); JsonObject workaroundJsonObject = new JsonObject(); workaroundJsonObject.add("total_count", 0); workaroundJsonObject.add("data", new JsonArray()); rawValueAsString = workaroundJsonObject.toString();
/** * Get the JsonObject value associated with a key. * * @param key A key string. * @return A JsonObject which is the value. * @throws com.restfb.json.JsonException if the key is not found or if the value is not a JsonObject. */ public JsonObject getJsonObject(String key) throws JsonException { Object o = get(key); if (o instanceof JsonObject) { return (JsonObject) o; } throw new JsonException("JsonObject[" + quote(key) + "] is not a JsonObject."); }
/** * Construct a JsonArray from a collection of beans. The collection should * have Java Beans. * * @throws JsonException If not an array. */ public JsonArray(Collection<?> collection, boolean includeSuperClass) { this.myArrayList = new ArrayList<Object>(); if (collection != null) { Iterator<?> iter = collection.iterator(); ; while (iter.hasNext()) { Object o = iter.next(); if (o instanceof Map<?, ?>) { this.myArrayList.add(new JsonObject((Map<?, ?>) o, includeSuperClass)); } else if (!JsonObject.isStandardProperty(o.getClass())) { this.myArrayList.add(new JsonObject(o, includeSuperClass)); } else { this.myArrayList.add(o); } } } }
/** * Gets the Facebook API error {@code fbtrace_id}. * * Internal support identifier. When reporting a bug related to a Graph API call, include the fbtrace_id to help us * find log data for debugging. * * @return the Facebook API error {@code fbtrace_id} */ public String getFbtraceId() { if (getRawErrorJson() != null && getRawErrorJson().get("error").isObject()) { JsonObject errorJson = getRawErrorJson().get("error").asObject(); return errorJson.getString("fbtrace_id", ""); } return ""; }
/** * Given a map of query names to queries, verify that it contains valid data * and convert it to a JSON object string. * * @param queries The query map to convert. * @return The {@code queries} in JSON string format. * @throws IllegalArgumentException If the provided {@code queries} are invalid. */ protected String queriesToJson(Map<String, String> queries) { verifyParameterPresence("queries", queries); if (queries.keySet().size() == 0) throw new IllegalArgumentException("You must specify at least one query."); JsonObject jsonObject = new JsonObject(); for (Entry<String, String> entry : queries.entrySet()) { if (isBlank(entry.getKey()) || isBlank(entry.getValue())) throw new IllegalArgumentException("Provided queries must have non-blank keys and values. " + "You provided: " + queries); try { jsonObject.put(trimToEmpty(entry.getKey()), trimToEmpty(entry.getValue())); } catch (JsonException e) { // Shouldn't happen unless bizarre input is provided throw new IllegalArgumentException("Unable to convert " + queries + " to JSON.", e); } } return jsonObject.toString(); }
public void setMaxCampaignDuration(JsonObject maxCampaignDuration) { if (maxCampaignDuration != null) { this.maxCampaignDuration = maxCampaignDuration.toString(); } else { this.maxCampaignDuration = null; } }