Conversation
|
I skimmed through it quickly, and I see two major points atm:
|
|
|
@JohnWeisz, I've appended this PR with @jsonInheritance(discriminatorProperty({
property: 'type',
types: () => ({
Employee: Employee,
Investor: Investor,
PartTimeEmployee: PartTimeEmployee,
}),
}))
@jsonObject
class Person {
name: string;
}This functionality enables the following flow: Does this approach work for your use case? |
eb5e533 to
41be7f5
Compare
|
I'm currently investigating some issues with circular references and multiple |
Neos3452
left a comment
There was a problem hiding this comment.
Couple of points:
- Can we get the old tests to have the same exceptions, but with just the new decorator? I saw some were changed, and we would like to support the same use cases
- I saw some tests completely removed, why is that?
- We need some docs, and explanation for the circular dependency issue. Would be nice to link to the original blog post
- I think we will need to document an upgrade path too
src/json-inheritance.ts
Outdated
| /** | ||
| * Function to be used to mutate the resulting serialization given the source object. | ||
| */ | ||
| onSerializeType?: (source: any, result: {[k: string]: any}) => void; |
There was a problem hiding this comment.
Instead of mutation can it return a value that should be used as a result? Does not change much, but allows to use pure functions.
src/json-inheritance.ts
Outdated
| if (reverseMapping === undefined) { | ||
| const reverseMapItems: Array<[Function, string]> = Object.keys(types()) | ||
| .map(discriminator => { | ||
| return [types()[discriminator].prototype.constructor, discriminator]; |
There was a problem hiding this comment.
types() is evaluated for each element of the dictionary. You could save it in a variable
There was a problem hiding this comment.
Does not make a difference IMO as I have to call a function regardless after the map is made, it is no longer called. I'll add a commit to show it. The result is less readable with minimal benefit IMO. I could save it in a variable and make it even less readable.
src/json-inheritance.ts
Outdated
| result[property] = getReverseMapping().get(source.constructor); | ||
| }, | ||
| resolveType: data => { | ||
| return types()[data[property]]; |
There was a problem hiding this comment.
This is maybe a nit, but onSerializeType evaluates types once and stores the result, while here it will evaluated every time. It could mean the behaviour is not consistent across both
There was a problem hiding this comment.
Same comment here: Does not make a difference IMO as I have to call a function regardless so no gain. I'll add a commit to show it. The result is less readable with minimal benefit IMO.
b992d10 to
836ec19
Compare
836ec19 to
89e2169
Compare
cfcbe91 to
7dbe9ca
Compare
|
Rebase complete |
Implemented in fork @Decoverto
This PR continues on top of #158 by reworking inheritance.
The PR removes
knownTypes,typeHintEmitter,typeResolver, andnameResolverwith a more comprehensive@jsonObjectInheritancedecorator. The new implementation is also a lot less complex. 330 lines of source code get deleted 🚀.Since this PR is branched from #158, the diff will include changes from that PR. For only the inheritance rework, see: MatthiasKunnen/TypedJSON@issue-139-define-type-lazily...MatthiasKunnen:rework-inheritance.
Todo: