Re: [heycam/webidl] Consider simplifying serializers (#188)

So one thing I realized might be a problem with the default serializer + custom toJSON approach is that it introduces an inconsistency in whether there's a Get() performed. That is, given the following hypothetical future Web IDL:

```js
interface A {
  attribute DOMString x;
  attribute DOMString y;
  
  toJSON = default; // or whatever, e.g. `jsonifier;` or `[DefaultSerializer] object toJSON();`
};

interface B {
  attribute DOMString x;
  attribute DOMString y;
  
  BJSON toJSON();
};

dictionary BJSON {
  DOMString x;
};

// In prose, define that `B`'s `toJSON()` returns a dictionary initialized
// with the value of `B`'s associated x.
```

it appears that `aInstance.toJSON()` will perform two Get()s (for x and y), whereas `bInstance.toJSON()` will perform zero gets (since it is written in the normal style of all Web IDL operations and accesses "internal slots" or "associated values").

I think we should ensure consistency somehow, besides just requiring spec writers to do manual Get()s. For example:

- We could provide an abstract operation `SerializeToJSON(platformObject, listOfProps)` where if `listOfProps` is omitted it uses all the attributes in the definition. (It does *not* do property enumeration.) Then we could require every toJSON() method definition to delegate to this.
- We could use an extended attribute: `[Serializer] object toJSON()` vs. `[Serializer=(x)] object toJSON()`. Might get unwieldy for longer cases.
- We could go with some variation on the existing syntax. Maybe `serializer;` vs. `serializer = { x };`.

We also want to handle the case of serializing to the value of a specific attribute, so that `url.toJSON()` returns `url.href`. Maybe that's `SerializeSinglePropertyToJSON(platformObject, prop)` or `[AttributeSerializer=prop]` or `serializer = prop`.

This is still simpler than the existing spec in the following respects:

- Does not allow returning arbitrary mappings, only those derived from attributes
- Does not allowing opting in or out of inheritance. (The default behavior always includes inherited attributes.)
- Does not allow named property serialization
- Does not allow array serialization
- Does not allow arbitrary serialization behavior defined in prose

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/heycam/webidl/issues/188#issuecomment-271436381

Received on Monday, 9 January 2017 23:05:27 UTC