Re: [WebIDL] toJSON

David Bruant:
> My reading of the current state of the spec is that
> JSON.stringify(anyEvent) is '{}' which is close to useless. This is true
> for any WebIDL compliant object.
> ECMAScript 5 introduced JSON and the toJSON property. Could it be
> considered to make something useful out of WebIDL compliant objects
> using this property?

I've added "serializers" to the spec, which allow you to define a toJSON 
method, like stringifiers let you define a toString.

   interface Whatever {
     readonly attribute DOMString blah;

     serializer;
   };

then you define in prose what the serialization behaviour for Whatever 
is.  If you've got an operation that returns a value you want to be the 
serialization behaviour, you can stick it on there:

   interface Something {
     serializer Whatever getWhatever();
   };

You can also define serialization behaviour inline in the IDL, which I 
imagine will cover common cases:

   interface Whatever {
     readonly attribute DOMString blah;
     readonly attribute long count;
     readonly attribute boolean isIt;

     serializer = { blah, count };
   };

     => toJSON returns { blah: String, count: Number }

   interface Whatever {
     readonly attribute DOMString blah;
     readonly attribute long count;
     readonly attribute boolean isIt;

     serializer = { attribute };
   };

     => toJSON returns { blah: String, count: Number, isIt: Boolean }
        i.e. all attributes

   interface NamedStrings {
     getter DOMString get(DOMString name);
     setter DOMString set(DOMString name, DOMString value);

     serializer = { getter };
   };

     => toJSON returns an object of the named properties

   interface List {
     getter DOMString get(unsigned long index);
     setter void set(unsigned long index, DOMString value);
     readonly attribute unsigned long length;

     serializer = [ getter ];
   };

     => toJSON returns an Array of the indexed properties

http://dev.w3.org/2006/webapi/WebIDL/#idl-serializers
http://dev.w3.org/2006/webapi/WebIDL/#es-serializer

http://dev.w3.org/cvsweb/2006/webapi/WebIDL/Overview.xml.diff?r1=1.546;r2=1.549;f=h

Received on Monday, 25 June 2012 01:41:55 UTC