automatically parse json files on import

pull/89/head
jmoenig 2019-01-09 17:45:31 +01:00
rodzic dab03fac5d
commit 80ea5ab9ba
3 zmienionych plików z 33 dodań i 9 usunięć

Wyświetl plik

@ -7,7 +7,8 @@
* Morphic: added option to include alpha in color comparison
* Objects: fixed "set pen color (color picker)" to observe, i.e. overwrite transparency
* Lists, Threads, Objects: added (Bernat's) JSON parser to SPLIT block
* Lists, Objects: added "export as JSON" capability
* Lists, Objects: added "export as JSON" capability
* Lists, Objects: automatically parse json files on import
### 2019-01-08
* Objects: automatically parse csv files on import, experimental "raw data" and "parse" ops

Wyświetl plik

@ -541,7 +541,7 @@ List.prototype.canBeCSV = function () {
isString(value) ||
value === true ||
value === false ||
(value instanceof List && value.hasOnlyAtomicData);
(value instanceof List && value.hasOnlyAtomicData());
});
};
@ -551,7 +551,7 @@ List.prototype.canBeJSON = function () {
isString(value) ||
value === true ||
value === false ||
(value instanceof List && value.canBeJSON);
(value instanceof List && value.canBeJSON());
});
};

Wyświetl plik

@ -9549,7 +9549,9 @@ WatcherMorph.prototype.userMenu = function () {
'text/csv;charset=utf-8', // RFC 4180
myself.getter // variable name
);
}
},
null,
new Color(100, 0, 0)
);
}
if (this.currentValue instanceof List &&
@ -9602,6 +9604,19 @@ WatcherMorph.prototype.userMenu = function () {
);
}
);
} else if (this.currentValue instanceof List &&
this.currentValue.canBeJSON()) {
menu.addItem(
'export...',
function () {
var ide = myself.parentThatIsA(IDE_Morph);
ide.saveFileAs(
myself.currentValue.asJSON(true), // guessObjects
'text/json;charset=utf-8',
myself.getter // variable name
);
}
);
} else if (this.currentValue instanceof Context) {
vNames = this.currentValue.outerContext.variables.names();
if (vNames.length) {
@ -9643,20 +9658,25 @@ WatcherMorph.prototype.importData = function (raw) {
function isTextFile(aFile) {
// special cases for Windows
// check the file extension for text-like-ness
return aFile.type.indexOf("text") !== -1 ||
return aFile.type.indexOf('text') !== -1 ||
contains(['txt', 'csv', 'xml', 'json', 'tsv'], ext);
}
function isCSVFile(aFile) {
return aFile.type.indexOf("csv") !== -1 || (ext === 'csv');
function isType(aFile, string) {
return aFile.type.indexOf(string) !== -1 || (ext === string);
}
frd.onloadend = function (e) {
if (!raw && isCSVFile(aFile)) {
if (!raw && isType(aFile, 'csv')) {
myself.target.setVar(
myself.getter,
Process.prototype.parseCSV(e.target.result)
);
} else if (!raw && isType(aFile, 'json')) {
myself.target.setVar(
myself.getter,
Process.prototype.parseJSON(e.target.result)
);
} else {
myself.target.setVar(
myself.getter,
@ -9711,9 +9731,12 @@ WatcherMorph.prototype.importData = function (raw) {
WatcherMorph.prototype.parseTxt = function () {
// experimental!
var src = this.target.vars[this.getter].value;
this.target.setVar(
this.getter,
Process.prototype.parseCSV(this.target.vars[this.getter].value)
src.indexOf('\[') === 0 ?
Process.prototype.parseJSON(src)
: Process.prototype.parseCSV(src)
);
};