Comments (5)
Hi @melkior !
Can you paste me an example of your dataSet with the _
? Thanks!
from jsonapi-serializer.
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.
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.
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.
👍
from jsonapi-serializer.
Related Issues (20)
- feat: On deserialization it would be nice if the meta, links, etc, are available as a Symbol in the data
- How can I build deep relationships with my serializer HOT 1
- Automatically calculate attribute list
- 3.6.6 breaks deep relations HOT 4
- What is the difference between `ref` and `id`? HOT 2
- Strange issue with nested relationship deserializing HOT 3
- Support has died. See description. HOT 2
- deserialize does not deserializer links inside relationships objects HOT 3
- Express inside examples is set to version 0.0.0.0 which detects it as vulnerable by scanning tools like Twistlock
- falsy JavaScript attribute values are getting set to null if nullIfMissing flag is set
- How to deserialize with different type?
- Can't Make Relationship Work
- Ability to prefix 'meta' and 'links' key on deserialization HOT 1
- Deserializer [relationships] without [included] field wont work HOT 1
- would not be nice to have a generic valueForRelationship? HOT 1
- How to show all atributte relationships?
- Slow deserialization for specific input HOT 1
- Is it possible to customize the value of the type key? HOT 1
- meta data for Resource Identifier Objects
- Express name throws vulnerability error
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonapi-serializer.