OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Why is Date behaving quirkily in JSON.stringify replacer? [duplicate]

  • Thread starter Thread starter Wang Tang
  • Start date Start date
W

Wang Tang

Guest
I have JS objects that I need to stringify (to insert into Postgres). These objects contain Dates, which need to be saved as timestamps rather than ISO representations.

In the context our my application, this is most conveniently done via a JSON.stringify replacer function (https://developer.mozilla.org/en-US...eference/Global_Objects/JSON/stringify#syntax).

However, Date behaves very weirdly in there. Consider:


Code:
function replacer(key, value) {
    if (key != "temp") {
        return value
    }
    console.log(`[${key}]=${value}`)
    let areWeObjectivelyDating = this[key] instanceof Date
    let areWeSubjectivelyDating = value instanceof Date
    console.log("are we dating?")
    if (areWeObjectivelyDating) {
        console.log("> it's a date!")
    }
    else {
        console.log("> objectively, no")
    }
    if (areWeSubjectivelyDating) {
        console.log("> from my perspective? yes!")
    }
    else {
        console.log("> sorry I don't see us working out")
    }
    console.log()
    return value
}

let json = {
    temp: new Date()
}

console.log("= check replacer solo =")
replacer("temp", json["temp"])

console.log("")

console.log("= check replacer in stringify =")
JSON.stringify(json, replacer)

The output is:

Code:
= check replacer solo =
[temp]=Wed Jun 26 2024 09:20:53 GMT+0000 (Coordinated Universal Time)
are we dating?
> objectively, no
> from my perspective? yes!

= check replacer in stringify =
[temp]=2024-06-26T09:20:53.371Z
are we dating?
> it's a date!
> sorry I don't see us working out

The solo check works as expected. But when replacer is called by JSON.stringify, I can access json["temp"] via this, which gives me the actual Date object. However, value is the date already converted to an ISO string.

Why is this the case? Where is this (unexpected) behavior documented?

<p>I have JS objects that I need to stringify (to insert into Postgres). These objects contain <code>Date</code>s, which need to be saved as timestamps rather than ISO representations.</p>
<p>In the context our my application, this is most conveniently done via a <code>JSON.stringify</code> replacer function (<a href="https://developer.mozilla.org/en-US...eference/Global_Objects/JSON/stringify#syntax" rel="nofollow noreferrer">https://developer.mozilla.org/en-US...eference/Global_Objects/JSON/stringify#syntax</a>).</p>
<p>However, <code>Date</code> behaves very weirdly in there. Consider:</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>function replacer(key, value) {
if (key != "temp") {
return value
}
console.log(`[${key}]=${value}`)
let areWeObjectivelyDating = this[key] instanceof Date
let areWeSubjectivelyDating = value instanceof Date
console.log("are we dating?")
if (areWeObjectivelyDating) {
console.log("> it's a date!")
}
else {
console.log("> objectively, no")
}
if (areWeSubjectivelyDating) {
console.log("> from my perspective? yes!")
}
else {
console.log("> sorry I don't see us working out")
}
console.log()
return value
}

let json = {
temp: new Date()
}

console.log("= check replacer solo =")
replacer("temp", json["temp"])

console.log("")

console.log("= check replacer in stringify =")
JSON.stringify(json, replacer)</code></pre>
</div>
</div>
</p>
<p>The output is:</p>
<pre><code>= check replacer solo =
[temp]=Wed Jun 26 2024 09:20:53 GMT+0000 (Coordinated Universal Time)
are we dating?
> objectively, no
> from my perspective? yes!

= check replacer in stringify =
[temp]=2024-06-26T09:20:53.371Z
are we dating?
> it's a date!
> sorry I don't see us working out
</code></pre>
<p>The solo check works as expected. But when <code>replacer</code> is called by <code>JSON.stringify</code>, I can access <code>json["temp"]</code> via <code>this</code>, which gives me the actual <code>Date</code> object. However, <code>value</code> is the date already converted to an ISO string.</p>
<p>Why is this the case? Where is this (unexpected) behavior documented?</p>
 

Latest posts

Top