Ashu
Ashu

Reputation: 41

How to deserialize the data using JSONAPI-Serializer module of NodeJs

I am working with nodejs and using module named "JSONAPI-Serializer" for serialization and deserialization purpose.

I am able to serialize the objects without any issue but not able to deserialize the same serialized data.

So,using below code I am able to serialize the data:

root@test$: node test.js

var data = [{ id: 1},{ id: 2 }];
var JSONAPISerializer = require('jsonapi-serializer').Serializer;

var UserSerializer = new JSONAPISerializer('id', {"test":"me"});
var users = UserSerializer.serialize(data);
console.log("Serialized:\n", UserSerializer.serialize(data));

Below is the generated result for the same:-

Serialized:
 { data: [ { type: 'ids', id: '1' }, { type: 'ids', id: '2' } ] }

But on deserialization, I am not able to make it work trying to perform the same example as documented in document of "JSONAPI-Serializer".

Can anyone tell me the way to deserialize the above data using "JSONAPI-Serilizer" ?

Upvotes: 2

Views: 2021

Answers (1)

wookieb
wookieb

Reputation: 4489

You're missing your deserialization code. Here is mine that works. The issue what missing "attributes" option for serializer.

var data = [{ id: 1},{ id: 2 }];
var JSONAPISerializer = require('jsonapi-serializer').Serializer;
var JSONAPIDeserializer = require('jsonapi-serializer').Deserializer;

var UserSerializer = new JSONAPISerializer('id', {attributes: ['id']});
var users = UserSerializer.serialize(data);

console.log(users)
var UserDeserialize = new JSONAPIDeserializer();
UserDeserialize.deserialize(users)
.then(a => console.log(a))

Upvotes: 1

Related Questions