2

I know it is possible to convert a JavaScript data object to JSON using JSON.stringify. e.g:

var cat = {
    soundString: 'Meow!'
};

Then call JSON.stringify(cat) to get "{"soundString":"Meow!"}"

I am interested to know if it is possible to have a parallel to this, except instead of turning the JavaScript object into JSON, turn it into a JavaScript string that can be evaluated back to the object.

var cat = {
    meow: function() {
        console.log('Meow!');
    }
};

I want something that would take the cat object and produce a string "{meow: function(){console.log('Meow!');}}", which can be parsed back using eval.

Can this be accomplished?

10
  • Comes down to "can I serialize a function/getter/setter", right? Commented Jul 7, 2016 at 22:15
  • If I can serialize/encode objects containing functions in a way that can be evaluated/deserialize back to the object. Commented Jul 7, 2016 at 22:15
  • Non-native functions can be serialized by getting the code of the function using the toString() method. You would either have to invent your own serialization format or use actual Javascript that the other end would run eval() on (with the inherent risks of doing that). Commented Jul 7, 2016 at 22:17
  • @jfriend00 but can i extract the actual code from the function in the object? If I understand it correctly, it gets compiled to native as soon as you create the object, so restoring the code seems difficult unless you keep a copy of the code alongside the object as you mentioned via custom toString/prototype.getCode. Commented Jul 7, 2016 at 22:18
  • 1
    Yes, the actual code is still available. See example: jsfiddle.net/jfriend00/21kkegkh. Also see MDN on Function.prototype.toString(). Commented Jul 7, 2016 at 22:20

1 Answer 1

1

Write a recursive deep copy method and check for Array.isArray(item)

Use .hasOwnProperty to avoid the prototype chain

Put additional quotations wherever you want

var str = "{"
for (var key in cat) {
    str += key + ":" + cat[key] + ","
}
str = str.substr(0, str.length-1) + "}"
Sign up to request clarification or add additional context in comments.

4 Comments

props for using 'for in loop' in exactly where it was meant to! I always forget and just avoid it. And even elegantly removing trailing comma. It solves the problem.
One note though, need to append '(' and post-append ')' for it to be evaluatable back to the object, but otherwise it works! Here is my final result: hastebin.com/eyacegaduq.coffee . Worth noting that it is only one level copy, and isn't exhaustive, but it's cool to know its doable.
Actually im not even sure that it is a one level copy. it seems to work on multilevel as well. This is really cool!
no it's not a deep copy, i was just lazy. you will get an issue with embedded objects and arrays. primitives will be missing quotes as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.