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 8101969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:07:05+00:00 2026-06-05T23:07:05+00:00

Using JSON.stringify and JSON.parse to serialize and deserialize objects has subtle problems such as

  • 0

Using JSON.stringify and JSON.parse to serialize and deserialize objects has subtle problems such as date objects being serialized as strings and then coming out on the other side as strings rather than date objects. I’ve tried using postMessage instead which has the benefit of producing 1:1 results, but I’m worried about other handlers eaves dropping on the messages. I’ve considered writing my own serializer/deserializer, but would prefer native browser capabilities.

Is there a way to use postMessage like a serializer, without having to worry about eavesdropping?

NOTE: I don’t care about eavesdropping using dev tools. My app is loading plugin like modules and I want to make sure those aren’t able to eavesdrop on messages that are for other modules.

  • 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-05T23:07:06+00:00Added an answer on June 5, 2026 at 11:07 pm

    You can supply a custom JSON parser/stringifier to JSON.stringify and JSON.parse:

    var serialized = JSON.stringify(obj, /*func*/replacer);
    var deserialized = JSON.parse(serialized, /*func*/reviver);
    

    In addition to the custom reviver, you can also define a toJSON method on the object which is going to be serialized. This method receives one argument: the key’s name indexes in arrays, property names in objects, if applicable.

    // Examples of a serializing a function
    var dummyFunction = function() {return 'hey';};
    
    Function.prototype.toJSON = function(key) {
        'use strict';
        return this.toString();
    };
    JSON.stringify(dummyFunction);
    // Method 2:
    var replacer = function(/*string*/key, /*any*/value, /*boolean*/pretty_print) {
        if (typeof value == 'function') return value.toString();
        return value;
    };
    JSON.stringify(dummyFunction, replacer);
    // >>> "function () {\n    return \"hey\";\n}"
    // Result of the previous, stored in a variable
    var result =  '"function () {\\n    return \\"hey\\";\\n}"';
    
    // Reviver example
    // Note: Just an example. Do not use it without modification in production code,
    // Because the pattern can easily be misguided: "function(){}alert('Evil');x={}"
    // Even if you do not mind, at least add a try-catch block inside
    //   `if (func)`, so that a malformed function does not break the reviver
    JSON.parse(result, function(/*string*/key, /*string*/value) {
        var func = /^\s*function\s*\(([^)]*)\)\s\{([\S\s]*)}$/.exec(value);
        if (func) {
            // Note: Function( .. ) is equivalent to new Function( .. )
            var args = func[1].match(/[^,\s]+/g); // <-- Parameters
            // Function body:
            if (args === null) args = [func[2]];
            else args.unshift(func[2]);
            return Function.apply(null, args);
        }
        return value;
    });
    

    Here’s a JSPerf comparison of native parsing vs parsing with a reviver: http://jsperf.com/json-reviver. It shows that a custom reviver which does nothing special is two times slower, and that a reviver with expensive functionality is significantly slower.
    However, when the last method is compared to a weaker method using native JSON and “manual conversion”, then the differences seems to be neglected.
    Note: Only a single value is benchmarked. Make sure to create a custom benchmark for your own (specific) case, because it’s impossible to create one benchmark which represents every possible case.

    I have created a JSPerf test case which checks the impact of .toJSON. To isolate noise, I’ve created a test case based on the Date object. A custom function with several function calls is just two times slower than a native Date.prototype.toJSON.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using JSON.stringify and JSON.parse to store and retrieve objects from localStorage . However,
I am using json.net to parse objects and delivering them to a webservice I
In my web app, I'm serializing objects for storage using JSON.stringify() as described here
I am building an array in javascript, using JSON.stringify() on it, and then setting
I am using JSON.NET in C# to parse a response from the Klout API.
I've got a JSON value that has been converted from a JavaScript object using
I have an javascript object with Date property which is being converted to JSON
I am storing some values in localStorage using stringify and I'm trying to parse
I create a JSON object in my html view using JSON.stringify(object) which outputs the
My JS saves some string data to JSON using stringify(), but observing the outputted

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.