GillianPerard / typescript-json-serializer

A typescript library to deserialize json into typescript classes and serialize classes into json.
MIT License
211 stars 29 forks source link

[FEAT]: not have to had @JsonProperty all over the place? #228

Open deanbiltup opened 5 days ago

deanbiltup commented 5 days ago

Description

perhaps there is already a solution for this?

Coming from java land, jackson simply will do any plain old class. I realize typescript is different and needs decorators and things dissapears. We have 15 apis and 20 methods = 300 request, 300 responses * on average 10 fields lets say -> 6000 locations of needing @JsonProperty. I do not want to add those all. I do not mind adding @JsonObject 600 times probably more as requests and responses use similar objects under the covers. (I guess 6000 maybe a bit high then too). Anyways, I only want to use @JsonProperty when I need to like for Date or enum and special cases. string fields should not need them and if I want to exclude a propery, I would use @JsonExclude or something. our apis do not have a single exclusion(that is pretty rare that this is desired since our api is implemented by controller server-side and on the client side, we have a client implemetning the same exact api. Our client/server apis are 100% using the same code.

Proposed solution

use @JsonObject and assume all fields are included except ones with @JsonExclude much like jackson in java does. is this possible?

deanbiltup commented 5 days ago

I was using serializeObject and deserializeObject on this tyep and the title is lost :( :(

@JsonObject() export class TempDto { public title?: string; @JsonProperty() public availableEnd?: Date; }

deanbiltup commented 5 days ago

I found an option 'allow' that fixed this. I am very surprised it was not the default.

I would have an @JsonProperty(exclude=true) instead like jackson as 90% of time no one wants to type all these @JsonProperty all over the place. closing.

deanbiltup commented 5 days ago

ohhh, odd, I did need it after all

@JsonObject() export class FetchProposalRequest { @JsonProperty() public outlineId?: string; }

so it serializes fine and then on the server side if I do not have @JsonProperty, it fails to load the outlineId into the json :(.

or is this a bug? I was hoping I could miss many and it seems I can miss some but others are critical and must have.

deanbiltup commented 4 days ago

I have a log before and after serializing [1] $$$$$$$$$$$$$$$serizlizing=FetchProposalsResponse [1] $$$$$$$$$$$$$$$$done serializing

but also in the middle I have export const stageEnumToString = (classSize?: StageEnumDto): string | undefined => { console.log(serializeing stage=${classSize}); if(!classSize) return undefined; return classSize.value; };

and @JsonProperty({ beforeDeserialize: stringToStageEnum, afterSerialize: stageEnumToString }) public stage?: StageEnumDto;

finding where things are going wrong is a huge pain. in java, it would fail fast pointing to exact annotation missing or a 'can't serialize this as it is not a POJO or something'. any way to fail fast here?

I am spending way too much time trying to figure out why it is not invoking my afterSerialize: stageEnumToString.

is there some kind of logging in place for debugging?

deanbiltup commented 4 days ago

getting closer. first enum works and second one does not. second one does NOT call afterSerialize on the way out. When server reads it in, it also does not call beforeDeserialize and instead fails on cryptic error

@JsonProperty({
    beforeDeserialize: stringToClassType,
    afterSerialize: classTypeToString
})
public trainingType?: ClassTypeEnumDto;
@JsonProperty({
    beforeDeserialize: stringToStageEnum,
    afterSerialize: stageEnumToString
})
public stage?: StageEnumDto;

I don't have any name properties but it fails with

[1] chokepoint stack TypeError: Cannot read properties of undefined (reading 'name') [1] at d.deserializeProperty (/Users/dean/workspace/biltup/biltup/server/node_modules/typescript-json-serializer/dist/index.cjs.js:1:7391) [1] at /Users/dean/workspace/biltup/biltup/server/node_modules/typescript-json-serializer/dist/index.cjs.js:1:2789 [1] at Array.forEach () [1] at d.deserializeObject (/Users/dean/workspace/biltup/biltup/server/node_modules/typescript-json-serializer/dist/index.cjs.js:1:2753) [1] at NewServerSerializer.deserialize (/Users/dean/workspace/biltup/biltup/server/src/util/httpProcessing.ts:71:44) [1] at translateOrReturnNewImpl (/Users/dean/workspace/biltup/biltup/server/src/util/httpProcessing.ts:143:39) [1] at processTicksAndRejections (node:internal/process/task_queues:95:5) [1] Url: /api/user/updateProposal time=59 Response body: {"message":"Internal Server Error"}