Comments (21)
One problem I would have with this approach, if I understand it correctly, is it becomes less general when you have to specify a schema a priori. I am in the process of using jsonapi-serializer to override the default response of a back-end, and it needs to be completely agnostic to the models defined in any particular app. Essentially, as you have it set up, you need foreknowledge of the types and schemas. Or maybe these could be created dynamically at runtime? Then it's a matter of creating a separate JSONAPISerializer
for each data type, storing it in an object, and referencing it when a response needs to be sent.
But, I guess the question I have to ask next, what does this accomplish? Is it a performance benefit? Is the cost of creating a new serializer/serialization (as currently) high enough to need to be improved?
from jsonapi-serializer.
My problem is basically a semantics thing. You are writing new JSONAPISerializer
, but you aren't creating a serializer. You are creating a single serialized response.
from jsonapi-serializer.
That I agree with. Seems strange to use a constructor function.
from jsonapi-serializer.
I agree with your suggestion. Let me think about it 👍
from jsonapi-serializer.
The alternative can be something like:
var json = new JsonApiSerializer('users', dataSet).perform({
attributes: ['firstName', 'lastName']
});
But honestly, I'm not sure about the benefits.
Thoughts?
from jsonapi-serializer.
Feel free to reopen this issue if you want to continue this discussion.
from jsonapi-serializer.
I found this a bit confusing as well purely from a semantics perspective. To me it would seem more natural to use it like this:
var userSerializer = new JsonApiSerializer('users', {
attributes: ['firstName', 'lastName']
});
var user1Json = userSerializer.serialize(data1);
var user2Json = userSerializer.serialize(data2);
from jsonapi-serializer.
I like the syntax @sarus, I agree
from jsonapi-serializer.
@SeyZ If you reopen this issue I'm happy to work on a PR. The big question is if you want to try to maintain backwards compatibility with the 2.x
line or if this change causes a bump to 3.x
.
Off the top of my head I'm not sure of a good way to make it backwards compatible though.
from jsonapi-serializer.
Actually for backwards compatibility here are my thoughts:
If you pass three arguments to JsonApiSerializer then we assume you are invoking the legacy behavior of returning the JSON immediately.
// Same old behavior
var json = new JsonApiSerializer('users', data, {
attributes: ['firstName', 'lastName']
});
If you only pass two arguments (the collectionName and the options object) then we default to the new behavior which is to treat the Serializer as a reusable serializer rather than a serialization function.
var usersSerializer= new JsonApiSerializer('users', {
attributes: ['firstName', 'lastName']
});
var json = usersSerializer.serialize(data);
The serializer.js
class could look something like this (all current tests passing with this although we'd want to add tests for the new behavior)
'use strict';
var _ = require('lodash');
var SerializerUtils = require('./serializer-utils');
function JsonApiSerializer(collectionName, records, opts){
if(arguments.length === 3){
// legacy behavior
this.collectionName = collectionName;
this.opts = opts;
return this.serialize(records);
}else{
// treat as a reusable serializer
this.collectionName = collectionName;
this.opts = records;
}
}
JsonApiSerializer.prototype.serialize = function(records){
var self = this;
var payload = {};
function getLinks(links) {
return _.mapValues(links, function (value) {
if (_.isFunction(value)) {
return value(records);
} else {
return value;
}
});
}
function collection() {
payload.data = [];
records.forEach(function (record) {
var serializerUtils = new SerializerUtils(self.collectionName, record,
payload, self.opts);
payload.data.push(serializerUtils.perform());
});
return payload;
}
function resource() {
payload.data = new SerializerUtils(self.collectionName, records, payload, self.opts)
.perform(records);
return payload;
}
if (self.opts.topLevelLinks) {
payload.links = getLinks(self.opts.topLevelLinks);
}
if (self.opts.meta) {
payload.meta = self.opts.meta;
}
if (_.isArray(records)) {
return collection(records);
} else {
return resource(records);
}
};
module.exports = JsonApiSerializer;
from jsonapi-serializer.
Awesome idea @sarus ! Then, we will introduce warning messages when users will use the old syntax. 👍
from jsonapi-serializer.
How would you like the warning messages to be delivered? I took a look at util.deprecate
but I don't think that will work in this case since we are not deprecating the function (just changing how it works)
from jsonapi-serializer.
Yeah, why not just console.warn
?
from jsonapi-serializer.
sounds good I can do that. I'll have some time to work on it this week. The code change was minor but I want to update tests and documentation as well.
from jsonapi-serializer.
👍
from jsonapi-serializer.
Any news, @sarus?
from jsonapi-serializer.
Bump. @sarus, are you working on this issue? Otherwise, I will start implementing it by myself -- on this repo maybe.
from jsonapi-serializer.
Big apologies for my tardiness on this. I'll get this pull request done this week. Been very crazy at work and just had a new daughter born =) I let this slip on the priority list since the serializer has been working fine despite my desire to change around how it gets called.
from jsonapi-serializer.
Nice. Well done. My wife forked my second son last month.
I'm looking forward to see your pull request.
from jsonapi-serializer.
Awesome guys and congrats for your forks 😄
from jsonapi-serializer.
@SeyZ Finished updating serializer.js
and updating all the legacy tests to use the new format (legacy tests are still there as well). Do we want collection-serializer
to work in the same way?
I didn't see any tests for collection-serializer
or documentation as well. I assume it works as a standalone object from the 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.