-
Notifications
You must be signed in to change notification settings - Fork 196
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
Comments
Hi @melkior ! Can you paste me an example of your dataSet with the |
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:
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. |
I think you are looking for the 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);
}
}); |
Yeah, that's it. Ok, with one little difference, I just need:
I'll close issue, thank you very much. |
👍 |
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
The text was updated successfully, but these errors were encountered: