what's the difference between deserialization and normalization? - ember.js

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.

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.

Related

How to pass dictionary or list or custom objects from feature file

If a method takes dictionary as parameter, how to pass it. Do we need to construct the dictionary from values present in feature file and pass internally to the method. Is there any direct way we can pass objects?
As far as I know, you can't pass objects in features, but you can send text and tables in the feature file. The text you send can be exploited to send JSON string of your dict/lists and further create objects using that data in step definition.

Prevent normalizing of the model name by overriding 'normalizeModelName' in Ember Data

I would like to prevent normalizing (dasherized by convention) of the model name and instead use the original name. I want to override the function 'normalizeModelName' as the page http://emberjs.com/api/data/#method_normalizeModelName suggests that this should be possible. But i'm not able to do so.
Simply assigning a new function to DS.normalizeModelName is returning an error: Cannot assign to read only property 'normalizeModelName' of object '[object Object]'.
How could I prevent normalizing of the (internal) model name?
small warning: this is related to 2.10.0
i will not guarantee compatibility beyond or below this version. you need to dig through the corresponding files yourself.
since i had a similar problem i dug through the sourcecode of some ember-data stuff and came to the following conclusion:
you need to extend DS.JSONAPIAdapter with pathForType(name) wich essentially takes the name and camelizes as well as pluralizes it before it's being returned as plain text string.
this usually converts model names from foo-bar to fooBars by doing this: https://github.com/emberjs/data/blob/v2.10.0/addon/adapters/json-api.js#L134-L137
now about the opposite:
you need to extend DS.JSONAPISerializer with keyForRelationship(key, typeClass, method)
where key is essentially the model name aquired from relationships in your models. like: fooBar
this usually translates to foo-bar by simply doing return dasherize(key); https://github.com/emberjs/data/blob/v2.10.0/addon/serializers/json-api.js#L453-L455
you might to also dig through some other methods inside the serializer: modelNameFromPayloadKey modelNameFromPayloadType payloadKeyFromModelName payloadTypeFromModelName
just throw in some debugger; lines to see whats going through.

Ember Data: model.get('modelName') is undefined, but model._internalModel works

I'm using Ember Data 2.2.0 and Ember 2.2.1. After retrieving a model from the data store, I'd like to get the model's type name as a string.
According to the API Docs, DS.Model defines a modelName property, which looks like what I want. However, I find that model.modelName, and model.get('modelName')are undefined, after retrieving model from the store with findRecord.
On the other hand, model._internalModel.modelName returns the lowercased, dasherized name of the model, as expected.
What's going on here?
It looks like a slight ambiguity in the documentation. It doesn't help that the modelName example uses DS.Store#modelFor which could be confused with Ember.Route#modelFor.
DS.Store#modelFor returns, according to the documentation, "a model class for a particular key. Used by methods that take a type key (like find, createRecord, etc.)". What I understand by this is that it returns the actual DS.Model class, and not the instance. _internalModel also returns the DS.Model class, hence the same behaviour.
I believe the safer alternative is doing model.constructor.modelName.

DRF - Serialize and Deserialize further explanation

I am starting to work with Django Rest Framework and I am a little confused with Serializers, sometimes its called to serialize and sometimes to deserialize, sometimes is called with the data param, and sometimes it doesn't. When and how is a good use for serializers to serialize and deserialize?
Serialization is the process of preparing your data to be sent ower the network in the case of REST. The result of the serialization is a json/xml in the case of Django REST framework. So you need to serialize your data when you get it, and you deserialize it when you save it to the model with a POST/PUT request.
For further reading: http://www.django-rest-framework.org/api-guide/serializers/
Serialization: Converting complex data types like queryset to native data types like dictionary.
DeSerialization: Converting python native data types to Complex data types.
so both the actions are accomplished with the same serializer. basically when we call a serializer with parameter data (like serializer=Serializer(data=data_dict), it return a complex object. and when we call it with complex object, it gives a dictionary.
So basically there is nothing like deserializer. Serializers perform both serialization and deserialization depending on the parameter we pass and that's how we call it.
we serializer data to send over the network so that it can be easily interpreted and rendered easily in the browser. we deserialize data to get complex object and to probably save it to database.
Thanks! Hope you find this helpful.:)

packing objects as json with django?

I've run into a snag in my views.
Here "filtered_posts" is array of Django objects coming back from the model.
I am having a little trouble figuring out how to get as text data that I can
later pack into json instead of using serializers.serialize...
What results is that the data comes double-escaped (escaped once by serializers.serialize and a second time by json.dumps).
I can't figure out how to return the data from the db in the same way that it would come back if I were using the MySQLdb lib directly, in other words, as strings, instead of references to objects. As it stands if I take out the serializers.serialize, I get a list of these django objects, and it doesn't even list them all (abbreviates them with '...(remaining elements truncated)...'.
I don't think I should, but should I be using the __unicode__() method for this? (and if so, how should I be evoking it?)
JSONtoReturn = json.dumps({
'lowest_id': user_posts[limit - 1].id,
'user_posts': serializers.serialize("json", list(filtered_posts)),
})
The Django Rest Framework looks pretty neat. I've used Tastypie before, too.
I've also done RESTful APIs that don't include a framework. When I do that, I define toJSON methods on my objects, that return dictionaries, and cascade the call to related elements. Then I call json.dumps() on that. It's a lot of work, which is why the frameworks are worth looking at.
What you're looking for is Django Rest Framework. It handles related objects in exactly thew way you're expecting it to (you can include a nested object, like in your example, or simply have it output the PK of the related object for the key).