Re: [web-nfc] What's the value of `toJSON()`? (#366)

https://w3c.github.io/web-nfc/#dfn-convert-ndefrecord-payloaddata-bytes seems really inconsistent with the web platform.

Firstly with naming. Other APIs such as fetch and push use `text()` and `json()` rather than `toText()` and `toJSON()`. This was done deliberately since `toJSON()` is kinda like `toString()` in that it's used as a way to 'cast' an object to a particular type. This means if an `NDEFRecord` object is processed as part of `JSON.stringify()`, it'll call the `toJSON()` methods. This is weird since the return value of `NDEFRecord`'s `toJSON()` method doesn't return a JSON-compatible representation of itself, which is the intent of the `toJSON()` method elsewhere on the platform.

Additionally, the conversion reacts to the `recordType`. If I ask for a record to be parsed as JSON, but the type isn't 'json' or 'opaque', it returns null. This is weird because I've *asked* for the record to be read as JSON. If it can't be read as JSON, it should throw.

If the developer has indicated something should be interpreted as a type, we don't generally 'succeed' if the other end has declared itself to be a different type. Eg, if I use an `<img>` to load something that's not an image, it'll fire the error event, even if its content-type indicates it isn't an image (`text/javascript` for instance).

In some cases we fail if the content declares itself to be another type (eg, we check the content-type for module scripts), but will still error.

In fetch and push, if you call `.json()`, the content will be parsed as json, regardless of the content type of the response. If the content cannot be parsed as JSON, it throws/rejects.

-- 
GitHub Notification of comment by jakearchibald
Please view or discuss this issue at https://github.com/w3c/web-nfc/issues/366#issuecomment-540991007 using your GitHub account

Received on Friday, 11 October 2019 09:29:33 UTC