This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
buffer: change output of Buffer.prototype.toJSON()
Expand the JSON representation of Buffer to include type information so that it can be deserialized in JSON.parse() without context. Fixes #5110. Fixes #5143.
- Loading branch information
1 parent
9b8dd39
commit 840a29f
Showing
3 changed files
with
25 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
840a29f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ei, the example code of buf.toJSON() in official doc (http://nodejs.org/docs/v0.11.13/api/buffer.html#buffer_buf_tojson) doesn't use buf.toJSON()!!!!!!!!!!
If I replace this line: var json = JSON.stringify(buf) by this line: var json=buf.toJSON(), running that example I get this output (node v0.11.13, Fedora 20):
{ type: 'Buffer', data: [ 116, 101, 115, 116 ] }
undefined:1
[object Object]
^
SyntaxError: Unexpected token o
at Object.parse (native)
at Object. (/home/q2dg/borrar:9:17)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:349:32)
at Function.Module._load (module.js:305:12)
at Function.Module.runMain (module.js:490:10)
at startup (node.js:124:16)
at node.js:807:3
instead of the expected:
{"type":"Buffer","data":[116,101,115,116]}
<Buffer 74 65 73 74>
840a29f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@q2dg - it might be helpful to have an example that actually calls
toJSON()
, but this function is typically called internally byJSON.stringify()
. Taken from MDN:In this particular case,
toJSON()
returns an object, not a string. So it makes sense thatJSON.parse()
would throw.840a29f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cjihrig Ooh, I see!! Thanks a lot!!