I transformed this:
function MangaElt(obj) {
"use strict";
this.mirror = obj.mirror;
this.name = obj.name;
this.url = obj.url;
if (obj.lastChapterReadURL !== undefined) {
this.lastChapterReadURL = obj.lastChapterReadURL;
this.lastChapterReadName = obj.lastChapterReadName;
} else {
this.lastChapterReadURL = null;
this.lastChapterReadName = null;
}
this.listChaps = [];
if (obj.listChaps !== undefined && obj.listChaps !== null && obj.listChaps !== "null") {
if (!isArray(obj.listChaps)) {
this.listChaps = JSON.parse(obj.listChaps);
}
}
this.read = 0;
if (obj.read !== undefined && obj.read !== null && obj.read !== "null") {
this.read = obj.read;
}
}
Into this:
function MangaElt(obj) {
"use strict";
this.mirror = obj.mirror;
this.name = obj.name;
this.url = obj.url;
this.lastChapterReadURL = obj.lastChapterReadURL || null;
this.lastChapterReadName = obj.lastChapterReadName || null;
this.listChaps = JSON.parse(obj.listChaps) || [];
this.read = obj.read || 0;
this.update = obj.update || 1;
}
As you can see, the code is now more readable and compact. The snippet works under normal circumstances just fine. The thing is that I don't have sometimes all the values in the obj
object, so, I expect some undefined
's here and there. And that is the reason of my questions:
JSON.parse
interpret a undefined
as string, trowing as say the MDN, "syntax error" for undefined
?undefined
and just return undefined
? (This may rise arguments, so, if you believe that is good as is, just ignore this question or state that I'm just wrong with my train of trough)obj.listChaps
is an array and forget about JSON.parse
here?. (This is always an array or a pseudo-array in a string, and since this is a collaborative project, someone may have a reason for this)For the curious that may ask, 'what's the error you are getting?' is this:
Error in event handler for 'undefined': Unexpected token u SyntaxError: Unexpected token u
at Object.parse (native)
at new MangaElt (chrome-extension://nhjloagockgobfpopemejpgjjechcpfd/js/MangaElt.js:44:25)
at readManga (chrome-extension://nhjloagockgobfpopemejpgjjechcpfd/js/background.js:410:24)
at chrome-extension://nhjloagockgobfpopemejpgjjechcpfd/js/background.js:607:9
at Event.dispatchToListener (event_bindings:356:21)
at Event.dispatch_ (event_bindings:342:27)
at Event.dispatch (event_bindings:362:17)
at miscellaneous_bindings:165:24
at Event.dispatchToListener (event_bindings:356:21)
at Event.dispatch_ (event_bindings:342:27) event_bindings:346
This is what already existing entries looks like, which do not generate errors. This scenario is what motivated my question. The type of keys are always the same and are tested beforehand:
name
is a stringmirror
is a stringurl
is a stringlistChaps
is an "array" inside a stringts
and upts
are integersBTW, obj
is an object, but I think that it's almost impossible to miss. Also, this is a Chrome extension, but I don't think that's relevant. Complete script here.
How Does JSON Parse Work? The JSON parse function takes data that's in a text format and then converts it into JavaScript. Usually, these take the form of JavaScript objects, but the parse function can be used directly on arrays as well.
JSON. stringify will omit all object attributes that are undefined .
You want to be checking for undefined , not null . If you want to cover for all falsy values (null, undefined, zero...) you may just turn the first line into : if (this. ItemAttributes.
JSON as a string json or profiles. json above, that file actually contains text in the form of a JSON object or array, which happens to look like JavaScript. Just like with text files, if you want to use JSON in your project, you'll need to parse or change it into something your programming language can understand.
undefined
is not a valid JSON token. When converting an undefined value to JSON, the correct practice is to render it as null.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With