0

My issue is that I have a very long string to parse (basically an object represented as a string), I'm trying to parse it manually without using eval, I've got more than 1000 lines of looping functions and I'm not even close to complete the algorithm.

I was checking how this was done in jQuery.metadata and they just used eval! my 1000 lines of code can be shrink to just an eval, but is this safe? I've heard that this function is not safe neither fast, but my algorithm is also slow considering all those loops and parsing.

e.g.

<button onajax="{reload:'#someitem',callback: function('somedata'),items:{1,2,3}}">

I need to set

var onajaxargs = {reload:'#someitem',callback: function('somedata'),items:{1,2,3}};
Felishia
  • 61
  • 6
  • 3
    If that object is represented as a JSON string, You better use `JSON.parse()`. – Roman Hocke Sep 17 '14 at 12:21
  • @Roman Hocke : would it parse a string such as "{action : doSomething('some data')}", I'm sorry I'm not so used to JSON – Felishia Sep 17 '14 at 12:30
  • 2
    No, it won't. So Your string is not a JSON, good to know. Just for curiosity: where does that string come from? – Roman Hocke Sep 17 '14 at 12:52
  • I'm trying to create a library as a extension for jQuery that avoids scripting as much as possible, so basically some attributes as onajax="{reload:'data',callback:myfunc('func')}" would do the job and this is obviously an object, according to jQuery.metadata I should use eval, but I'm not sure yet. – Felishia Sep 17 '14 at 13:01

2 Answers2

1

If you can give a little bit more information on what you are trying to do then I can better answer your question, but like Roman said, JSON seems to be the best method for Parsing the string.

This explains how to use JSON.parse() pretty well: Parse JSON in JavaScript?

Community
  • 1
  • 1
staticinteger
  • 93
  • 1
  • 8
0

See Why is using the JavaScript eval function a bad idea? if you have not already.

I would suggest using this approach of first encoding to JSON, and then "reviving" your function members. http://ovaraksin.blogspot.com/2013/10/pass-javascript-function-via-json.html

jsonText='{"reload":"#someitem","callback": "function(somedata) {alert(somedata)}","items":[1,2,3]}';

var jsonTransformed = JSON.parse(jsonText, function (key, value) {
        if (value && (typeof value === 'string') && value.indexOf("function") === 0) {
            // we can only pass a function as string in JSON ==> doing a real function
            var jsFunc = new Function('return ' + value)();
            return jsFunc;
        }

        return value;
});

Several observations:

  1. You need to have a canonical JSON to make sure it works - all strings and names of members to be enclosed in double quotes.
  2. You would need to provide proper escaping of the quotes (single and double) in the string.
  3. You need to make the function members to contain the text "function" in the beginning of the string.
  4. Array members to be enclosed with square brackets, not with curly braces as in your original post.
Community
  • 1
  • 1
Zlatin Zlatev
  • 3,034
  • 1
  • 24
  • 32
  • I'm sorry but it doesn't work for my needs, I tried a mixed way, I created some rules and a small parser, even so, when an exception comes a function is created just like var jsFunc = new Function('return ' + value)() to get its value, which is very much like eval. – Felishia Oct 01 '14 at 12:36