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

@ -8,6 +8,7 @@
* Objects: fixed "set pen color (color picker)" to observe, i.e. overwrite transparency * 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, 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 ### 2019-01-08
* Objects: automatically parse csv files on import, experimental "raw data" and "parse" ops * 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) || isString(value) ||
value === true || value === true ||
value === false || value === false ||
(value instanceof List && value.hasOnlyAtomicData); (value instanceof List && value.hasOnlyAtomicData());
}); });
}; };
@ -551,7 +551,7 @@ List.prototype.canBeJSON = function () {
isString(value) || isString(value) ||
value === true || value === true ||
value === false || 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 'text/csv;charset=utf-8', // RFC 4180
myself.getter // variable name myself.getter // variable name
); );
} },
null,
new Color(100, 0, 0)
); );
} }
if (this.currentValue instanceof List && 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) { } else if (this.currentValue instanceof Context) {
vNames = this.currentValue.outerContext.variables.names(); vNames = this.currentValue.outerContext.variables.names();
if (vNames.length) { if (vNames.length) {
@ -9643,20 +9658,25 @@ WatcherMorph.prototype.importData = function (raw) {
function isTextFile(aFile) { function isTextFile(aFile) {
// special cases for Windows // special cases for Windows
// check the file extension for text-like-ness // 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); contains(['txt', 'csv', 'xml', 'json', 'tsv'], ext);
} }
function isCSVFile(aFile) { function isType(aFile, string) {
return aFile.type.indexOf("csv") !== -1 || (ext === 'csv'); return aFile.type.indexOf(string) !== -1 || (ext === string);
} }
frd.onloadend = function (e) { frd.onloadend = function (e) {
if (!raw && isCSVFile(aFile)) { if (!raw && isType(aFile, 'csv')) {
myself.target.setVar( myself.target.setVar(
myself.getter, myself.getter,
Process.prototype.parseCSV(e.target.result) Process.prototype.parseCSV(e.target.result)
); );
} else if (!raw && isType(aFile, 'json')) {
myself.target.setVar(
myself.getter,
Process.prototype.parseJSON(e.target.result)
);
} else { } else {
myself.target.setVar( myself.target.setVar(
myself.getter, myself.getter,
@ -9711,9 +9731,12 @@ WatcherMorph.prototype.importData = function (raw) {
WatcherMorph.prototype.parseTxt = function () { WatcherMorph.prototype.parseTxt = function () {
// experimental! // experimental!
var src = this.target.vars[this.getter].value;
this.target.setVar( this.target.setVar(
this.getter, this.getter,
Process.prototype.parseCSV(this.target.vars[this.getter].value) src.indexOf('\[') === 0 ?
Process.prototype.parseJSON(src)
: Process.prototype.parseCSV(src)
); );
}; };