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

beaufortfrancois has just created a new issue for https://github.com/w3c/web-nfc:

== What's the value of `toJSON()`? ==
Is `NDEFRecord.toJSON()` doing more than `JSON.parse(NDEFRecord.toText())`? I guess it doesn't raise an error when payload is not a valid JSON message but I'd like to hear from you what is its value.

```js
// With
if (record.mediaType === 'json') {
  const data = record.toJSON();
  if (data) {
    if ('someKey' in data) { /* ... */ }
  } else {
    // Show error
  }
}

// Without
if (record.mediaType === 'json') {
  try {
    const data = JSON.parse(record.toText());
    if ('someKey' in data) { /* ... */ }
  } catch(e) {
    // Show error
  }
}
```

At a certain extent, `toArrayBuffer()` is all it's needed to get text/json as well.

```js
const decoder = new TextDecoder();
const text = decoder.decode(record.toArrayBuffer());
const data = JSON.parse(text);
// etc.
```


Please view or discuss this issue at https://github.com/w3c/web-nfc/issues/366 using your GitHub account

Received on Thursday, 10 October 2019 15:22:00 UTC