Its converted to string first, see spec 
at http://www.ecma-international.org/ecma-262/5.1/#sec-15.12.2

For example, this results in [1, 2, 3] array :

> var qqq = {}
undefined
> qqq.toString = function () { return "[1, 2, 3]"; }
[Function]
> JSON.parse(qqq)

Buffer.toString is defined 
here: 
https://github.com/nodejs/node/blob/c41c09375b217d9daec614edb590147b14ff9b21/lib/buffer.js#L405
By default, its "return string using buffer bytes and interpreting them as 
utf8 sequence"

On Thursday, 4 February 2016 08:28:10 UTC+11, Chethiya Abeysinghe wrote:
>
> Following works in nodejs 4.2.3. But I don't see any documentation saying 
> JSON.parse() accepts a buffer. Is this behavior guaranteed but not 
> documented oris there something I'm doing wrong? 
>
> o = {"tes": 1, "4 byte utf8": "𠜎 𠜱 𠝹 𠱓"};    // { tes: 1, '4 byte 
> utf8': '𠜎 𠜱 𠝹 𠱓' }
> buffer = new Buffer(JSON.stringify(o), 'utf8');  // <Buffer 7b 22 74 65 73 
> 22 3a 31 2c 22 34 20 62 79 74 65 20 75 74 66 38 22 3a 22 f0 a0 9c 8e 20 f0 
> a0 9c b1 20 f0 a0 9d b9 20 f0 a0 b1 93 22 7d>
> JSON.parse(buffer);       // { tes: 1, '4 byte utf8': '𠜎 𠜱 𠝹 𠱓' }
>
> Thanks
>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/a58fe332-af57-4600-89d6-702ff449df8a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to