From fcebbf3ef4d4046e634242c80650ae4becd3eac0 Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Thu, 23 Nov 2023 12:52:48 +0000 Subject: [PATCH 1/4] Added Utils.readStorageJSON and relaxed JSON parser, and swap all interface.html over to using it. This allows Bangle.js 2v20 (or cutting edge) and later to store a relaxed 'JSON' on internal storage which (while still normal JS) is smaller and faster (and preserves unicode better) See https://github.com/espruino/Espruino/issues/2429 --- apps/calendar/interface.html | 5 ++--- apps/espruinoterm/interface.html | 5 +---- apps/grocery/interface.html | 4 ++-- apps/guitarsongs/manage_songs.html | 4 ++-- apps/mylocation/interface.html | 5 ++--- apps/noteify/interface.html | 4 ++-- apps/openstmap/interface.html | 10 ++-------- apps/owmweather/interface.html | 25 ++++++++++++------------- apps/powermanager/interface.html | 19 ++++++++----------- apps/rep/interface.html | 4 +--- apps/sched/interface.html | 8 ++++---- apps/sleepphasealarm/interface.html | 4 ++-- apps/tinycmc/interface.html | 26 +++++++++++++------------- core | 2 +- 14 files changed, 54 insertions(+), 71 deletions(-) diff --git a/apps/calendar/interface.html b/apps/calendar/interface.html index ea64632f86..71b9a153c9 100644 --- a/apps/calendar/interface.html +++ b/apps/calendar/interface.html @@ -178,9 +178,8 @@ })()\n`, contents => { const fileNames = JSON.parse(contents); if (fileNames.length > 0) { - Util.readStorage('calendar.days.json',data=>{ - holidays = JSON.parse(data || "[]") || []; - + Util.readStorageJSON('calendar.days.json',data=>{ + holidays = data || []; Util.hideModal(); render(); }); diff --git a/apps/espruinoterm/interface.html b/apps/espruinoterm/interface.html index 660b3a86c5..6ff7b7da54 100644 --- a/apps/espruinoterm/interface.html +++ b/apps/espruinoterm/interface.html @@ -83,11 +83,8 @@ function onInit() { Util.showModal("Loading..."); - Util.readStorage("espruinoterm.json", function(j) { + Util.readStorageJSON("espruinoterm.json", function(options) { Util.hideModal(); - try { - options = JSON.parse(j); - } catch (e) {} if (!Array.isArray(options)) setDefaults(); refresh(); }); diff --git a/apps/grocery/interface.html b/apps/grocery/interface.html index 0c8df1e863..c424643a83 100644 --- a/apps/grocery/interface.html +++ b/apps/grocery/interface.html @@ -76,11 +76,11 @@

Add a new product

function getData() { // show loading window Util.showModal("Loading..."); - Util.readStorage('grocery_list.json', data=>{ + Util.readStorageJSON('grocery_list.json', data=>{ // remove window Util.hideModal(); - settings = JSON.parse(data || '{"products": []}'); + settings = data || {"products": []}; products = settings.products; renderProducts(); }); diff --git a/apps/guitarsongs/manage_songs.html b/apps/guitarsongs/manage_songs.html index 9280d8d969..bdcab53af5 100644 --- a/apps/guitarsongs/manage_songs.html +++ b/apps/guitarsongs/manage_songs.html @@ -211,9 +211,9 @@

Available Chords

this.editSong = song; }, loadSongs: function () { - Util.readStorage('guitar_songs.json', (contents) => { + Util.readStorageJSON('guitar_songs.json', (contents) => { this.songsState = 'loaded'; - this.localSongs = JSON.parse(contents) || []; + this.localSongs = contents || []; this.watchSongs = JSON.parse(JSON.stringify(this.localSongs)); }); window.setTimeout(() => { diff --git a/apps/mylocation/interface.html b/apps/mylocation/interface.html index 79a122bf73..1bd3129ee4 100644 --- a/apps/mylocation/interface.html +++ b/apps/mylocation/interface.html @@ -98,11 +98,10 @@ function onInit() { // read existing location - Util.readStorage("mylocation.json", function(data) { + Util.readStorageJSON("mylocation.json", function(data) { if (data===undefined) return; // no file try { - var j = JSON.parse(data); - setPosition(j); + setPosition(data); } catch (e) { console.error(e); } diff --git a/apps/noteify/interface.html b/apps/noteify/interface.html index 4d7974ad91..6635ff5ed1 100644 --- a/apps/noteify/interface.html +++ b/apps/noteify/interface.html @@ -26,8 +26,8 @@ function getData() { // show loading window Util.showModal("Loading..."); - Util.readStorage(`noteify.json`,data=>{ - notes = JSON.parse(data || "[]"); + Util.readStorageJSON(`noteify.json`,data=>{ + notes = data || []; // remove window Util.hideModal(); diff --git a/apps/openstmap/interface.html b/apps/openstmap/interface.html index 9e22c57e4e..938a8ea320 100644 --- a/apps/openstmap/interface.html +++ b/apps/openstmap/interface.html @@ -150,15 +150,9 @@ files.trim().split(",").forEach(filename => { if (filename=="") return; promise = promise.then(() => new Promise(resolve => { - Util.readStorage(filename, fileContents => { - console.log(filename + " => " + fileContents); + Util.readStorageJSON(filename, mapInfo => { + console.log(filename + " => " + mapInfo); let mapNumber = filename.match(/\d+/)[0]; // figure out what map number we are - let mapInfo; - try { - mapInfo = JSON.parse(fileContents); - } catch (e) { - console.error(e); - } loadedMaps[mapNumber] = mapInfo; if (mapInfo!==undefined) { let latlon = L.latLng(mapInfo.lat, mapInfo.lon); diff --git a/apps/owmweather/interface.html b/apps/owmweather/interface.html index 3f9467a834..2050357492 100644 --- a/apps/owmweather/interface.html +++ b/apps/owmweather/interface.html @@ -2,35 +2,34 @@ - +

Set OpenWeatherMap (OWM) API key

- +

Where to get your personal API key?

Go to https://home.openweathermap.org/users/sign_up and sign up for a free account.
After registration you can login and optain your personal API key.

- +