Giter Site home page Giter Site logo

Comments (5)

SeyZ avatar SeyZ commented on May 19, 2024

Hi @melkior !

Can you paste me an example of your dataSet with the _ ? Thanks!

from jsonapi-serializer.

melkior avatar melkior commented on May 19, 2024

Try this:

var data = [{
    id: 1,
    first_name: 'Sandro',
    last_name: 'Munda',
  },{
    id: 2,
    first_name: 'John',
    last_name: 'Doe',
  }];

var JSONAPISerializer = require('jsonapi-serializer');

var users =new JSONAPISerializer('user', data, {
  topLevelLinks: { self: 'http://localhost:3000/api/users' },
  dataLinks: {
    self: function (user) {
      return 'http://localhost:3000/api/users/' + user.id
    }
  },
  attributes: ['first_name', 'last_name']
});

console.log(JSON.stringify(users));

Output:

{
    "links": {
        "self": "http://localhost:3000/api/users"
    },
    "data": [{
        "type": "users",
        "id": "1",
        "attributes": {
            "first-name": "Sandro",
            "last-name": "Munda"
        },
        "links": {
            "self": "http://localhost:3000/api/users/1"
        }
    }, {
        "type": "users",
        "id": "2",
        "attributes": {
            "first-name": "John",
            "last-name": "Doe"
        },
        "links": {
            "self": "http://localhost:3000/api/users/2"
        }
    }]
}

It will convert first_name/last_name into first-name/last-name.

This conversion is part of dasherize() function, so it would be nice to make it configurable the same way as pluralizeType, and also patch would be just additional check in that function. Anyway, it would be nice to have since specification says nothing about this kind of conversions.

If you are interested I can try to make a change?

Thank you.

from jsonapi-serializer.

SeyZ avatar SeyZ commented on May 19, 2024

I think you are looking for the keyForAttribute option.
Example:

var inflection = require('inflection');

var dataSet = {
    id: '1',
    'first_name': 'Sandro',
    'last_name': 'Munda'
};

var json = new JsonApiSerializer('user', dataSet, {
    attributes: ['first_name', 'last_name'],
    keyForAttribute: function(attribute) {
        return inflection.underscore(attribute);
    }
});

from jsonapi-serializer.

melkior avatar melkior commented on May 19, 2024

Yeah, that's it.

Ok, with one little difference, I just need:

keyForAttribute: function(attribute) {
    return attribute;
}

I'll close issue, thank you very much.

from jsonapi-serializer.

SeyZ avatar SeyZ commented on May 19, 2024

👍

from jsonapi-serializer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.