GillianPerard / typescript-json-serializer

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

[FEAT]: convert typescript property to the value in @JsonProperty name during serialization #205

Closed MisterMunchkin closed 1 year ago

MisterMunchkin commented 1 year ago

Description

Hi,

First of all, thanks for this project it's been really useful.

Is there a way where we can have the name value in @JsonProperty replace the property names during serialization?

Example

export class NotionProp {
  @JsonProperty({name: 'phone_number'})
  phoneNumber?: string;
  ...
}
...

const serializer = new JsonSerializer();
const pageRequest = new NotionProp();

pageRequest.phoneNumber = '12346688';
const serialized = serializer.serializeObject(pageRequest);

Proposed Output:

{
 phone_number: '12346688'
}

Current Output:

{
 phoneNumber: '12346688'
}

Proposed solution

No response

mn4367 commented 1 year ago

Unless I'm missing or misunderstanding something, this is already supported? If you search for the string humanId in the README.MD you'll find an example which does exactly what you are proposing.

MisterMunchkin commented 1 year ago

Yea I'm not sure what the problem is now because I tried creating a stackblitz for it and can't replicate the issue I'm having on my repo.

It might be that I'm using the beta NextJs appDir but yea not sure right now.

MisterMunchkin commented 1 year ago

I tried making the stackblitz mirror what I currently have better and was able to replicate it: https://stackblitz.com/edit/nextjs-vaprea?file=classes/test.ts

It looks like it stops working once I add a beforeSerialize that will turn the array into an object with a custom key while also removing an element.

I haven't really locked down what stops it from working exactly, but after I added the beforeSerialize that's when I was able to replicate my issue.

GillianPerard commented 1 year ago

Hi, thank you for using my lib!

I'll check your stackblitz. I don't know how nextjs works but there were some troubles with react projects because of reacts does not support correcly decorators...

GillianPerard commented 1 year ago

Except your test file, the project is in javascript so I tried with pure typescript stackblitz. Could you tell me if it's the behavior you want? https://stackblitz.com/edit/typescript-xqziwr?file=index.ts

MisterMunchkin commented 1 year ago

In your stackblitz console it displays this

{inner_page: {…}}
 inner_page: Object
  new name: Object
   id: "1234"

Which isn't the behavior I want because I expected id to be page_id which is what we set the name to in the InnerPage class

@JsonObject()
export class InnerPage {
  @JsonProperty({ name: 'page_id' })
  id?: string;

  name: string;
}
GillianPerard commented 1 year ago

Your case is a little bit weird 😅.

When you transform the array into a dictionary, it cannot understand that it must serialize so it returns the new object.

MisterMunchkin commented 1 year ago

Ah, yea I'm not a fan of transforming it but yea, Notion API is kind of annoying to work with at the moment 😂.

It's fine, there are some workarounds I can do so this case isn't really that big of an issue.

one was to just have the property names be the same as what is needed on the API or to set up the data in a way that does not need it to transform from array to dictionary.

At least now I know what's actually causing the issue. Thanks!

GillianPerard commented 1 year ago

Yes, the before... and after... functions are used to manipulate the inner data, not to modify the data structure.

In the example you published in Stackblitz, you can just rename the id by page_id manually, but I assume that your code is way more complex than your example ahah.

Just think that as you modify the structure before serialization, the sub-properties will not be passed to the serializer and will be returned without any kind of manipulation...

So I can close the issue?

MisterMunchkin commented 1 year ago

yea that makes sense. You can close this now thanks!