Search in sources :

Example 81 with JsonNode

use of org.codehaus.jackson.JsonNode in project dianping-open-sdk by dianping.

the class DemoDealAPI method requestCities.

/**
     * 功能描述: 获取所有支持团购的城市
     * <p>
     * 前置条件:
     * <p>
     * 方法影响:
     * <p>
     * Author xiaopeng.li, Aug 12, 2013
     * 
     * @since dianping-java-samples 1.0
     * @param appKey
     * @param secret
     * @return
     * @throws IOException
     * @throws JsonProcessingException
     */
public static List<String> requestCities(String appKey, String secret) throws JsonProcessingException, IOException {
    String apiUrl = "http://api.dianping.com/v1/metadata/get_cities_with_deals";
    String jsonResult = DemoApiTool.requestApi(apiUrl, appKey, secret, new HashMap<String, String>());
    JsonNode tree = JSON_MAPPER.readTree(jsonResult);
    JsonNode citiesNode = tree.get("cities");
    List<String> cities = new ArrayList<String>();
    for (Iterator<JsonNode> iterator = citiesNode.getElements(); iterator.hasNext(); ) {
        JsonNode node = iterator.next();
        cities.add(node.getTextValue());
    }
    return cities;
}
Also used : ArrayList(java.util.ArrayList) JsonNode(org.codehaus.jackson.JsonNode)

Example 82 with JsonNode

use of org.codehaus.jackson.JsonNode in project sling by apache.

the class SlingClientDoGetJsonTest method testDoGetJsonInfinity.

@Test
public void testDoGetJsonInfinity() throws Exception {
    SlingClient c = new SlingClient(httpServer.getURI(), "user", "pass");
    JsonNode res = c.doGetJson(GET_JSON_PATH, -1, 200);
    assertEquals("admin-infinity", res.get("jcr:createdBy").getTextValue());
}
Also used : JsonNode(org.codehaus.jackson.JsonNode) Test(org.junit.Test)

Example 83 with JsonNode

use of org.codehaus.jackson.JsonNode in project oxTrust by GluuFederation.

the class SearchResourcesWebService method computeResults.

/**
 * Here we reuse every single POST search found in other web services, but handle serialization differently to a more
 * manual approach for performance reasons. In the end, this saves us 3 deserializations and 3 serializations of
 * multiple results packs.
 * Result set as a whole will not be sorted by sortBy param but every group of resources (by resource type) will be
 * sorted as such
 * @param searchRequest
 * @param resources
 * @return
 */
private Pair<Integer, Integer> computeResults(SearchRequest searchRequest, List<JsonNode> resources) throws Exception {
    int i;
    int totalInPage = 0, totalResults = 0, skip = 0;
    boolean resultsAvailable = false;
    Integer startIndex_ = searchRequest.getStartIndex();
    JsonNode tree = null;
    // Move forward to skip the searches that might have no results and find the first one starting at index = searchRequest.getStartIndex()
    for (i = 0; i < NUM_RESOURCE_TYPES && !resultsAvailable; i++) {
        tree = getListResponseTree(i, searchRequest);
        if (tree != null) {
            totalResults += tree.get("totalResults").asInt();
            if (totalResults > 0) {
                if (totalResults >= startIndex_) {
                    // when null, it means searchRequest.getCount() was zero or empty page
                    resultsAvailable = tree.get("itemsPerPage") != null;
                    if (searchRequest.getStartIndex() == 1)
                        skip = startIndex_ - (totalResults - tree.get("totalResults").asInt()) - 1;
                }
                // Adjust startindex of subsequent searches to 1
                searchRequest.setStartIndex(1);
            }
        }
    }
    if (resultsAvailable) {
        // Accumulate till we have searchRequest.getCount() results or exhaust data
        Iterator<JsonNode> iterator = tree.get("Resources").getElements();
        while (iterator.hasNext() && totalInPage < searchRequest.getCount()) {
            if (skip == 0) {
                totalInPage++;
                resources.add(iterator.next());
            } else {
                skip--;
                iterator.next();
            }
        }
        while (i < NUM_RESOURCE_TYPES && totalInPage < searchRequest.getCount()) {
            resultsAvailable = false;
            tree = getListResponseTree(i, searchRequest);
            if (tree != null) {
                totalResults += tree.get("totalResults").asInt();
                if (tree.get("totalResults").asInt() > 0)
                    resultsAvailable = tree.get("itemsPerPage") != null;
            }
            if (resultsAvailable) {
                for (iterator = tree.get("Resources").getElements(); iterator.hasNext() && totalInPage < searchRequest.getCount(); totalInPage++) resources.add(iterator.next());
            }
            i++;
        }
        // Continue the remainder of searches to just compute final value for totalResults
        while (i < NUM_RESOURCE_TYPES) {
            tree = getListResponseTree(i, searchRequest);
            if (tree != null)
                totalResults += tree.get("totalResults").asInt();
            i++;
        }
    }
    // Revert startIndex to original
    searchRequest.setStartIndex(startIndex_);
    return new Pair<Integer, Integer>(totalInPage, totalResults);
}
Also used : JsonNode(org.codehaus.jackson.JsonNode) Pair(org.xdi.util.Pair)

Example 84 with JsonNode

use of org.codehaus.jackson.JsonNode in project oxTrust by GluuFederation.

the class ListResponseJsonSerializer method serialize.

@Override
public void serialize(ListResponse listResponse, JsonGenerator jGen, SerializerProvider provider) throws IOException {
    try {
        jGen.writeStartObject();
        jGen.writeArrayFieldStart("schemas");
        for (String schema : listResponse.getSchemas()) jGen.writeString(schema);
        jGen.writeEndArray();
        jGen.writeNumberField("totalResults", listResponse.getTotalResults());
        if (!skipResults) {
            if (listResponse.getItemsPerPage() > 0) {
                // these two bits are "REQUIRED when partial results are returned due to pagination." (section 3.4.2 RFC 7644)
                jGen.writeNumberField("startIndex", listResponse.getStartIndex());
                jGen.writeNumberField("itemsPerPage", listResponse.getItemsPerPage());
            }
            // Section 3.4.2 RFC 7644: Resources [...] REQUIRED if "totalResults" is non-zero
            if (listResponse.getTotalResults() > 0) {
                jGen.writeArrayFieldStart("Resources");
                if (listResponse.getResources().size() > 0)
                    for (BaseScimResource resource : listResponse.getResources()) {
                        JsonNode jsonResource = mapper.readTree(resourceSerializer.serialize(resource, attributes, excludeAttributes));
                        jGen.writeTree(jsonResource);
                    }
                else if (jsonResources != null)
                    for (JsonNode node : jsonResources) jGen.writeTree(node);
                jGen.writeEndArray();
            }
        }
        jGen.writeEndObject();
    } catch (Exception e) {
        throw new IOException(e);
    }
}
Also used : BaseScimResource(org.gluu.oxtrust.model.scim2.BaseScimResource) JsonNode(org.codehaus.jackson.JsonNode) IOException(java.io.IOException) IOException(java.io.IOException)

Example 85 with JsonNode

use of org.codehaus.jackson.JsonNode in project apex-malhar by apache.

the class CouchDBPOJOInputOperator method getTuple.

@Override
@SuppressWarnings("unchecked")
public Object getTuple(Row value) throws IOException {
    Object obj;
    try {
        obj = objectClass.newInstance();
    } catch (InstantiationException ex) {
        throw new RuntimeException(ex);
    } catch (IllegalAccessException ex) {
        throw new RuntimeException(ex);
    }
    if (setterDocId != null) {
        setterDocId.set(obj, value.getId());
    }
    JsonNode val = value.getValueAsNode();
    for (int i = 0; i < setterDoc.size(); i++) {
        Class<?> type = fieldType.get(i);
        if (type.isPrimitive()) {
            if (type == int.class) {
                ((SetterInt) setterDoc.get(i)).set(obj, val.get(columns.get(i)).getIntValue());
            } else if (type == boolean.class) {
                ((SetterBoolean) setterDoc.get(i)).set(obj, val.get(columns.get(i)).getBooleanValue());
            } else if (type == long.class) {
                ((SetterLong) setterDoc.get(i)).set(obj, val.get(columns.get(i)).getLongValue());
            } else if (type == double.class) {
                ((SetterDouble) setterDoc.get(i)).set(obj, val.get(columns.get(i)).getDoubleValue());
            } else {
                throw new RuntimeException("Type is not supported");
            }
        } else {
            ((Setter<Object, Object>) setterDoc.get(i)).set(obj, mapper.readValue(val.get(columns.get(i)), type));
        }
    }
    return obj;
}
Also used : SetterLong(org.apache.apex.malhar.lib.util.PojoUtils.SetterLong) Setter(org.apache.apex.malhar.lib.util.PojoUtils.Setter) JsonNode(org.codehaus.jackson.JsonNode) SetterInt(org.apache.apex.malhar.lib.util.PojoUtils.SetterInt)

Aggregations

JsonNode (org.codehaus.jackson.JsonNode)200 Test (org.junit.Test)55 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)50 IOException (java.io.IOException)40 ArrayList (java.util.ArrayList)20 HashMap (java.util.HashMap)20 HTTP (org.neo4j.test.server.HTTP)18 Resource (com.netflix.simianarmy.Resource)17 AWSResource (com.netflix.simianarmy.aws.AWSResource)17 ObjectNode (org.codehaus.jackson.node.ObjectNode)15 Response (org.neo4j.test.server.HTTP.Response)12 Map (java.util.Map)9 ArrayNode (org.codehaus.jackson.node.ArrayNode)9 RpcException (cz.metacentrum.perun.core.api.exceptions.RpcException)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 JsonParseException (org.neo4j.server.rest.domain.JsonParseException)7 Date (java.util.Date)6 List (java.util.List)6 Description (org.hamcrest.Description)6 TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)6