Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom mapping functions #28

Open
jongunter opened this issue Jun 21, 2017 · 3 comments
Open

Custom mapping functions #28

jongunter opened this issue Jun 21, 2017 · 3 comments

Comments

@jongunter
Copy link

jongunter commented Jun 21, 2017

Hi there...great library! I usually use automapper-ts, but this is more lightweight and easier to configure.

It would be cool if the @JsonProperty decorator could accept custom mapping functions.

For example:

const deserializeDate = dateString => moment(dateString).toDate();  // pretend this is valid momentjs syntax
const convertToStatusEnum = statusString => return enum[statusString] // fetches the appropriate enumerator

class Homework {
   @JsonProperty({name:  'date', mappingFn: deserializeDate })
   public dueDate: Date = null;

   @JsonProperty({mappingFn: convertToStatusEnum})
   public status: Statuses = Statuses.NotDone;
}
@Sir-J
Copy link

Sir-J commented Aug 24, 2017

customConverter should help You

@hbenlulu
Copy link

hbenlulu commented Dec 3, 2017

Hey

Do you have an example of how to use customConverter ?

Thanks,
Hanan

@Sir-J
Copy link

Sir-J commented Dec 4, 2017

export const dateConverter: ICustomConverter = {
     fromJson(data: any): any {
         return new Date(data);
     },

     toJson(data: Date): any {
         return data.toJSON();
     }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants