URL url = new URL( urlBuilder.toString() );
JsonFactory f = new JsonFactory();
JsonParser jp = f.createJsonParser( url.openStream() );
Results results = new Results();
results.setProviderRefs( new ArrayList<ProviderRef>() );
results.setProviders( new ArrayList<Provider>() );
results.setFacets( new HashMap<String,Map<String,String>>() );
jp.nextToken(); // will return JsonToken.START_OBJECT (verify?)
//
// Get result metadata
//
while (jp.nextToken() != JsonToken.END_OBJECT) {
String fieldName = jp.getCurrentName();
jp.nextToken(); // move to value, or START_OBJECT/START_ARRAY
//
// Build the Results object
//
if( "total".equals( fieldName )) {
results.setTotal( jp.getIntValue() );
} else if( "page".equals( fieldName )) {
results.setPage( jp.getIntValue() );
} else if( "per_page".equals( fieldName )) {
results.setPerPage( jp.getIntValue() );
} else if( "query_time".equals( fieldName )) {
results.setQueryTime( jp.getDoubleValue() );
} else if ("facets".equals( fieldName )) { // contains an object
Map<String,String> facetMap = null;
while ( jp.nextToken() != JsonToken.END_OBJECT ) {
fieldName = jp.getCurrentName();
facetMap = new HashMap<String,String>();
results.getFacets().put( fieldName, facetMap );
JsonToken token = jp.nextToken();
while ( token != JsonToken.END_OBJECT ) {
if( token == JsonToken.START_OBJECT ) { // skip the "{" for each facet
token = jp.nextToken();
if( token == JsonToken.END_OBJECT ) {
token = jp.nextToken();
}
}
}
System.out.println( "token1 = ["+token+"]");
token = jp.nextToken();
// results.getFacets().get( fieldName ).put( value1, value2 ));
System.out.println( "token2 = ["+token+"]");
System.out.println( "token3 = ["+jp.getText()+"]");
}
} else if ("providerRefs".equals( fieldName )) {
getProviderRefs( jp, results );
} else {
// throw new IllegalStateException("Unrecognized field '"+fieldName+"'!");
System.out.println("Unrecognized field '"+fieldName+"'!");
}
}
//
// Get result providers
//
Provider provider = new Provider();
while (jp.nextToken() != JsonToken.END_OBJECT) {
String fieldName = jp.getCurrentName();
jp.nextToken(); // move to value, or START_OBJECT/START_ARRAY
//
// Build the Results object
//
if( "total".equals( fieldName )) {
results.setTotal( jp.getIntValue() );
} else if( "page".equals( fieldName )) {
results.setPage( jp.getIntValue() );
jp.close(); // ensure resources get cleaned up timely and properly
}
}