cerialize icon indicating copy to clipboard operation
cerialize copied to clipboard

Custom Objects

Open tkagerer opened this issue 8 years ago • 2 comments

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

tkagerer avatar Mar 10 '17 15:03 tkagerer

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}

weichx avatar Mar 10 '17 18:03 weichx

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.

tkagerer avatar Mar 15 '17 12:03 tkagerer