Skip to content

Commit 531656d

Browse files
committed
url: implement URL.prototype.toJSON
Refs: whatwg/url#229
1 parent bd07c8f commit 531656d

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

doc/api/url.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -652,12 +652,32 @@ and [`url.format()`][] methods would produce.
652652
* Returns: {String}
653653

654654
The `toString()` method on the `URL` object returns the serialized URL. The
655-
value returned is equivalent to that of [`url.href`][].
655+
value returned is equivalent to that of [`url.href`][] and [`url.toJSON()`][].
656656

657657
Because of the need for standard compliance, this method does not allow users
658658
to customize the serialization process of the URL. For more flexibility,
659659
[`require('url').format()`][] method might be of interest.
660660

661+
#### url.toJSON()
662+
663+
* Returns: {String}
664+
665+
The `toJSON()` method on the `URL` object returns the serialized URL. The
666+
value returned is equivalent to that of [`url.href`][] and
667+
[`url.toString()`][].
668+
669+
This method is automatically called when an `URL` object is serialized
670+
with [`JSON.stringify()`][].
671+
672+
```js
673+
const myURLs = [
674+
new URL('https://www.example.com'),
675+
new URL('https://test.example.org')
676+
];
677+
console.log(JSON.stringify(myURLs));
678+
// Prints ["https://www.example.com/","https://test.example.org/"]
679+
```
680+
661681
### Class: URLSearchParams
662682

663683
The `URLSearchParams` API provides read and write access to the query of a
@@ -1043,3 +1063,5 @@ console.log(myURL.origin);
10431063
[`urlSearchParams.entries()`]: #url_urlsearchparams_entries
10441064
[`urlSearchParams@@iterator()`]: #url_urlsearchparams_iterator
10451065
[stable sorting algorithm]: https://en.wikipedia.org/wiki/Sorting_algorithm#Stability
1066+
[`JSON.stringify()`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
1067+
[`url.toJSON()`]: #url_url_tojson

lib/internal/url.js

+9
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,15 @@ Object.defineProperties(URL.prototype, {
523523
binding.parse(hash, binding.kFragment, null, ctx,
524524
onParseHashComplete.bind(this));
525525
}
526+
},
527+
toJSON: {
528+
writable: true,
529+
enumerable: true,
530+
configurable: true,
531+
// eslint-disable-next-line func-name-matching
532+
value: function toJSON() {
533+
return this[kFormat]({});
534+
}
526535
}
527536
});
528537

test/parallel/test-whatwg-url-properties.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ for (const prop in url) {
2323
const expected = ['toString',
2424
'href', 'origin', 'protocol',
2525
'username', 'password', 'host', 'hostname', 'port',
26-
'pathname', 'search', 'searchParams', 'hash'];
26+
'pathname', 'search', 'searchParams', 'hash', 'toJSON'];
2727

2828
assert.deepStrictEqual(props, expected);
2929

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const URL = require('url').URL;
5+
const { test, assert_equals } = common.WPT;
6+
7+
/* eslint-disable */
8+
/* WPT Refs:
9+
https://github.com/w3c/web-platform-tests/blob/02585db/url/url-tojson.html
10+
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
11+
*/
12+
test(() => {
13+
const a = new URL("https://example.com/")
14+
assert_equals(JSON.stringify(a), "\"https://example.com/\"")
15+
})
16+
/* eslint-enable */

0 commit comments

Comments
 (0)