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 Objects #48

Open
tkagerer opened this issue Mar 10, 2017 · 2 comments
Open

Custom Objects #48

tkagerer opened this issue Mar 10, 2017 · 2 comments

Comments

@tkagerer
Copy link

I had to change

if (metadata.deserializedType && typeof (<any>metadata.deserializedType).Deserialize === "function") {
            instance[keyName] = (<any>metadata.deserializedType).Deserialize(source);
}

TO

if (metadata.deserializedType && typeof (<any>metadata.deserializedType).Deserialize === "function") {
            instance[keyName] = **new** (<any>metadata.deserializedType).Deserialize(source);
}

in order to maintain object type.

If you skip new here you get an anonymous object.

--- Background

I used @autoserializeAs(MyObject)

but what i got on deserialisation was a anonymous Object without the methods I expected the Object to have (MyObject).

I would appreciate an opinion to this or if i missed something.

Greetings

@weichx
Copy link
Owner

weichx commented Mar 10, 2017

is object a constructable function (ie class type) or just a literal? You shouldn't need to be using new in there for sure. If autoserializeAs is not passed a class type, it will assume you are passing in a custom serializer which is { Deserialize: (instance : YOUR_TYPE, json:any) => any, Serialize: (instance : YOUR_TYPE) => any}

@tkagerer
Copy link
Author

tkagerer commented Mar 15, 2017

Hi @weichx, thank you for your quick answer and sorry for my late answer.

I might have special requirements here.

See http://plnkr.co/edit/5ed8ZK10CFWtHjLTIpTV?p=info for the patched version (app.component.ts)

See http://plnkr.co/edit/QqrY3aPxOimtol7yfvJW?p=info for the original version (app.component.ts)
(I couldn't reproduce the same effect I have in my project here, nevertheless it also doesn't work here)

I couldn't find any negative side effects of my patched version.

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

2 participants