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 qi4j-sdk by Qi4j.
the class MapEntityStoreMixin method readEntityState.
protected EntityState readEntityState(DefaultEntityStoreUnitOfWork unitOfWork, Reader entityState) throws EntityStoreException {
try {
Module module = unitOfWork.module();
JSONObject jsonObject = new JSONObject(new JSONTokener(entityState));
EntityStatus status = EntityStatus.LOADED;
String version = jsonObject.getString(JSONKeys.VERSION);
long modified = jsonObject.getLong(JSONKeys.MODIFIED);
String identity = jsonObject.getString(JSONKeys.IDENTITY);
// Check if version is correct
String currentAppVersion = jsonObject.optString(JSONKeys.APPLICATION_VERSION, "0.0");
if (!currentAppVersion.equals(application.version())) {
if (migration != null) {
migration.migrate(jsonObject, application.version(), this);
} else {
// Do nothing - set version to be correct
jsonObject.put(JSONKeys.APPLICATION_VERSION, application.version());
}
LoggerFactory.getLogger(MapEntityStoreMixin.class).debug("Updated version nr on " + identity + " from " + currentAppVersion + " to " + application.version());
// State changed
status = EntityStatus.UPDATED;
}
String type = jsonObject.getString(JSONKeys.TYPE);
EntityDescriptor entityDescriptor = module.entityDescriptor(type);
if (entityDescriptor == null) {
throw new EntityTypeNotFoundException(type);
}
Map<QualifiedName, Object> properties = new HashMap<>();
JSONObject props = jsonObject.getJSONObject(JSONKeys.PROPERTIES);
for (PropertyDescriptor propertyDescriptor : entityDescriptor.state().properties()) {
Object jsonValue;
try {
jsonValue = props.get(propertyDescriptor.qualifiedName().name());
} catch (JSONException e) {
// Value not found, default it
Object initialValue = propertyDescriptor.initialValue(module);
properties.put(propertyDescriptor.qualifiedName(), initialValue);
status = EntityStatus.UPDATED;
continue;
}
if (JSONObject.NULL.equals(jsonValue)) {
properties.put(propertyDescriptor.qualifiedName(), null);
} else {
Object value = valueSerialization.deserialize(propertyDescriptor.valueType(), jsonValue.toString());
properties.put(propertyDescriptor.qualifiedName(), value);
}
}
Map<QualifiedName, EntityReference> associations = new HashMap<>();
JSONObject assocs = jsonObject.getJSONObject(JSONKeys.ASSOCIATIONS);
for (AssociationDescriptor associationType : entityDescriptor.state().associations()) {
try {
Object jsonValue = assocs.get(associationType.qualifiedName().name());
EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference((String) jsonValue);
associations.put(associationType.qualifiedName(), value);
} catch (JSONException e) {
// Association not found, default it to null
associations.put(associationType.qualifiedName(), null);
status = EntityStatus.UPDATED;
}
}
JSONObject manyAssocs = jsonObject.getJSONObject(JSONKeys.MANY_ASSOCIATIONS);
Map<QualifiedName, List<EntityReference>> manyAssociations = new HashMap<>();
for (AssociationDescriptor manyAssociationType : entityDescriptor.state().manyAssociations()) {
List<EntityReference> references = new ArrayList<>();
try {
JSONArray jsonValues = manyAssocs.getJSONArray(manyAssociationType.qualifiedName().name());
for (int i = 0; i < jsonValues.length(); i++) {
Object jsonValue = jsonValues.getString(i);
EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference((String) jsonValue);
references.add(value);
}
manyAssociations.put(manyAssociationType.qualifiedName(), references);
} catch (JSONException e) {
// ManyAssociation not found, default to empty one
manyAssociations.put(manyAssociationType.qualifiedName(), references);
}
}
JSONObject namedAssocs = jsonObject.getJSONObject(JSONKeys.NAMED_ASSOCIATIONS);
Map<QualifiedName, Map<String, EntityReference>> namedAssociations = new HashMap<>();
for (AssociationDescriptor namedAssociationType : entityDescriptor.state().namedAssociations()) {
Map<String, EntityReference> references = new LinkedHashMap<>();
try {
JSONObject jsonValues = namedAssocs.getJSONObject(namedAssociationType.qualifiedName().name());
JSONArray names = jsonValues.names();
if (names != null) {
for (int idx = 0; idx < names.length(); idx++) {
String name = names.getString(idx);
Object value = jsonValues.get(name);
EntityReference ref = value == JSONObject.NULL ? null : EntityReference.parseEntityReference((String) value);
references.put(name, ref);
}
}
namedAssociations.put(namedAssociationType.qualifiedName(), references);
} catch (JSONException e) {
// NamedAssociation not found, default to empty one
namedAssociations.put(namedAssociationType.qualifiedName(), references);
}
}
return new DefaultEntityState(unitOfWork, version, modified, EntityReference.parseEntityReference(identity), status, entityDescriptor, properties, associations, manyAssociations, namedAssociations);
} catch (JSONException e) {
throw new EntityStoreException(e);
}
}
use of org.json.JSONTokener in project openkit-android by OpenKit.
the class Response method createResponsesFromString.
static List<Response> createResponsesFromString(String responseString, HttpURLConnection connection, RequestBatch requests, boolean isFromCache) throws FacebookException, JSONException, IOException {
JSONTokener tokener = new JSONTokener(responseString);
Object resultObject = tokener.nextValue();
List<Response> responses = createResponsesFromObject(connection, requests, resultObject, isFromCache);
Logger.log(LoggingBehavior.REQUESTS, RESPONSE_LOG_TAG, "Response\n Id: %s\n Size: %d\n Responses:\n%s\n", requests.getId(), responseString.length(), responses);
return responses;
}
Aggregations