2008/7/17 Laser Lips <louds...@gmail.com>:
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