question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deserializing id back to _id

See original GitHub issue

The serializer has the option to specify an alternate field as the id for a resource. This comes in handy when working with databases (notably MongoDB):

new Serializer("users", { id: "_id" }).serialize(users);

I wonder why the same facility isn’t available for deserialization so that after transport, the id can be properly mapped back to the appropriate field in the model:

new Deserializer({ id: "_id" }).deserialize(users);

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
SeyZcommented, Sep 9, 2017

I’ve added the support of the id option 😉 FYI, here’s the test:

  describe('id', function () {
    it('should override the id field', function (done) {
      var dataSet = {
        data: [{
          type: 'users',
          id: '54735750e16638ba1eee59cb',
          attributes: { 'first-name': 'Sandro', 'last-name': 'Munda' }
        }, {
          type: 'users',
          id: '5490143e69e49d0c8f9fc6bc',
          attributes: { 'first-name': 'Lawrence', 'last-name': 'Bennett' }
        }]
      };

      new JSONAPIDeserializer({
        id: '_id'
      }).deserialize(dataSet, function (err, json) {
        expect(json[0]).to.not.have.keys('id');
        expect(json[1]).to.not.have.keys('id');
        expect(json[0]._id).equal('54735750e16638ba1eee59cb');
        expect(json[1]._id).equal('5490143e69e49d0c8f9fc6bc');
        done(null, json);
      });

    });
  });
2reactions
prashaanttcommented, Nov 4, 2016

@jcw- FWIW, I worked around this by manually assigning _id to a new id attribute after deserialization, something like:

user.id = user._id;
delete user._id;

This is trivial to do but should ideally be built-in.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON Deserialization from ID into Object - Stack Overflow
As the answer here (Jackson deserialize JsonIdentityReference (alwaysAsId = true)) states, using a setter deserializer works well and does ...
Read more >
Id is different after JSON deserialization
When you use a variable of type Id , salesforce is using the 18 character one (even if you ... the query would...
Read more >
CWE-502: Deserialization of Untrusted Data (4.9) - MITRE
Serialization and deserialization refer to the process of taking program-internal object-related data, packaging it in a way that allows the data to be ......
Read more >
Serialization in Java - DigitalOcean
When we run above class, we get following output. SubClass read = SubClass{id=10,value=Data,name=Pankaj}. So in this way, we can serialize ...
Read more >
Serializing and Deserializing a List with Gson - Baeldung
One common use case is to serialize and deserialize a list of POJOs. Consider the class: public class MyClass { private int id; ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found