How to ignore file.loadAsObjec(), if file is not a valid json
-
local obj = file.loadAsObject(); if (!isDefined(obj)) obj = "";
-
not working
... I also tried this option, and I get the same error on the line where loadAsObject is -
@deniskorg What do you have on the line before?
-
inline function checkFileAssignBankCC(file) { if(!file.isFile()) return -1; if(typeof file !== "object" || file == undefined || file == null) return -1; local objFile = file.loadAsObject(); if (!isDefined(objFile)) objFile = ""; if(!isDefined(objFile.bankCC)) return -1; if(parseInt(objFile.bankCC.length) == 0 || parseInt(objFile.bankCC.length) !== 14) return -1; for(i = 0; i < objFile.bankCC.length; i++) { if(typeof i !== "number") return -1; }; return true; };
-
parseInt(objFile.bankCC.length) == 0 for this... i need to use parse int, only in that way is work
is a bug or ? -
and for more context this is in the invalid file :
{
"bankCC": [
-1
-1
]
}that comma is missing
-
inline function checkFileAssignBankCC(file) { if (!isDefined(file) || !file.isFile()) // We can check for undefined here return -1; // Why -1 ? /// This if statement isn't needed if (typeof file !== "object" || file == undefined || file == null) return -1; };
@deniskorg said in How to ignore file.loadAsObjec(), if file is not a valid json:
parseInt(objFile.bankCC.length) == 0 for this... i need to use parse int, only in that way is work
That shouldn't be needed, if the JSON is valid.
Since in your case there is the potential the user could use invalid JSON. So I would recommend you load the file as a string, then parse it as JSON, and check if it's valid.
local s = file.loadAsString(); local obj = s.parseAsJSON(); if (obj == null) Console.print("invalid"); else Console.print(trace(obj));
-
thank you David ,
local s = file.loadAsString();
local obj = s.parseAsJSON();if i use this it work
-
i use return -1 :)) because this check func is used in other func and and i get numbers for diff errors, and other things
/// This if statement isn't needed
if (typeof file !== "object" || file == undefined || file == null) return -1;I thought it was a problem for that file and that's why I was getting an error at loadAsObject,
but your version is a working method ... with loadAsString , and after parseAsJson,THANK YOU
-
And David...maybe I'm wrong somewhere but if i use this
const ARR_TEST = [1, 2, 3];
if(ARR_TEST.length !== 3)
Console.print("test");but if i use this it work
if(parseInt(ARR_TEST.length) !== 3)
Console.print("test"); -
@deniskorg said in How to ignore file.loadAsObjec(), if file is not a valid json:
if(parseInt(ARR_TEST.length) !== 3)
Try
if (ARR_TEST.length != 3)
-
@d-healey it works, thank you