@@ -652,12 +652,32 @@ and [`url.format()`][] methods would produce.
652
652
* Returns: {String}
653
653
654
654
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() ` ] [ ] .
656
656
657
657
Because of the need for standard compliance, this method does not allow users
658
658
to customize the serialization process of the URL. For more flexibility,
659
659
[ ` require('url').format() ` ] [ ] method might be of interest.
660
660
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
+
661
681
### Class: URLSearchParams
662
682
663
683
The ` URLSearchParams ` API provides read and write access to the query of a
@@ -925,3 +945,5 @@ console.log(myURL.origin);
925
945
[ `urlSearchParams.entries()` ] : #url_urlsearchparams_entries
926
946
[ `urlSearchParams@@iterator()` ] : #url_urlsearchparams_iterator
927
947
[ stable sorting algorithm ] : https://en.wikipedia.org/wiki/Sorting_algorithm#Stability
948
+ [ `JSON.stringify()` ] : https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
949
+ [ `url.toJSON()` ] : #url_url_tojson
0 commit comments