fixed JSON encoding for nested lists

snap7
Jens Mönig 2022-02-07 08:49:36 +01:00
rodzic 4c6e1c5de4
commit b7a0af2f47
3 zmienionych plików z 16 dodań i 12 usunięć

Wyświetl plik

@ -5,9 +5,13 @@
* **New Features:** * **New Features:**
* **Notable Changes:** * **Notable Changes:**
* **Notable Fixes:** * **Notable Fixes:**
* JSON encoding for nested lists
* **Documentation Updates:** * **Documentation Updates:**
* **Translation Updates:** * **Translation Updates:**
### 2022-02-07
* lists: fixed JSON encoding for nested lists
### 2022-02-04 ### 2022-02-04
* new dev version * new dev version

Wyświetl plik

@ -22,7 +22,7 @@
<script src="src/scenes.js?version=2021-11-24"></script> <script src="src/scenes.js?version=2021-11-24"></script>
<script src="src/gui.js?version=2022-02-04"></script> <script src="src/gui.js?version=2022-02-04"></script>
<script src="src/paint.js?version=2021-07-05"></script> <script src="src/paint.js?version=2021-07-05"></script>
<script src="src/lists.js?version=2022-01-28"></script> <script src="src/lists.js?version=2022-02-07"></script>
<script src="src/byob.js?version=2022-01-07"></script> <script src="src/byob.js?version=2022-01-07"></script>
<script src="src/tables.js?version=2022-01-28"></script> <script src="src/tables.js?version=2022-01-28"></script>
<script src="src/sketch.js?version=2021-11-03"></script> <script src="src/sketch.js?version=2021-11-03"></script>

Wyświetl plik

@ -65,7 +65,7 @@ ZERO, WHITE*/
// Global settings ///////////////////////////////////////////////////// // Global settings /////////////////////////////////////////////////////
modules.lists = '2022-January-28'; modules.lists = '2022-February-07';
var List; var List;
var ListWatcherMorph; var ListWatcherMorph;
@ -957,25 +957,24 @@ List.prototype.asCSV = function () {
return items.map(encodeCell).join(','); return items.map(encodeCell).join(',');
}; };
List.prototype.asJSON = function (guessObjects) { List.prototype.asJSON = function () {
// Caution, no error catching! // Caution, no error catching!
// this method assumes that the list.canBeJSON() // this method assumes that the list.canBeJSON()
function objectify(list, guessObjects) { function objectify(list) {
var items = list.itemsArray(), var items = list.itemsArray(),
obj = {}; obj = {};
if (canBeObject(items)) { if (canBeObject(items)) {
items.forEach(pair => { items.forEach(pair => {
var value = pair.length() === 2 ? pair.at(2) : undefined; var value = pair.length() === 2 ? pair.at(2) : undefined;
obj[pair.at(1)] = (value instanceof List ? obj[pair.at(1)] = (value instanceof List ?
objectify(value, guessObjects) : value); objectify(value) : value);
}); });
return obj; return obj;
} }
return items.map(element => { return items.map(element => element instanceof List ?
return element instanceof List ? objectify(element) : element
objectify(element, guessObjects) : element; );
});
} }
function canBeObject(array) { function canBeObject(array) {
@ -984,18 +983,19 @@ List.prototype.asJSON = function (guessObjects) {
// than as array // than as array
var keys; var keys;
if (array.every( if (array.every(
element => element instanceof List && (element.length() < 3) element => element instanceof List && (element.length() === 2)
)) { )) {
keys = array.map(each => each.at(1)); keys = array.map(each => each.at(1));
return keys.every(each => isString(each) && isUniqueIn(each, keys)); return keys.every(each => isString(each) && isUniqueIn(each, keys));
} }
return false;
} }
function isUniqueIn(element, array) { function isUniqueIn(element, array) {
return array.indexOf(element) === array.lastIndexOf(element); return array.indexOf(element) === array.lastIndexOf(element);
} }
return JSON.stringify(objectify(this, guessObjects)); return JSON.stringify(objectify(this));
}; };
List.prototype.canBeTXT = function () { List.prototype.canBeTXT = function () {
@ -1502,7 +1502,7 @@ ListWatcherMorph.prototype.userMenu = function () {
); );
} else { } else {
ide.saveFileAs( ide.saveFileAs(
this.list.asJSON(true), // guessObjects this.list.asJSON(),
'text/json;charset=utf-8', 'text/json;charset=utf-8',
localize('data') // name localize('data') // name
); );