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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:10:11+00:00 2026-05-16T20:10:11+00:00

I need to serialize and deserialize JavaScript objects to store them in a DB.

  • 0

I need to serialize and deserialize JavaScript objects to store them in a DB.

Note that these objects contain functions, so I can’t store them as JSON, so I can’t use json2.js.

What’s the state of the art in [de]serialization of JavaScript objects (in JavaScript of course).

  • 1 1 Answer
  • 1 View
  • 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-05-16T20:10:11+00:00Added an answer on May 16, 2026 at 8:10 pm

    In general, there’s no way (in a browser) to serialize objects with functions attached to them: Every function has a reference to its outer scope, that scope won’t exist when you deserialize it, so serialized references to that scope will be invalid.

    What I would do is use the built-in (or json2.js) JSON.stringify and JSON.parse functions with the replacer and reviver parameters. Here’s a partial example of how it would work:

    JSON.stringify(yourObject, function(name, value) {
        if (value instanceof LatLng) { // Could also check the name if you want
            return 'LatLng(' + value.lat() + ',' + value.lng() + ')';
        }
        else if (...) {
            // Some other type that needs custom serialization
        }
        else {
            return value;
        }
    });
    
    JSON.parse(jsonString, function(name, value) {
        if (/^LatLng\(/.test(value)) { // Checking the name would be safer
            var match = /LatLng\(([^,]+),([^,]+)\)/.exec(value);
            return new LatLng(match[1], match[2]);
        }
        else if (...) {
            ...
        }
        else {
            return value;
        }
    });
    

    You can use any serialization format you want in your custom types. The "LatLng(latitude,longitude)" format is just one way of doing it. You could even return a JavaScript object that can be serialized to JSON natively.

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

Sidebar

Related Questions

I need to serialize and deserialize objective-c objects into JSON to store in CouchDB.
I need to serialize and deserialize an object that contains an System.Uri property using
I need to deserialize some JavaScript object represented in JSON to an appropriate C#
I use a XmlSerializer to serialize/deserialize some objects. The problem is the performance. When
Problem I need a key-value store that can store values of the following form:
I have a small hierarchy of objects that I need to serialize and transmit
i need to serialize AS3 strong typed Objects, send them to a C# Application
I am working on some classes which I need to serialize/deserialize to xml, that
I'm looking at converting a class that we currently serialize/deserialize with XMLSerializer to use
I have a simple Java class that I need to serialize to be stored

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.