0

I want to send a javascript object like this one through a $.ajax request :

 var o = {
   a: 1, 
   b: 'dummy string', 
   c: ['a', 1, {}], 
   d: {dd: 1}, 
   e: new Date(), 
   f: function() {
        console.log('here');
   }
}

I know that i normally should use JSON.stringify before sending it to my php script. The problem is JSON.stringify, removes the properties it can't stringify :

JSON.stringify(o);

returns this ->

"{
  "a":1,
  "b":"dummy string",
  "c":["a",1,{}],
  "d":{"dd":1},
  "e":"2015-11-13T21:34:36.667Z"
}"

But what shoud i do, if i want to store in a mysql column the "o" javascript object as plain text like this :

o = {
   a: 1, 
   b: 'dummy string', 
   c: ['a', 1, {}], 
   d: {dd: 1}, 
   e: new Date(), 
   f: function() {
        console.log('here');
   }
}
12
  • Why do you need to send it? Commented Nov 13, 2015 at 21:57
  • What is the PHP script going to do with a Javascript function? Commented Nov 13, 2015 at 21:58
  • You may be able to use the replacer function argument of JSON.stringify() to encode the parameters that it natively would strip out if for some reason you actually need to keep them. Commented Nov 13, 2015 at 21:58
  • @MartinVseticka Because i want to store the javascript code as plain text. Commented Nov 13, 2015 at 22:01
  • 1
    @wawawoom Yes, in that case I would have the snippet in a textrea most likely and I would just store the text.. Commented Nov 13, 2015 at 22:18

2 Answers 2

3

You could try this:

var o = {
   a: 1, 
   b: 'dummy string', 
   c: ['a', 1, {}], 
   d: {dd: 1}, 
   e: new Date(), 
   f: function() {
        console.log('here');
   }
};

o.f = o.f.toString();
var stringy = JSON.stringify(o);
document.getElementById('test2').innerHTML = stringy;

Fiddle: http://jsfiddle.net/e2cxandt/

Obviously this needs to be changed a bit so you aren't overwriting the function by maybe cloning the object but as a quick example you can see it now has the property in the string.

As someone mentioned in the comments above, war10ck, here is an example of using the replacer argument of JSON.stringify

var o = {
   a: 1, 
   b: 'dummy string', 
   c: ['a', 1, {}], 
   d: {dd: 1}, 
   e: new Date(), 
   f: function() {
        console.log('here');
   }
};

function replacer (key, value) {
  if (typeof value === "function") {
    return value.toString();
  }
  return value;
}

var stringy2 = JSON.stringify(o, replacer);
document.getElementById('test2').innerHTML = stringy2;

Fiddle: http://jsfiddle.net/e2cxandt/1/

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, actually i've been working with a plugin called JSONfn (github.com/vkiryukhin/jsonfn), and it do the job, but i wanted to know if there was another solution
Cool, no problem! There may be other ways, this is just one way.
More generally do you think it's possible to send the object WITHOUT stringify it ?
@wawawoom no, that's not possible, it has to be a string to be sent via http.
Without doing stringify on object as a whole or without doing toString on the function property? I don't believe you can do it without making the entire object a string, as for handling the property thats a function, there could be other ways I am not aware of.
|
1

a couple universal (non-specific-instance) options:

you can define a custon toJSON on any object:

Function.prototype.toJSON=function(){return String(this);}; 

JSON.stringify({a:1, b:function(){alert(123)}});

which shows:

{"a":1,"b":"function (){alert(123)}"}

the one caveat is that your function literal is quoted as a string and no longer a function. to fix that if needed, you can use a reviver parameter to JSON.parse().

a better option: using a replace arg to JSON.stringify():

JSON.stringify({a:1, b:function(){alert(123)}}, function(k,v){
  if(typeof v==="function") return String(v);
  return v;
});

which shows:

{"a":1,"b":"function (){alert(123)}"}

that way is particularly nice because you need not modify an built-in or instance objects.

2 Comments

Modifying the prototypes of built-in types is generally a bad idea.
i agree 100%, this is a simple demo. you can define .toJSON() as an own property on a function instance, or do some fancy ES15 subclassing, or just turn it into a string on the object before stringifying.

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.