W3C home > Mailing lists > Public > public-script-coord@w3.org > April to June 2012

Re: [WebIDL] toJSON

From: Cameron McCormack <cam@mcc.id.au>
Date: Mon, 25 Jun 2012 11:41:19 +1000
Message-ID: <4FE7C1BF.908@mcc.id.au>
To: David Bruant <bruant.d@gmail.com>
CC: "public-script-coord@w3.org" <public-script-coord@w3.org>
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

This archive was generated by hypermail 2.3.1 : Wednesday, 8 May 2013 19:30:06 UTC