Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic char conversion #25

Closed
melkior opened this issue Aug 16, 2015 · 5 comments
Closed

Automatic char conversion #25

melkior opened this issue Aug 16, 2015 · 5 comments

Comments

@melkior
Copy link

melkior commented Aug 16, 2015

Hi,

I've got one problem with serialization. Now, if attribute (options) contains '_' ie. underscore character, it will be converted to '-' ie. minus character (inside data). Is this correct behavior, because I'd really like to leave attributes untouched? Anyway ,if you look here:

http://jsonapi.org/format/#document-member-names

it looks like underscore is allowed character.

Thank you very much

@SeyZ
Copy link
Owner

SeyZ commented Aug 17, 2015

Hi @melkior !

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

@melkior
Copy link
Author

melkior commented Aug 17, 2015

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.

@SeyZ
Copy link
Owner

SeyZ commented Aug 17, 2015

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);
    }
});

@melkior
Copy link
Author

melkior commented Aug 17, 2015

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.

@melkior melkior closed this as completed Aug 17, 2015
@SeyZ
Copy link
Owner

SeyZ commented Aug 17, 2015

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants