In JSON.stringify implementation, replacer function's this value is not set to a containing object as required by specs (and implemented in all other engines).
WebKit defines it as follow:
You can provide an optional replacer method. It will be passed the
key and value of each member, with this bound to the containing
object. The value that is returned from your method will be
serialized. If your method returns undefined, then the member will
be excluded from the serialization.
MDN defines it as follow:
As a function, it takes two parameters: the key and the value being stringified. The object in which the key was found is provided as the replacer's this parameter.
Qt/QML V4 engine, however, violates this twice at once:
1. this is set to a new object with a value for an empty ("") key;
2. value on this is the same already toJSON() serialized value as the second argument.
…which is kinda defeats the purpose, and buggy as hell. I wonder, how it happened that no tests exist to check such behavior.
I wanted to serialize Date objects as number of seconds since epoch (because that's how they expect it on server API). Normally, I would set a global replacer function in my HTTP request sending flow:
It works in browsers and nodejs CLI, but fails miserably in Qt.
|For Gerrit Dashboard: QTBUG-95324|
|361930,1||Set correct `this` value in JSON.stringify replacer scope||dev||qt/qtdeclarative||Status: NEW||0||0|