Skip to content

Loading…

Fix #5110. #5143

Closed
wants to merge 1 commit into from

3 participants

@NodeGuy

Expand the JSON representation of Buffer to include type information
so that it can be deserialized in JSON.parse without context.

@TooTallNate TooTallNate commented on an outdated diff
doc/api/buffer.markdown
((6 lines not shown))
- var copy = new Buffer(JSON.parse(json));
+ var copy = JSON.parse(json, function(key, value) {
+ return (value instanceof Object) && (value.type == 'Buffer')

instanceof Object is probably overkill. I don't think any JS native data types have a type property.

@bnoordhuis Node.js Foundation member

Well, it catches null. But you could also write it as value && value.type === 'Buffer'

Ya, that's a little cleaner. @BallBearing could you update? Don't forget the test case as well.

@NodeGuy
NodeGuy added a note

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@TooTallNate

Also you're gonna need to make sure you've signed the CLA and reformat the commit message according to: https://github.com/joyent/node/blob/master/CONTRIBUTING.md

David Braun 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.
49d7e2e
@NodeGuy

Also you're gonna need to make sure you've signed the CLA and reformat the commit message according to:
https://github.com/joyent/node/blob/master/CONTRIBUTING.md

Already done and done.

@TooTallNate TooTallNate added a commit that closed this pull request
David Braun 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.
840a29f
@TooTallNate

Thanks David, landed in 840a29f.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 26, 2013
  1. buffer: Change output of Buffer.prototype.toJSON.

    David Braun committed
    Expand the JSON representation of Buffer to include type information
    so that it can be deserialized in JSON.parse without context.
    
    Fixes #5110.
Showing with 25 additions and 8 deletions.
  1. +8 −5 doc/api/buffer.markdown
  2. +4 −1 lib/buffer.js
  3. +13 −2 test/simple/test-buffer.js
View
13 doc/api/buffer.markdown
@@ -118,9 +118,8 @@ See `buffer.write()` example, above.
### buf.toJSON()
-Returns a JSON-representation of the Buffer instance, which is identical to the
-output for JSON Arrays. `JSON.stringify` implicitly calls this function when
-stringifying a Buffer instance.
+Returns a JSON-representation of the Buffer instance. `JSON.stringify`
+implicitly calls this function when stringifying a Buffer instance.
Example:
@@ -128,9 +127,13 @@ Example:
var json = JSON.stringify(buf);
console.log(json);
- // '[116,101,115,116]'
+ // '{"type":"Buffer","data":[116,101,115,116]}'
- var copy = new Buffer(JSON.parse(json));
+ var copy = JSON.parse(json, function(key, value) {
+ return value && value.type === 'Buffer'
+ ? new Buffer(value.data)
+ : value;
+ });
console.log(copy);
// <Buffer 74 65 73 74>
View
5 lib/buffer.js
@@ -381,7 +381,10 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
Buffer.prototype.toJSON = function() {
- return Array.prototype.slice.call(this, 0);
+ return {
+ type: 'Buffer',
+ data: Array.prototype.slice.call(this, 0)
+ };
};
View
15 test/simple/test-buffer.js
@@ -853,8 +853,19 @@ Buffer(Buffer(0), 0, 0);
});
-// GH-3905
-assert.equal(JSON.stringify(Buffer('test')), '[116,101,115,116]');
+// GH-5110
+(function () {
+ var buffer = new Buffer('test'),
+ string = JSON.stringify(buffer);
+
+ assert.equal(string, '{"type":"Buffer","data":[116,101,115,116]}');
+
+ assert.deepEqual(buffer, JSON.parse(string, function(key, value) {
+ return value && value.type === 'Buffer'
+ ? new Buffer(value.data)
+ : value;
+ }));
+})();
// issue GH-4331
assert.throws(function() {
Something went wrong with that request. Please try again.