Package com.restfb.json

Examples of com.restfb.json.JsonObject


        JsonArray resultByMetric = raw.get(date);

        // [{"metric":"page_active_users","value":582},
        // {"metric":"page_tab_views_login_top_unique","value":{"wall":12,"app_4949752878":1}}]
        for (int resultIndex = 0; resultIndex < resultByMetric.length(); resultIndex++) {
          JsonObject metricResult = resultByMetric.getJsonObject(resultIndex);

          try {
            String metricName = metricResult.getString("metric");
            Object metricValue = metricResult.get("value");

            // store into output collection
            SortedMap<Date, Object> resultByDate = result.get(metricName);
            if (resultByDate == null) {
              resultByDate = new TreeMap<Date, Object>();
View Full Code Here


    SortedMap<Date, JsonArray> result = new TreeMap<Date, JsonArray>();

    if (!fqlByQueryIndex.isEmpty()) {
      // request the client sends all the queries in fqlByQueryIndex to Facebook
      // in one go and have the raw JsonObject be returned
      JsonObject response = facebookClient.executeFqlMultiquery(fqlByQueryIndex, JsonObject.class);

      // transform the response into a Map
      for (Iterator<?> it = response.keys(); it.hasNext();) {
        String key = (String) it.next();
        JsonArray innerResult = (JsonArray) response.get(key);

        try {
          // resolve the map key back into a date
          int queryIndex = Integer.parseInt(key);
          Date d = datesByQueryIndex.get(queryIndex);
View Full Code Here

      int subStrEnd = Math.min(50, json.length());
      if (!json.substring(0, subStrEnd).contains("\"error\"")) {
        return; // no need to parse json
      }

      JsonObject errorObject = null;

      // We need to swallow exceptions here because it's possible to get a legit
      // Facebook response that contains illegal JSON (e.g.
      // users.getLoggedInUser returning 1240077) - we're only interested in
      // whether or not there's an error_code field present.
      try {
        errorObject = new JsonObject(json);
      } catch (JsonException e) {}

      if (errorObject == null || !errorObject.has(BATCH_ERROR_ATTRIBUTE_NAME)
          || !errorObject.has(BATCH_ERROR_DESCRIPTION_ATTRIBUTE_NAME))
        return;

      throw legacyFacebookExceptionMapper.exceptionForTypeAndMessage(errorObject.getInt(BATCH_ERROR_ATTRIBUTE_NAME),
        null, httpStatusCode, null, errorObject.getString(BATCH_ERROR_DESCRIPTION_ATTRIBUTE_NAME));
    } catch (JsonException e) {
      throw new FacebookJsonMappingException("Unable to process the Facebook API response", e);
    }
  }
View Full Code Here

    List<T> data = new ArrayList<T>();

    if (json == null)
      throw new FacebookJsonMappingException("You must supply non-null connection JSON.");

    JsonObject jsonObject = null;

    try {
      jsonObject = new JsonObject(json);
    } catch (JsonException e) {
      throw new FacebookJsonMappingException("The connection JSON you provided was invalid: " + json, e);
    }

    // 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) : facebookClient.getJsonMapper()
        .toJavaObject(jsonData.get(i).toString(), connectionType));

    // Pull out paging info, if present
    if (jsonObject.has("paging")) {
      JsonObject jsonPaging = jsonObject.getJsonObject("paging");
      previousPageUrl = jsonPaging.has("previous") ? jsonPaging.getString("previous") : null;
      nextPageUrl = jsonPaging.has("next") ? jsonPaging.getString("next") : null;
      if (null != previousPageUrl && previousPageUrl.startsWith("http://")) {
        previousPageUrl = previousPageUrl.replaceFirst("http://", "https://");
      }
      if (null != nextPageUrl && nextPageUrl.startsWith("http://")) {
        nextPageUrl = nextPageUrl.replaceFirst("http://", "https://");
      }
    } else {
      previousPageUrl = null;
      nextPageUrl = null;
    }
   
    if (jsonObject.has("summary")) {
        JsonObject jsonSummary = jsonObject.getJsonObject("summary");
        totalCount = jsonSummary.has("total_count") ? jsonSummary.getLong("total_count") : null;
    } else {
        totalCount = null;
    }

    this.data = unmodifiableList(data);
View Full Code Here

    metricResult = results.get("page_fans_gender");
    assertEquals(1, metricResult.size());
    Object metricValue = metricResult.get(d20101205_0000pst);
    Assert.assertTrue(metricValue instanceof JsonObject);
    JsonObject o = (JsonObject) metricValue;
    assertEquals(58, o.getInt("U"));
    assertEquals(1656, o.getInt("F"));
    assertEquals(2014, o.getInt("M"));
  }
View Full Code Here

    assertEquals(582, itValues.next());
    assertEquals(125, itValues.next());

    metricResult = results.get("page_tab_views_login_top_unique");
    assertEquals(4, metricResult.size());
    JsonObject o = (JsonObject) metricResult.get(d20030629_0000pst);
    assertEquals(3, o.length());
    assertEquals(2, o.getInt("photos"));
    assertEquals(3, o.getInt("app_4949752878"));
    assertEquals(30, o.getInt("wall"));

    o = (JsonObject) metricResult.get(d20030630_0000pst);
    assertEquals(4, o.length());
    assertEquals(1, o.getInt("photos"));
    assertEquals(1, o.getInt("app_4949752878"));
    assertEquals(2, o.getInt("app_2373072738"));
    assertEquals(23, o.getInt("wall"));

    o = (JsonObject) metricResult.get(d20030701_0000pst);
    assertEquals(2, o.length());
    assertEquals(1, o.getInt("app_4949752878"));
    assertEquals(12, o.getInt("wall"));

    o = (JsonObject) metricResult.get(d20030702_0000pst);
    assertEquals(2, o.length());
    assertEquals(1, o.getInt("photos"));
    assertEquals(11, o.getInt("wall"));
  }
View Full Code Here

      int subStrEnd = Math.min(50, json.length());
      if (!json.substring(0, subStrEnd).contains("\"error\"")) {
        return; // no need to parse json
      }

      JsonObject errorObject = null;

      // We need to swallow exceptions here because it's possible to get a legit
      // Facebook response that contains illegal JSON (e.g.
      // users.getLoggedInUser returning 1240077) - we're only interested in
      // whether or not there's an error_code field present.
      try {
        errorObject = new JsonObject(json);
      } catch (JsonException e) {}

      if (errorObject == null || !errorObject.has(LEGACY_ERROR_CODE_ATTRIBUTE_NAME))
        return;

      throw legacyFacebookExceptionMapper.exceptionForTypeAndMessage(
        errorObject.getInt(LEGACY_ERROR_CODE_ATTRIBUTE_NAME), null, httpStatusCode, null,
        errorObject.getString(LEGACY_ERROR_MSG_ATTRIBUTE_NAME));
    } catch (JsonException e) {
      throw new FacebookJsonMappingException("Unable to process the Facebook API response", e);
    }
  }
View Full Code Here

    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();
  }
View Full Code Here

    String responseString = makeRequest(object, true, true, null, parameters);
    String cmpString;

    try {
      JsonObject jObj = new JsonObject(responseString);
      cmpString = jObj.getString("success");
    } catch (JsonException jex) {
      cmpString = responseString;
    }

    return "true".equals(cmpString);
View Full Code Here

    try {
      JsonArray jsonArray =
          new JsonArray(makeRequest("fql.multiquery", false, false, null,
            parametersWithAdditionalParameter(Parameter.with(QUERIES_PARAM_NAME, queriesToJson(queries)), parameters)));

      JsonObject normalizedJson = new JsonObject();

      for (int i = 0; i < jsonArray.length(); i++) {
        JsonObject jsonObject = jsonArray.getJsonObject(i);

        // For empty resultsets, Facebook will return an empty object instead of
        // an empty list. Hack around that here.
        JsonArray resultsArray =
            jsonObject.get("fql_result_set") instanceof JsonArray ? jsonObject.getJsonArray("fql_result_set")
                : new JsonArray();

        normalizedJson.put(jsonObject.getString("name"), resultsArray);
      }

      return objectType.equals(JsonObject.class) ? (T) normalizedJson : jsonMapper.toJavaObject(
        normalizedJson.toString(), objectType);
    } catch (JsonException e) {
View Full Code Here

TOP

Related Classes of com.restfb.json.JsonObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.