HISE Logo Forum
    • Categories
    • Register
    • Login

    How to ignore file.loadAsObjec(), if file is not a valid json

    Scheduled Pinned Locked Moved General Questions
    13 Posts 2 Posters 489 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • d.healeyD
      d.healey @deniskorg
      last edited by

      @deniskorg

      local obj = file.loadAsObject();
      
      if (!isDefined(obj))
          obj = "";
      
      

      Libre Wave - Freedom respecting instruments and effects
      My Patreon - HISE tutorials
      YouTube Channel - Public HISE tutorials

      deniskorgD 1 Reply Last reply Reply Quote 0
      • deniskorgD
        deniskorg @deniskorg
        last edited by

        not working
        ... I also tried this option, and I get the same error on the line where loadAsObject is

        d.healeyD 1 Reply Last reply Reply Quote 0
        • d.healeyD
          d.healey @deniskorg
          last edited by

          @deniskorg What do you have on the line before?

          Libre Wave - Freedom respecting instruments and effects
          My Patreon - HISE tutorials
          YouTube Channel - Public HISE tutorials

          1 Reply Last reply Reply Quote 0
          • deniskorgD
            deniskorg @d.healey
            last edited by deniskorg

            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;
            };
            
            deniskorgD 1 Reply Last reply Reply Quote 0
            • deniskorgD
              deniskorg @deniskorg
              last edited by

              parseInt(objFile.bankCC.length) == 0 for this... i need to use parse int, only in that way is work
              is a bug or ?

              deniskorgD d.healeyD 2 Replies Last reply Reply Quote 0
              • deniskorgD
                deniskorg @deniskorg
                last edited by

                and for more context this is in the invalid file :
                {
                "bankCC": [
                -1
                -1
                ]
                }

                that comma is missing

                1 Reply Last reply Reply Quote 0
                • d.healeyD
                  d.healey @deniskorg
                  last edited by

                  @deniskorg

                  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));
                  

                  Libre Wave - Freedom respecting instruments and effects
                  My Patreon - HISE tutorials
                  YouTube Channel - Public HISE tutorials

                  deniskorgD 1 Reply Last reply Reply Quote 0
                  • deniskorgD
                    deniskorg @d.healey
                    last edited by deniskorg

                    thank you David ,
                    local s = file.loadAsString();
                    local obj = s.parseAsJSON();

                    if i use this it work

                    deniskorgD 1 Reply Last reply Reply Quote 0
                    • deniskorgD
                      deniskorg @deniskorg
                      last edited by

                      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

                      deniskorgD 1 Reply Last reply Reply Quote 1
                      • deniskorgD
                        deniskorg @deniskorg
                        last edited by

                        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");

                        d.healeyD 1 Reply Last reply Reply Quote 0
                        • d.healeyD
                          d.healey @deniskorg
                          last edited by

                          @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)

                          Libre Wave - Freedom respecting instruments and effects
                          My Patreon - HISE tutorials
                          YouTube Channel - Public HISE tutorials

                          deniskorgD 1 Reply Last reply Reply Quote 0
                          • deniskorgD
                            deniskorg @d.healey
                            last edited by

                            @d-healey it works, thank you

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post

                            22

                            Online

                            1.8k

                            Users

                            11.9k

                            Topics

                            103.9k

                            Posts