Functions in JSON

5,658 views
Skip to first unread message

Laser Lips

unread,
Jul 17, 2008, 7:31:46 AM7/17/08
to JSON Schema
Hi, how can we decalre functions in JSON and call them e.g.

var JSON = {
"MyVar":"Hello",
"ThisVar":"Hello again",
"function":alert('hello');

}
alert(JSON.MyVar);
alert(JSON.ThisVar);
JSON.function;

Thanks

Ian Lewis

unread,
Jul 21, 2008, 6:46:21 AM7/21/08
to json-...@googlegroups.com
Cute. I wouldn't do this at all because JSON's spec doesn't really
include functions but if you must, you could use the function name and
call it by other means in javascript.

2008/7/17 Laser Lips <louds...@gmail.com>:

Stephen McKamey

unread,
Jul 21, 2008, 10:37:30 AM7/21/08
to JSON Schema
This is really more of a question for JSON than the JSON Schema
group. That being said as Ian mentioned, JSON explicitly excludes
functions because it isn't meant to be a JavaScript-only data
structure (despite the JS in the name).

One alternative you do have within JavaScript is to essentially bind
functions as appropriate when deserializing. For instance, you can
pass in an optional "reviver" function into the JSON.parse method and
this can determine which if any functions should be added to the
object. (see http://www.json.org/js.html for the details) The
canonical example allows certain strings to be converted into Dates,
but your situation could involve adding functions to certain objects
after parsing and before returning them.

Hope this helps.

Kris Zyp

unread,
Jul 21, 2008, 11:36:53 AM7/21/08
to JSON Schema
If you are writing your data for a consumer that is known to be JavaScript
capable (like a web browser), you can simply use full JavaScript object
literals with any valid JavaScript value, including functions, instead of
just using the JSON subset. You can't (honestly) call it JSON anymore, but
it is still JavaScript object literals (from which JSON is derived):

I wanted to propose a specific extension format for using JSON Schema for
JavaScript environments. This is not intended to alter the base
language-agnostic JSON-conformant JSON Schema specification, but is an
extension for JavaScript-specific use. For JavaScript specific JSON Schema
(maybe call it JavaScript Schema, or JSON Schema for JavaScript), a JSON
Schema could include an additional property "prototype" that defines what
the prototype (__proto__) of all object instances would be. For example, we
could write:

{
type:"object",
properties:{
name:{type:"string"}
},
prototype:{
getName: function(){
return this.name;
},
setName: function(name){
this.name = name;
}
}
}

This would indicate to a JavaScript consumer that all valid instances of
this schema should be an object with a name property that is a string, and
it's prototype should be the provided prototype object, such that one could
call object.getName().

Of course, schema providers must ensure that they do not send schemas
functions to non-JavaScript parsing JSON consumers. Unknown properties in a
schema (like "prototype" for non-JavaScript consumers) are supposed to be
ignored, so there really isn't a problem with sending the prototype property
object to pure JSON consumers as long as all the properties within it are
valid JSON.

If you are using a JSON library that validates (like Crockford's JSON
library), you have to disable validation if you want to parse functions, and
simply do the eval('(' + json + ')'). Of coures without validation, you are
responsible for making sure that safe JSON/JS is delivered from the server.

Thanks,
Kris

Reply all
Reply to author
Forward
0 new messages