Skip to content

Commit

Permalink
Merge pull request espruino#3110 from espruino/relaxed_json
Browse files Browse the repository at this point in the history
Added Utils.readStorageJSON with relaxed JSON parser
  • Loading branch information
gfwilliams authored Nov 24, 2023
2 parents 0e4fa18 + 4fc5fa2 commit c7efd4f
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 71 deletions.
5 changes: 2 additions & 3 deletions apps/calendar/interface.html
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
5 changes: 1 addition & 4 deletions apps/espruinoterm/interface.html
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
4 changes: 2 additions & 2 deletions apps/grocery/interface.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ <h4>Add a new product</h4>
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();
});
Expand Down
4 changes: 2 additions & 2 deletions apps/guitarsongs/manage_songs.html
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ <h3>Available Chords</h3>
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(() => {
Expand Down
5 changes: 2 additions & 3 deletions apps/mylocation/interface.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions apps/noteify/interface.html
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 2 additions & 8 deletions apps/openstmap/interface.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 + " => " + JSON.stringify(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);
Expand Down
26 changes: 13 additions & 13 deletions apps/owmweather/interface.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@
<head>
<link rel="stylesheet" href="../../css/spectre.min.css">
</head>
<body>
<body>
<h3>Set OpenWeatherMap (OWM) API key</h3>
<p><input id="apikey" onkeyup="checkInput()" style="width:90%; margin: 3px"></input><button id="upload" class="btn btn-primary">Save key</button></p>

<h4>Where to get your personal API key?</h4>
<p>Go to <a href="https://home.openweathermap.org/users/sign_up">https://home.openweathermap.org/users/sign_up</a> and sign up for a free account.<br>
After registration you can login and optain your personal API key.</p>


<script src="../../core/lib/interface.js"></script>

<script>

function checkInput() {
if(document.getElementById("apikey").value==="") {
document.getElementById('upload').disabled = true;
} else {
if(document.getElementById("apikey").value==="") {
document.getElementById('upload').disabled = true;
} else {
document.getElementById('upload').disabled = false;
}
}
checkInput();

var settings = {};
function onInit(){
console.log("Loading settings from BangleJs...");
try {
Util.readStorage("owmweather.json", data=>{
if(data.length > 0){
settings = JSON.parse(data);
Util.readStorageJSON("owmweather.json", data=>{
if(data){
settings = data;
console.log("Got settings", settings);
document.getElementById("apikey").value = settings.apikey;
console.log("Loaded apikey from BangleJs.");
Expand All @@ -41,9 +41,9 @@ <h4>Where to get your personal API key?</h4>
console.log("(Warning) Could not load apikey from BangleJs.");
console.log(ex);
}

}

document.getElementById("upload").addEventListener("click", function() {
try {
settings.apikey = document.getElementById("apikey").value;
Expand Down
19 changes: 8 additions & 11 deletions apps/powermanager/interface.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<head>
<link rel="stylesheet" href="../../css/spectre.min.css">

<style>
<style>
.table_wrapper{
display: block;
overflow-x: auto;
Expand Down Expand Up @@ -101,11 +101,9 @@
Puck.eval(`require("Storage").list("powermanager.def.json").length > 0`, (f)=>{
if (f) {
Util.showModal("Reading summarized info...");
Util.readStorage(
filename, data => {
Util.readStorageJSON(
filename, parsed => {
Util.hideModal();
let parsed = JSON.parse(data);

let sum = 0;
let rows = [];
for (var i in parsed.deferred) {
Expand All @@ -128,7 +126,7 @@
<button class="btn btn-primary" id="back" style="float: right;margin-right: 5px;margin-left: 10px;">Back</button>
<div>
This are functions used in timeouts and intervals and their accumulated execution times. Recorded in a time span of <b>${timeFormat(duration)}</b>. Timeouts/intervals have run for <b>${timeFormat(sum)} (${(sum/duration*100).toFixed(2)}%)</b>. Percentages are calculated from summarized timeout/interval running time.
</div>
</div>
<div class="table_wrapper">
Expand Down Expand Up @@ -181,13 +179,12 @@
Puck.eval(`require("Storage").list("powermanager.hw.json").length > 0`, (f)=>{
if (f) {
Util.showModal("Reading hardware info...");
Util.readStorage(
filename, data => {
Util.readStorageJSON(
filename, parsed => {
Util.hideModal();
let parsed = JSON.parse(data);
console.log("Hardware", parsed);
let duration = parsed.saved - parsed.start;

let rows = [];
for (var i in parsed.power) {
rows.push({func: i, time: parsed.power[i]});
Expand Down Expand Up @@ -244,7 +241,7 @@
Util.readStorageFile(
filename, data => {
Util.hideModal();

var htmlOverview = `<h1>Detailed logging</h1>
<button class="btn btn-primary" id="back" style="float: right;margin-right: 5px;">Back</button>
<div>
Expand Down
4 changes: 1 addition & 3 deletions apps/rep/interface.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,8 @@
uploadBtn.disabled = true;

Util.showModal("Loading...");
Util.readStorage(repJson, data => {
Util.readStorageJSON(repJson, reps => {
Util.hideModal();

reps = JSON.parse(data);
for(const rep of reps){
renderRep(rep);
}
Expand Down
8 changes: 4 additions & 4 deletions apps/sched/interface.html
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,11 @@

function getData() {
Util.showModal("Loading...");
Util.readStorage('sched.json',data=>{
alarms = JSON.parse(data || "[]") || [];
Util.readStorageJSON('sched.json',data=>{
alarms = data || [];

Util.readStorage('sched.settings.json',data=>{
schedSettings = JSON.parse(data || "{}") || {};
Util.readStorageJSON('sched.settings.json',data=>{
schedSettings = data || {};
Util.hideModal();
alarms.sort((a, b) => {
let x;
Expand Down
4 changes: 2 additions & 2 deletions apps/sleepphasealarm/interface.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
// show loading window
Util.showModal("Loading...");
// get the data
Util.readStorage('sleepphasealarm.json',data=>{
let logs = JSON.parse(data || "{}")?.logs || [];
Util.readStorageJSON('sleepphasealarm.json',data=>{
let logs = (data || {})?.logs || [];
// remove window
Util.hideModal();

Expand Down
26 changes: 13 additions & 13 deletions apps/tinycmc/interface.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@
<head>
<link rel="stylesheet" href="../../css/spectre.min.css">
</head>
<body>
<body>
<h3>Set Coinmarketcap API key</h3>
<p><input id="apikey" onkeyup="checkInput()" style="width:90%; margin: 3px"></input><button id="upload" class="btn btn-primary">Save key</button></p>

<h4>Where to get your personal API key?</h4>
<p>Go to <a href="https://coinmarketcap.com/api/">https://coinmarketcap.com/api/</a> and sign up for a free account.<br>
After registration you can login and obtain your personal API key.</p>


<script src="../../core/lib/interface.js"></script>

<script>

function checkInput() {
if(document.getElementById("apikey").value==="") {
document.getElementById('upload').disabled = true;
} else {
if(document.getElementById("apikey").value==="") {
document.getElementById('upload').disabled = true;
} else {
document.getElementById('upload').disabled = false;
}
}
checkInput();

var settings = {};
function onInit(){
console.log("Loading settings from BangleJs...");
try {
Util.readStorage("tinycmc.json", data=>{
if(data.length > 0){
settings = JSON.parse(data);
Util.readStorageJSON("tinycmc.json", data=>{
if(data){
settings = data;
console.log("Got settings", settings);
document.getElementById("apikey").value = settings.apikey;
console.log("Loaded apikey from BangleJs.");
Expand All @@ -41,9 +41,9 @@ <h4>Where to get your personal API key?</h4>
console.log("(Warning) Could not load apikey from BangleJs.");
console.log(ex);
}

}

document.getElementById("upload").addEventListener("click", function() {
try {
settings.apikey = document.getElementById("apikey").value;
Expand Down
2 changes: 1 addition & 1 deletion core
Submodule core updated 4 files
+8 −0 js/index.js
+72 −1 js/utils.js
+17 −4 lib/customize.js
+17 −4 lib/interface.js

0 comments on commit c7efd4f

Please sign in to comment.