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;
}
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());
}
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);
}
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);
}
}
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;
}
Aggregations