Closed loweoj closed 5 years ago
Hello, @loweoj, sorry for the delay on answering this issue. I really like the idea, but I'm not convinced yet the name should be toJSON
since it already receives the output of the serialization :thinking:, did you think of any alternative name?
Actually, after discussing with some other contributors there are some raised concerns about it:
UserSerializer.serialize(userStructure)
After considering these points, I'm not sure anymore if we should add this feature.
In my opinion, the static method toJSON
seems more a kind of callback that should actually be named as afterSerialize
or something like that, but, we can fall in a lot of problems dealing with this approach since it doesn't matter the feature you're gonna be dealing with the serialization will be always compromised to the behavior declared in afterSerialize
.
The second point is, it will assume that the structure
model should do more than it is proposed to do, for example, to harbor unnecessary business serialization logic.
I agree with @talyssonoc that complex serialization should live in a serializer layer and this feature will just add complexity to solve a problem more related to architectural design.
Yup makes sense. Thanks for the input guys.
This feature would allow you to specify a static toJSON method on the instance that would be called with the serialized JSON. It would allow you to modify the JSON before being output. Adding removing and modifying attributes to the final output.
e.g.
It should also work on nested structures: