- From: François Beaufort via GitHub <sysbot+gh@w3.org>
- Date: Thu, 10 Oct 2019 15:21:58 +0000
- To: public-web-nfc@w3.org
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