0

Best approach for creating an object from deserializing.

I'm looking for good approach when create object from serialized data. Let's assume that there is an object defined like that:

function A()
{}
A.prototype.a = "";

and serialized data: "a". So which approach will be better and why:

1. Create static method deserialize:

    A.deserialize = function( data )
    {
        var a = new A();
        a.a = data;
        return a;
    }

and will be called like that:

var a = A.deserialize("a");

2. Create method in prototype

A.prototype.deserialize = function ( data )
{
    this.a = data;
}

and it will be called like that

 var a = new A();
 a.deserialize( "a" );

3. Process data in contstructor

function A(data) 
{ 
   this.a = data; 
}

Consider that data can be different types for example - string, json or ArrayBuffer. I'm looking for a more generic solution. Is there any matter how I will create object?

Azzy Elvul
  • 1,403
  • 1
  • 12
  • 22

2 Answers2

0

i wrote this jquery function, makes it very easy to serialize any form data.

$.fn.serializeObject = function () {
    var result = {};
    this.each(function () {
        var this_id = (this.id.substring(this.id.length - 2) == '_s') ? this.id.substring(0, this.id.length - 2) : this.id.replace('_val', '');
        if (this.type == 'radio') {
            var this_id = this.name;
            result[this_id.toLowerCase()] = $('input[name=' + this_id + ']:checked').val();
        }
        else if (this.type == 'checkbox') {         
            result[this_id.toLowerCase()] = $('#' + this_id).prop('checked');
        }
        else {          
            if (this_id.indexOf('___') > -1) {
                this_id = this_id.substring(0, this_id.indexOf('___'));
            }
            result[this_id.toLowerCase()] = this.value;

        }
    });
    return result;
};

you can easily call it by doing var form_vars = $('#div input, #div select, #div textarea').serializeForm()

you can add additional properties to the object by doing form_vars.property = 'value';, you can also even add js arrays and json objects to it. then you can use $.ajax to submit.

Chris Brickhouse
  • 650
  • 5
  • 15
  • sure it is, with my method you can get any value from the object by doing "object_name.property" instead of needing functions to get the data from the object. i believe that's what you were asking for opinions on. – Chris Brickhouse Feb 05 '14 at 14:34
  • Sorry but this is not related with my question. Maybe my English is bad. Assume that I'm not using JQuery and the object is not related with html elements. Assume that the data are received via webSocket with as arraybuffer and aren't related directly with html. I'm familiar with JS, jq and so on. I'm looking not to exact code but for approach. I'm looking for Pros and Cons and problems that can be related with different approaches. – Azzy Elvul Feb 05 '14 at 14:43
0

You can use generic solution for (de)serializing other objects as JSON and make an utility function out of it. See more at How to serialize & deserialize Javascript objects?

If you prefer deserializing each object type by itself.

Static method approach

Pros:

  • Static method handles whole object creation and setting values cleanly, not creating any unnecessary temp objects and such. By far the most cleaner solution.
  • This method doesn't require object to be aware of serialization process, and it can be easily added to existing solution

Prototype method approach

Cons:

  • This variant pollutes prototype chain.
  • You have to create an object for sake of creating an object unless you want to use it to fill it up from inside. That can be problematic if there is for instance logic in constructor, that needs to be executed.

Process data in constructor

Cons:

  • Constructor needs to be overloaded. It can be difficult to recognize, if data passed to constructor are normal or serialized data. e.g. normally it takes some string and serialized data is string as well.
Community
  • 1
  • 1
Michal Brašna
  • 2,293
  • 13
  • 17