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

> We need to find out how to handle the special text record
> 
> ```js
>   const records = [
>     {
>       recordType: "text",
>       lang: "en-US",
>       encoding: "utf-8",
>       data: encoder.encode("a text string");
>     }
>   ];
> ```
> 

How about making optional `lang` (default to user agent default locale) and `encoding` (default to utf-8) and keeping `data` a string for "text" recordType?

Note that this improvement is orthogonal to this issue, but still needed for "text" recordType as web developers needs to be able to set `lang` and `encoding`.

```js
// Both would be equivalent for an english user.
const records = [
    {
      recordType: "text",
      lang: "en-US",
      encoding: "utf-8",
      data: "a text string",
    }
  ];
const records = [
    {
      recordType: "text",
      data: "a text string",
    }
  ];
```

For info, `createTextRecord` on Android uses UTF-8 while Core NFC (iOS) seems to use UTF-16 encoding.

> We should throw when encoding is different from utf-8 on other text based records. Also throw on the wrong kind of utf-16 (as there are two :))
> 
> ```js
>      case "text":
>           console.log(`Text: ${new TextDecoder(record.encoding).decode(record.data)}, language ${record.lang}`);
>           break;
> ```

Exposing those seems good to me. Thank you!


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

Received on Tuesday, 15 October 2019 13:33:11 UTC