Search code examples
ember.jsember-data

what's the difference between deserialization and normalization?


Is to "deserialize" and "normalize" the same thing or are there differences? A straightforward question. I believe this is generic to front-end models communication to backend database sources/APIs. But if not, this is in the context of Ember Data.


Solution

  • There are differences. When you deseralize your intention is to change from one form say a javascript object and turning it into another form say a ember-data model. When you normalize your intention is to manipulate the data and/or structure of the current form.

    So with ember-data you have RESTSerializer with:

    • serialize which takes a model and turns it into a plain javascript object.
    • normalize which takes a plain javascript object and returns a plain javascript object.

    Compared to transforms/date.js:

    • deserialize Given a number or string returns a Date object.
    • serialize Returns a string representation of a Date object.

    To put both of these things in context, from the docs on the applyTransforms method on JSONSerializer (this is where the transforms work on the plain javascript object):

    Given a subclass of DS.Model and a JSON object this method will iterate through each attribute of the DS.Model and invoke the DS.Transform#deserialize method on the matching property of the JSON object. This method is typically called after the serializer's normalize method.