2 Replies Latest reply on Nov 6, 2012 8:11 AM by bduffy323

    object parse error

    bduffy323 Level 1

      I use the model of working with objects, saving them using toSource() in an objects insertLabel() method. Then when I need them again I just pull them out using extractLabel() and eval(). What I have recently found is that if you have a key with a leading 0, the toSource() eval() routine changes the key value. My work around for this was to include json2.js which can be found on github which adds a .stringify() and .parse() functionality to a global JSON object. I am wondering if this is just specific to indesign CS 6 on a PC or if others experience the same thing. Right now I only have the means of testing in this environment but I am curious as to if it reacts the same with in other environments.



      var dict = {}
      dict["0123"] = "test1";
      dict["2345"] = "test2";
      var tempzz = "";
      for (var tempyy in dict)
          tempzz+="dict["+tempyy+"] =" + dict[tempyy] +"\n";
      var mySource = dict.toSource()
      var myJSON = JSON.stringify(dict);
      var evalSource = eval(mySource);
      var evalJSON = JSON.parse(myJSON);
      var tempzz = "";
      for (var tempyy in evalSource)
          tempzz+="evalSource["+tempyy+"] =" + evalSource[tempyy] +"\n";
      var tempzz = "";
      for (var tempyy in evalJSON)
          tempzz+="evalJSON["+tempyy+"] =" + evalJSON[tempyy] +"\n";



      Any input or clarity would be great! Thanks!

        • 1. Re: object parse error
          Marc Autret Level 4

          Hi bduffy,


          This is a generic issue in ExtendScript. Object keys that sounds like numerals (as they only contains digits) are parsed as numeric values.

          This explains why '0123' becomes '83' (the interpreter sees an octal number: 0123).


          In regular JS:


          alert( {'0123': "foobar"}.toSource() );


          will display: ({'0123':"foobar"}),

          but in ExtendScript you get: ({0123:"foobar"})


          Thus, the quotes are lost in the source string and then the 0123 key will be improperly evaluated as 83!


          IMHO this is a very good reason to never use numerals for associative keys in ExtendScript. When I really need to manage such things I use a prefix, like '_', that prevents the interpreter for parsing any number. Anyway using JSON.stringify() is still an option, although this forces you to include an external library.





          PS: Note that you have similar issue with a '0x' prefix:

          alert( eval({'0x123' : "foobar"}.toSource()).toSource() );

          // => ({291:"foobar"})

          • 2. Re: object parse error
            bduffy323 Level 1

            That makes sense. I can see how it is good practice to use the underscore.



            Thanks for sweeping in and clearing things up, as usual!