All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.archive.format.json.JSONView Maven / Gradle / Ivy

The newest version!
package org.archive.format.json;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;

/**
 * 
 * Class which provides a column-oriented view of a JSON structure.
 * 
 * An instance is constructed with an array of field specifiers, each of which
 * declares the source path to one column of output.
 * 
 * @author brad
 *
 */
public class JSONView {
	private static final Logger LOG =
		Logger.getLogger(JSONView.class.getName());
	
	ArrayList pathSpecs;
	CrossProductOfLists crosser;

	public JSONView(String... pathSpecs) {
		this.pathSpecs = new ArrayList(pathSpecs.length);
		if(LOG.isLoggable(Level.INFO)) {
			LOG.info(String.format("Creating JSONView with(%s)",
					StringUtils.join(pathSpecs,",")));
		}
		for(String pathSpec : pathSpecs) {
			this.pathSpecs.add(JSONPathSpecFactory.get(pathSpec));
		}
		crosser = new CrossProductOfLists();
	}
	public List> apply(JSONObject json) {
		ArrayList>> results =
			new ArrayList>>(pathSpecs.size());
		
		for(JSONPathSpec pathSpec : pathSpecs) {
			List> result = pathSpec.extract(json);
			if(result == null) {
//				ArrayList tmp = new ArrayList();
				result = new ArrayList>();
			}
			results.add(result);
		}
		return crosser.crossProduct(results);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy