Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8715009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:44:34+00:00 2026-06-13T05:44:34+00:00

Scenario After reading this answer I realized that I could create object starting from

  • 0

Scenario

After reading this answer I realized that I could create object starting from a JSON literal.

So I guessed that I could do the opposite just using this useful JSON method:
JSON.stringify(myObject).

So I did as follow:

function MyObject(id, value, desc)
{
  this.id = id;
  this.value = value;
  this.desc = desc;
  this.toJSON = function()
  {
    return JSON.stringify(this);
  }

}

But when I run this stuff (demo) a Maximum call stack size exceeded error occurs.

After googling a bit, I found two references that explain this behaviour:

  • the JSON.stringify() method at MDN.
  • the JSON in Javascript article at JSON.org

If I get right, .toJSON overrides the .stringify. So if the first one calls the second one a loop is generated.

Questions

  1. (general) Why this design choice? toJSON is a kind of reserved of special keyword?
  2. (specific) I solved the stackoverflow bug changing the .toJSON name into .display. Not so elegant. Is there another solution?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:44:35+00:00Added an answer on June 13, 2026 at 5:44 am

    Question 1, is toJSON reserved?

    I’m not sure if it reserved, but for example the native Date object uses toJSON to create a stringified date representation:

    (new Date()).toJSON();           // -> "2012-10-20T01:58:21.427Z"
    JSON.stringify({d: new Date()}); // -> {"d":"2012-10-20T01:58:21.427Z"}"
    

    Question 2, an easy solution:

    create your custom stringify function that ignores toJSON methods (you may add it to the already existing global JSON):

    JSON.customStringify = function (obj) {
    
        var fn = obj.toJSON;
        obj.toJSON = undefined;
        var json = JSON.stringify(obj);
        obj.toJSON = fn;
        return json;
    }
    

    now it’s very easy to use in all your objects:

    function MyObject(id, value, desc)
    {
      this.id = id;
      this.value = value;
      this.desc = desc;
      this.toJSON = function()
      {
        return JSON.customStringify(this);
      }
    }
    

    To make it even more easy additionally add:

    JSON.customStringifyMethod = function () {
    
        return JSON.customStringify(this);
    }
    

    Now your objects might look like:

    function MyObject(id, value, desc)
    {
      this.id = id;
      this.value = value;
      this.desc = desc;
      this.toJSON = JSON.customStringifyMethod;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading this answer on "one file per component" approach when using WiX, I
This could potentially be a duplicate but I after spending several hours reading about
After reading this question I started to wonder: is it possible to have a
after reading the data from the request as Stream i needed to convert it
This question is related to c#.The scenario is that when i click the button
After reading this question and through the various Phone Book sorting scenarios put forth
Apparently after reading the old title that was Why do questions like is ++i
after reading the blue book (Eric Evan's Domain Driven Design) and starting applying the
Scenario A.java-----------after erasure-------->M.class Scenario B.java-----------after erasure-------->M.class Then why A is illegal and B is
Scenario: User A send application request to user B User B accepts request after

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.