Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upUnexpected behavior of JSON.stringify() #741
Comments
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
ljharb
Nov 26, 2016
Member
new Date().toString() produces the former, the latter is new Date().toISOString(). The conversion happens when the Date object is coerced to a string to become the object property key. Object.keys({[new Date()]: 'test'})[0] will produce the same string value - JSON.stringify doesn't come into it.
|
|
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
adius
Nov 26, 2016
I just realized that, too
What I was thinking of in the first place was rather: new Map([[new Date(), 'test']])
…and actually the yield of JSON.stringify(new Map([[new Date(), 'test']])) is also not optimal.
It should rather be {"2016-11-26T14:15:55.380Z": "test"} instead of [["2016-11-26T14:15:55.380Z","test"]].
adius
commented
Nov 26, 2016
|
I just realized that, too What I was thinking of in the first place was rather: It should rather be |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
ljharb
Nov 26, 2016
Member
In fact, it can't, because Maps can have objects as keys, and object keys themselves can only be strings or Symbols. An array of entries (where an "entry" is an array of key, value) is the only way a Map can be represented in a serializable fashion.
|
In fact, it can't, because |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
adius
Nov 26, 2016
is the only way a Map can be represented in a serializable fashion
Yeah, I guess you're right. Otherwise you would have to serialize simple maps as JSON Objects and complex maps as JSON Arrays, which would be very confusing.
Maybe there should be a JSON.stringifyMap() method which simply stringifies each key…
adius
commented
Nov 26, 2016
Yeah, I guess you're right. Otherwise you would have to serialize simple maps as JSON Objects and complex maps as JSON Arrays, which would be very confusing. Maybe there should be a |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
littledan
Nov 26, 2016
Member
Keys of objects are already strings or symbols when constructed. So it would be a little late for that
|
Keys of objects are already strings or symbols when constructed. So it would be a little late for that |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
benjamn
Nov 26, 2016
Member
There are a variety of libraries that attempt to improve on JSON by supporting stringification of objects with prototypes other than Object.prototype or Array.prototype, along with circular and repeated references, another common JSON.stringify gotcha.
Two that come to mind are lave and arson (full disclosure: I am the author of arson).
Here's how the example might work:
> const { encode, decode } = require("arson")
undefined
> map = new Map
Map {}
> map.set(new Date, "test")
Map { 2016-11-26T17:34:28.448Z => 'test' }
> encode(map)
'[["Map",1],[2,3],["Date",4],"test","2016-11-26T17:34:28.448Z"]'
> decode(_)
Map { 2016-11-26T17:34:28.448Z => 'test' }More examples in the README.md.
|
There are a variety of libraries that attempt to improve on Two that come to mind are Here's how the example might work: > const { encode, decode } = require("arson")
undefined
> map = new Map
Map {}
> map.set(new Date, "test")
Map { 2016-11-26T17:34:28.448Z => 'test' }
> encode(map)
'[["Map",1],[2,3],["Date",4],"test","2016-11-26T17:34:28.448Z"]'
> decode(_)
Map { 2016-11-26T17:34:28.448Z => 'test' }More examples in the |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
adius
commented
Nov 26, 2016
|
Thanks for the tip! |
adius commentedNov 26, 2016
yields
{ "Sat Nov 26 2016 13:46:13 GMT+0000 (UTC)": "test" }but I would expect it to yield
{ "2016-11-26T13:46:13.923Z": "test" }