use of org.json.JSONTokener in project OpenRefine by OpenRefine.
the class ColumnSplitChange method load.
public static Change load(LineNumberReader reader, Pool pool) throws Exception {
String columnName = null;
List<String> columnNames = null;
List<Integer> rowIndices = null;
List<List<Serializable>> tuples = null;
boolean removeOriginalColumn = false;
Column column = null;
int columnIndex = -1;
int firstNewCellIndex = -1;
List<Row> oldRows = null;
List<Row> newRows = null;
List<ColumnGroup> oldColumnGroups = null;
String line;
while ((line = reader.readLine()) != null && !"/ec/".equals(line)) {
int equal = line.indexOf('=');
CharSequence field = line.subSequence(0, equal);
String value = line.substring(equal + 1);
if ("columnName".equals(field)) {
columnName = value;
} else if ("columnNameCount".equals(field)) {
int count = Integer.parseInt(value);
columnNames = new ArrayList<String>(count);
for (int i = 0; i < count; i++) {
line = reader.readLine();
if (line != null) {
columnNames.add(line);
}
}
} else if ("rowIndexCount".equals(field)) {
int count = Integer.parseInt(value);
rowIndices = new ArrayList<Integer>(count);
for (int i = 0; i < count; i++) {
line = reader.readLine();
if (line != null) {
rowIndices.add(Integer.parseInt(line));
}
}
} else if ("tupleCount".equals(field)) {
int count = Integer.parseInt(value);
tuples = new ArrayList<List<Serializable>>(count);
for (int i = 0; i < count; i++) {
line = reader.readLine();
if (line == null) {
continue;
}
int valueCount = Integer.parseInt(line);
List<Serializable> tuple = new ArrayList<Serializable>(valueCount);
for (int r = 0; r < valueCount; r++) {
line = reader.readLine();
JSONTokener t = new JSONTokener(line);
Object o = t.nextValue();
tuple.add((o != JSONObject.NULL) ? (Serializable) o : null);
}
tuples.add(tuple);
}
} else if ("removeOriginalColumn".equals(field)) {
removeOriginalColumn = Boolean.parseBoolean(value);
} else if ("column".equals(field)) {
column = Column.load(value);
} else if ("columnIndex".equals(field)) {
columnIndex = Integer.parseInt(value);
} else if ("firstNewCellIndex".equals(field)) {
firstNewCellIndex = Integer.parseInt(value);
} else if ("oldRowCount".equals(field)) {
int count = Integer.parseInt(value);
oldRows = new ArrayList<Row>(count);
for (int i = 0; i < count; i++) {
line = reader.readLine();
if (line != null) {
oldRows.add(Row.load(line, pool));
}
}
} else if ("newRowCount".equals(field)) {
int count = Integer.parseInt(value);
newRows = new ArrayList<Row>(count);
for (int i = 0; i < count; i++) {
line = reader.readLine();
if (line != null) {
newRows.add(Row.load(line, pool));
}
}
} else if ("oldColumnGroupCount".equals(field)) {
int oldColumnGroupCount = Integer.parseInt(line.substring(equal + 1));
oldColumnGroups = ColumnChange.readOldColumnGroups(reader, oldColumnGroupCount);
}
}
ColumnSplitChange change = new ColumnSplitChange(columnName, columnNames, rowIndices, tuples, removeOriginalColumn, column, columnIndex, firstNewCellIndex, oldRows, newRows);
change._oldColumnGroups = oldColumnGroups != null ? oldColumnGroups : new LinkedList<ColumnGroup>();
return change;
}
use of org.json.JSONTokener in project OpenRefine by OpenRefine.
the class FileProjectManager method loadFromFile.
protected boolean loadFromFile(File file) {
logger.info("Loading workspace: {}", file.getAbsolutePath());
_projectsMetadata.clear();
boolean found = false;
if (file.exists() || file.canRead()) {
FileReader reader = null;
try {
reader = new FileReader(file);
JSONTokener tokener = new JSONTokener(reader);
JSONObject obj = (JSONObject) tokener.nextValue();
JSONArray a = obj.getJSONArray("projectIDs");
int count = a.length();
for (int i = 0; i < count; i++) {
long id = a.getLong(i);
File projectDir = getProjectDir(id);
ProjectMetadata metadata = ProjectMetadataUtilities.load(projectDir);
_projectsMetadata.put(id, metadata);
}
if (obj.has("preferences") && !obj.isNull("preferences")) {
_preferenceStore.load(obj.getJSONObject("preferences"));
}
if (obj.has("expressions") && !obj.isNull("expressions")) {
// backward compatibility
((TopList) _preferenceStore.get("scripting.expressions")).load(obj.getJSONArray("expressions"));
}
found = true;
} catch (JSONException e) {
logger.warn("Error reading file", e);
} catch (IOException e) {
logger.warn("Error reading file", e);
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
logger.warn("Exception closing file", e);
}
}
}
return found;
}
use of org.json.JSONTokener in project OpenRefine by OpenRefine.
the class ParsingUtilities method evaluateJsonStringToObject.
public static JSONObject evaluateJsonStringToObject(String s) throws JSONException {
if (s == null) {
throw new IllegalArgumentException("parameter 's' should not be null");
}
JSONTokener t = new JSONTokener(s);
Object o = t.nextValue();
if (o instanceof JSONObject) {
return (JSONObject) o;
} else {
throw new JSONException(s + " couldn't be parsed as JSON object");
}
}
use of org.json.JSONTokener in project ThinkAndroid by white-cat.
the class JsonHttpResponseHandler method parseResponse.
protected Object parseResponse(String responseBody) throws JSONException {
Object result = null;
//trim the string to prevent start with blank, and test if the string is valid JSON, because the parser don't do this :(. If Json is not valid this will return null
responseBody = responseBody.trim();
if (responseBody.startsWith("{") || responseBody.startsWith("[")) {
result = new JSONTokener(responseBody).nextValue();
}
if (result == null) {
result = responseBody;
}
return result;
}
use of org.json.JSONTokener in project tdme by andreasdr.
the class LevelFileImport method doImport.
/**
* Imports a level from a TDME level file to Level Editor
* @param path name
* @param file name
* @param level
* @param object id prefix
*/
public static void doImport(String pathName, String fileName, LevelEditorLevel level, String objectIdPrefix) throws Exception {
pathName = pathName.replace(File.separatorChar == '/' ? '\\' : '/', File.separatorChar);
fileName = fileName.replace(File.separatorChar == '/' ? '\\' : '/', File.separatorChar);
JSONObject jRoot = null;
InputStream is = null;
try {
jRoot = new JSONObject(new JSONTokener(FileSystem.getInstance().getContent(pathName, fileName)));
} catch (IOException ioe) {
throw ioe;
} finally {
if (is != null)
try {
is.close();
} catch (IOException ioei) {
}
}
// game root path
level.setGameRoot(Tools.getGameRootPath(pathName));
// check for version
float version = Float.parseFloat(jRoot.getString("version"));
// new: rotation order
level.setRotationOrder(jRoot.has("ro") == true ? RotationOrder.valueOf(jRoot.getString("ro")) : RotationOrder.XYZ);
// map properties
level.clearProperties();
// parse properties
JSONArray jMapProperties = jRoot.getJSONArray("properties");
for (int i = 0; i < jMapProperties.length(); i++) {
JSONObject jMapProperty = jMapProperties.getJSONObject(i);
level.addProperty(jMapProperty.getString("name"), jMapProperty.getString("value"));
}
// lights
if (jRoot.has("lights") == true) {
JSONArray jLights = jRoot.getJSONArray("lights");
for (int i = 0; i < jLights.length(); i++) {
JSONObject jLight = jLights.getJSONObject(i);
LevelEditorLight light = level.getLightAt(jLight.has("id") ? jLight.getInt("id") : i);
// set up light in level
light.getAmbient().set((float) jLight.getDouble("ar"), (float) jLight.getDouble("ag"), (float) jLight.getDouble("ab"), (float) jLight.getDouble("aa"));
light.getDiffuse().set((float) jLight.getDouble("dr"), (float) jLight.getDouble("dg"), (float) jLight.getDouble("db"), (float) jLight.getDouble("da"));
light.getSpecular().set((float) jLight.getDouble("sr"), (float) jLight.getDouble("sg"), (float) jLight.getDouble("sb"), (float) jLight.getDouble("sa"));
light.getPosition().set((float) jLight.getDouble("px"), (float) jLight.getDouble("py"), (float) jLight.getDouble("pz"), (float) jLight.getDouble("pw"));
light.setConstantAttenuation((float) jLight.getDouble("ca"));
light.setLinearAttenuation((float) jLight.getDouble("la"));
light.setQuadraticAttenuation((float) jLight.getDouble("qa"));
light.getSpotTo().set((float) jLight.getDouble("stx"), (float) jLight.getDouble("sty"), (float) jLight.getDouble("stz"));
light.getSpotDirection().set((float) jLight.getDouble("sdx"), (float) jLight.getDouble("sdy"), (float) jLight.getDouble("sdz"));
light.setSpotExponent((float) jLight.getDouble("se"));
light.setSpotCutOff((float) jLight.getDouble("sco"));
light.setEnabled(jLight.getBoolean("e"));
}
}
// entities
level.getEntityLibrary().clear();
JSONArray jModels = jRoot.getJSONArray("models");
for (int i = 0; i < jModels.length(); i++) {
JSONObject jModel = jModels.getJSONObject(i);
// add model to library
LevelEditorEntity levelEditorEntity = ModelMetaDataFileImport.doImportFromJSON(jModel.getInt("id"), new File(pathName).getCanonicalPath(), jModel.getJSONObject("entity"));
// do we have a valid entity?
if (levelEditorEntity == null) {
throw new Exception("Invalid entity");
}
// add entity
level.getEntityLibrary().addEntity(levelEditorEntity);
// parse optional model properties
if (jModel.has("properties")) {
JSONArray jModelProperties = jModel.getJSONArray("properties");
for (int j = 0; j < jModelProperties.length(); j++) {
JSONObject jModelProperty = jModelProperties.getJSONObject(j);
levelEditorEntity.addProperty(jModelProperty.getString("name"), jModelProperty.getString("value"));
}
}
}
// objects
level.clearObjects();
JSONArray jObjects = jRoot.getJSONArray("objects");
for (int i = 0; i < jObjects.length(); i++) {
JSONObject jObject = jObjects.getJSONObject(i);
LevelEditorEntity model = level.getEntityLibrary().getEntity(jObject.getInt("mid"));
Transformations transformations = new Transformations();
transformations.getPivot().set(model.getPivot());
transformations.getTranslation().set((float) jObject.getDouble("tx"), (float) jObject.getDouble("ty"), (float) jObject.getDouble("tz"));
transformations.getScale().set((float) jObject.getDouble("sx"), (float) jObject.getDouble("sy"), (float) jObject.getDouble("sz"));
Vector3 rotation = new Vector3((float) jObject.getDouble("rx"), (float) jObject.getDouble("ry"), (float) jObject.getDouble("rz"));
transformations.getRotations().add(new Rotation(rotation.getArray()[level.getRotationOrder().getAxis0VectorIndex()], level.getRotationOrder().getAxis0()));
transformations.getRotations().add(new Rotation(rotation.getArray()[level.getRotationOrder().getAxis1VectorIndex()], level.getRotationOrder().getAxis1()));
transformations.getRotations().add(new Rotation(rotation.getArray()[level.getRotationOrder().getAxis2VectorIndex()], level.getRotationOrder().getAxis2()));
transformations.update();
LevelEditorObject levelEditorObject = new LevelEditorObject(objectIdPrefix != null ? objectIdPrefix + jObject.getString("id") : jObject.getString("id"), jObject.has("descr") ? jObject.getString("descr") : "", transformations, model);
levelEditorObject.clearProperties();
// parse optional object properties, new in LE 0.3a
if (jObject.has("properties")) {
JSONArray jObjectProperties = jObject.getJSONArray("properties");
for (int j = 0; j < jObjectProperties.length(); j++) {
JSONObject jObjectProperty = jObjectProperties.getJSONObject(j);
levelEditorObject.addProperty(jObjectProperty.getString("name"), jObjectProperty.getString("value"));
}
}
// check if entity already exists
// small workaround for a bug that would allow to place to objects at same place with same model
/*
boolean skipObject = false;
for (int j = 0; j < level.getObjectCount(); j++) {
LevelEditorObject _levelEditorObject = level.getObjectAt(j);
if (_levelEditorObject.getModel() == levelEditorObject.getModel() &&
_levelEditorObject.getTransformations().getTranslation().equals(levelEditorObject.getTransformations().getTranslation())) {
System.out.println("Skipping '" + levelEditorObject + "' as we already have a object with this model and translation.");
// we already have a object with selected model on this translation
skipObject = true;
break;
}
}
// skip on object if requested
if (skipObject == true) continue;
*/
// otherwise add
level.addObject(levelEditorObject);
}
level.setObjectIdx(jRoot.getInt("objects_eidx"));
level.setPathName(pathName);
level.setFileName(fileName);
level.computeDimension();
}
Aggregations