2010-10-20 22:45:52 +02:00
|
|
|
/*
|
|
|
|
|
2012-01-27 18:05:45 +01:00
|
|
|
Copyright 2010,2012 Google Inc.
|
2010-10-20 22:45:52 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are
|
|
|
|
met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the following disclaimer
|
|
|
|
in the documentation and/or other materials provided with the
|
|
|
|
distribution.
|
|
|
|
* Neither the name of Google Inc. nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived from
|
|
|
|
this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2010-09-28 00:53:17 +02:00
|
|
|
package com.google.refine.importers;
|
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
2010-09-28 00:53:17 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2011-08-02 05:34:47 +02:00
|
|
|
import java.io.Reader;
|
|
|
|
import java.util.List;
|
2010-09-28 00:53:17 +02:00
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
import org.codehaus.jackson.JsonFactory;
|
|
|
|
import org.codehaus.jackson.JsonParseException;
|
|
|
|
import org.codehaus.jackson.JsonParser;
|
|
|
|
import org.codehaus.jackson.JsonToken;
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONObject;
|
2010-09-28 00:53:17 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import com.google.refine.ProjectMetadata;
|
2011-08-02 05:34:47 +02:00
|
|
|
import com.google.refine.importers.tree.ImportColumnGroup;
|
|
|
|
import com.google.refine.importers.tree.TreeImportingParserBase;
|
|
|
|
import com.google.refine.importers.tree.TreeReader;
|
2011-10-07 21:06:53 +02:00
|
|
|
import com.google.refine.importers.tree.TreeReaderException;
|
2011-08-02 05:34:47 +02:00
|
|
|
import com.google.refine.importing.ImportingJob;
|
|
|
|
import com.google.refine.importing.ImportingUtilities;
|
2010-09-28 00:53:17 +02:00
|
|
|
import com.google.refine.model.Project;
|
2011-08-02 05:34:47 +02:00
|
|
|
import com.google.refine.util.JSONUtilities;
|
2010-09-28 00:53:17 +02:00
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
public class JsonImporter extends TreeImportingParserBase {
|
2011-08-22 23:47:15 +02:00
|
|
|
static final Logger logger = LoggerFactory.getLogger(JsonImporter.class);
|
|
|
|
|
2012-01-26 23:38:25 +01:00
|
|
|
public final static String ANONYMOUS = "_";
|
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
public JsonImporter() {
|
|
|
|
super(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static private class PreviewParsingState {
|
|
|
|
int tokenCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
final static private int PREVIEW_PARSING_LIMIT = 1000;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public JSONObject createParserUIInitializationData(
|
|
|
|
ImportingJob job, List<JSONObject> fileRecords, String format) {
|
|
|
|
JSONObject options = super.createParserUIInitializationData(job, fileRecords, format);
|
2011-08-18 19:39:18 +02:00
|
|
|
if (fileRecords.size() > 0) {
|
2011-08-02 05:34:47 +02:00
|
|
|
try {
|
2011-08-18 19:39:18 +02:00
|
|
|
JSONObject firstFileRecord = fileRecords.get(0);
|
|
|
|
File file = ImportingUtilities.getFile(job, firstFileRecord);
|
|
|
|
InputStream is = new FileInputStream(file);
|
|
|
|
try {
|
|
|
|
JsonFactory factory = new JsonFactory();
|
|
|
|
JsonParser parser = factory.createJsonParser(is);
|
|
|
|
|
|
|
|
PreviewParsingState state = new PreviewParsingState();
|
|
|
|
Object rootValue = parseForPreview(parser, state);
|
|
|
|
if (rootValue != null) {
|
|
|
|
JSONUtilities.safePut(options, "dom", rootValue);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
is.close();
|
2010-09-28 00:53:17 +02:00
|
|
|
}
|
2011-08-18 19:39:18 +02:00
|
|
|
} catch (IOException e) {
|
2011-08-22 23:47:15 +02:00
|
|
|
logger.error("Error generating parser UI initialization data for JSON file", e);
|
2010-09-28 00:53:17 +02:00
|
|
|
}
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
2010-09-28 00:53:17 +02:00
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
final static private Object parseForPreview(JsonParser parser, PreviewParsingState state, JsonToken token)
|
|
|
|
throws JsonParseException, IOException {
|
|
|
|
if (token != null) {
|
|
|
|
switch (token) {
|
|
|
|
case START_ARRAY:
|
|
|
|
return parseArrayForPreview(parser, state);
|
|
|
|
case START_OBJECT:
|
|
|
|
return parseObjectForPreview(parser, state);
|
|
|
|
case VALUE_STRING:
|
|
|
|
return parser.getText();
|
|
|
|
case VALUE_NUMBER_INT:
|
|
|
|
return Integer.valueOf(parser.getIntValue());
|
|
|
|
case VALUE_NUMBER_FLOAT:
|
|
|
|
return Float.valueOf(parser.getFloatValue());
|
|
|
|
case VALUE_TRUE:
|
|
|
|
return Boolean.TRUE;
|
|
|
|
case VALUE_FALSE:
|
|
|
|
return Boolean.FALSE;
|
|
|
|
case VALUE_NULL:
|
|
|
|
return null;
|
2012-07-12 03:55:11 +02:00
|
|
|
case END_ARRAY:
|
|
|
|
case END_OBJECT:
|
|
|
|
case FIELD_NAME:
|
|
|
|
case NOT_AVAILABLE:
|
|
|
|
case VALUE_EMBEDDED_OBJECT:
|
|
|
|
default:
|
|
|
|
break;
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
final static private Object parseForPreview(JsonParser parser, PreviewParsingState state) {
|
|
|
|
try {
|
|
|
|
JsonToken token = parser.nextToken();
|
|
|
|
state.tokenCount++;
|
|
|
|
return parseForPreview(parser, state, token);
|
2011-10-07 21:06:53 +02:00
|
|
|
} catch (IOException e) {
|
2011-08-02 05:34:47 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final static private JSONObject parseObjectForPreview(JsonParser parser, PreviewParsingState state) {
|
|
|
|
JSONObject result = new JSONObject();
|
|
|
|
loop:while (state.tokenCount < PREVIEW_PARSING_LIMIT) {
|
|
|
|
try {
|
|
|
|
JsonToken token = parser.nextToken();
|
|
|
|
if (token == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
state.tokenCount++;
|
|
|
|
|
|
|
|
switch (token) {
|
|
|
|
case FIELD_NAME:
|
|
|
|
String fieldName = parser.getText();
|
|
|
|
Object fieldValue = parseForPreview(parser, state);
|
|
|
|
JSONUtilities.safePut(result, fieldName, fieldValue);
|
|
|
|
break;
|
|
|
|
case END_OBJECT:
|
|
|
|
break loop;
|
|
|
|
default:
|
|
|
|
break loop;
|
2010-09-28 00:53:17 +02:00
|
|
|
}
|
2011-10-07 21:06:53 +02:00
|
|
|
} catch (IOException e) {
|
2011-08-02 05:34:47 +02:00
|
|
|
break;
|
2010-09-28 00:53:17 +02:00
|
|
|
}
|
|
|
|
}
|
2011-08-02 05:34:47 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
final static private JSONArray parseArrayForPreview(JsonParser parser, PreviewParsingState state) {
|
|
|
|
JSONArray result = new JSONArray();
|
|
|
|
loop:while (state.tokenCount < PREVIEW_PARSING_LIMIT) {
|
|
|
|
try {
|
|
|
|
JsonToken token = parser.nextToken();
|
|
|
|
if (token == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
state.tokenCount++;
|
|
|
|
|
|
|
|
switch (token) {
|
|
|
|
case END_ARRAY:
|
|
|
|
break loop;
|
|
|
|
default:
|
|
|
|
Object element = parseForPreview(parser, state, token);
|
|
|
|
JSONUtilities.append(result, element);
|
|
|
|
}
|
2011-10-07 21:06:53 +02:00
|
|
|
} catch (IOException e) {
|
2011-08-02 05:34:47 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void parseOneFile(Project project, ProjectMetadata metadata,
|
|
|
|
ImportingJob job, String fileSource, Reader reader,
|
|
|
|
ImportColumnGroup rootColumnGroup, int limit, JSONObject options, List<Exception> exceptions) {
|
|
|
|
|
|
|
|
parseOneFile(project, metadata, job, fileSource,
|
|
|
|
new JSONTreeReader(reader), rootColumnGroup, limit, options, exceptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
static public class JSONTreeReader implements TreeReader {
|
|
|
|
final static Logger logger = LoggerFactory.getLogger("JsonParser");
|
|
|
|
|
|
|
|
JsonFactory factory = new JsonFactory();
|
|
|
|
JsonParser parser = null;
|
|
|
|
|
2012-01-27 18:05:45 +01:00
|
|
|
private JsonToken current = null;
|
|
|
|
private JsonToken next = null;
|
|
|
|
private String fieldName = ANONYMOUS;
|
|
|
|
private String fieldValue = null;
|
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
|
|
|
|
public JSONTreeReader(Reader reader) {
|
|
|
|
try {
|
|
|
|
parser = factory.createJsonParser(reader);
|
2012-01-27 18:05:45 +01:00
|
|
|
current = null;
|
|
|
|
next = parser.nextToken();
|
2011-10-07 21:06:53 +02:00
|
|
|
} catch (IOException e) {
|
2011-08-02 05:34:47 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does nothing. All Json is treated as elements
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public int getAttributeCount() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does nothing. All Json is treated as elements
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String getAttributeLocalName(int index) {
|
|
|
|
return null;
|
|
|
|
}
|
2010-09-28 00:53:17 +02:00
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
/**
|
|
|
|
* Does nothing. All Json is treated as elements
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String getAttributePrefix(int index) {
|
|
|
|
return null;
|
|
|
|
}
|
2010-09-28 00:53:17 +02:00
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
/**
|
|
|
|
* Does nothing. All Json is treated as elements
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String getAttributeValue(int index) {
|
|
|
|
return null;
|
|
|
|
}
|
2010-09-28 00:53:17 +02:00
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
@Override
|
2011-10-07 21:06:53 +02:00
|
|
|
public Token current() {
|
2012-01-27 18:05:45 +01:00
|
|
|
if (current != null) {
|
|
|
|
return this.mapToToken(current);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
2010-09-28 00:53:17 +02:00
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
@Override
|
2011-10-07 21:06:53 +02:00
|
|
|
public String getFieldName() throws TreeReaderException {
|
2012-01-27 18:05:45 +01:00
|
|
|
return fieldName;
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does nothing. Json does not have prefixes
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String getPrefix() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-10-07 21:06:53 +02:00
|
|
|
public String getFieldValue() throws TreeReaderException {
|
2012-01-27 18:05:45 +01:00
|
|
|
return fieldValue;
|
2010-09-28 00:53:17 +02:00
|
|
|
}
|
|
|
|
|
2011-08-02 05:34:47 +02:00
|
|
|
@Override
|
2011-10-07 21:06:53 +02:00
|
|
|
public boolean hasNext() {
|
2012-01-27 18:05:45 +01:00
|
|
|
return next != null;
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-10-07 21:06:53 +02:00
|
|
|
public Token next() throws TreeReaderException {
|
2012-01-27 18:05:45 +01:00
|
|
|
JsonToken previous = current;
|
|
|
|
current = next;
|
|
|
|
next = null; // in case an exception is thrown
|
2011-08-02 05:34:47 +02:00
|
|
|
try {
|
2012-01-27 18:05:45 +01:00
|
|
|
if (current != null) {
|
|
|
|
if (current.isScalarValue()) {
|
|
|
|
fieldValue = parser.getText();
|
|
|
|
} else {
|
|
|
|
fieldValue = null;
|
|
|
|
}
|
|
|
|
if (current == JsonToken.FIELD_NAME) {
|
|
|
|
fieldName = parser.getText();
|
|
|
|
} else if (current == JsonToken.START_ARRAY
|
|
|
|
|| current == JsonToken.START_OBJECT) {
|
|
|
|
// Use current field name for next level object
|
|
|
|
// ie elide one level of anonymous fields
|
|
|
|
if (previous != JsonToken.FIELD_NAME) {
|
|
|
|
fieldName = ANONYMOUS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-02 05:34:47 +02:00
|
|
|
next = parser.nextToken();
|
|
|
|
} catch (IOException e) {
|
2011-10-07 21:06:53 +02:00
|
|
|
throw new TreeReaderException(e);
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
2012-01-27 18:05:45 +01:00
|
|
|
return current();
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected Token mapToToken(JsonToken token){
|
|
|
|
switch(token){
|
|
|
|
case START_ARRAY: return Token.StartEntity;
|
|
|
|
case END_ARRAY: return Token.EndEntity;
|
|
|
|
case START_OBJECT: return Token.StartEntity;
|
|
|
|
case END_OBJECT: return Token.EndEntity;
|
|
|
|
case VALUE_STRING: return Token.Value;
|
|
|
|
case FIELD_NAME: return Token.Ignorable; //returned by the getLocalName function()
|
|
|
|
case VALUE_NUMBER_INT: return Token.Value;
|
|
|
|
//Json does not have START_DOCUMENT token type (so ignored as default)
|
|
|
|
//Json does not have END_DOCUMENT token type (so ignored as default)
|
|
|
|
case VALUE_TRUE : return Token.Value;
|
|
|
|
case VALUE_NUMBER_FLOAT : return Token.Value;
|
|
|
|
case VALUE_NULL : return Token.Value;
|
|
|
|
case VALUE_FALSE : return Token.Value;
|
|
|
|
case VALUE_EMBEDDED_OBJECT : return Token.Ignorable;
|
|
|
|
case NOT_AVAILABLE : return Token.Ignorable;
|
|
|
|
default: return Token.Ignorable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-09-28 00:53:17 +02:00
|
|
|
}
|