-
Notifications
You must be signed in to change notification settings - Fork 2
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
Is it possible to serialize/deserialize array? #8
Comments
You can use this workaround |
Automatic serialization of container types (like Map and Array) is not supported because reflection methods ( If class Book {
@Serialize.Custom({
down: (instances: Array<Topping>) => instances.map(instance => deflate(instance)),
up: (jsonObjs: Array<any>) => jsonObjs.map(jsonObj => inflate(Topping, jsonObj))
})
public topping: Topping[];
} |
I tried something like this:
and then
So I don`t have to copy and paste the long code there everytime. What do you think, is this a good solution? |
@Schwankenson I think it is fine. You can take one step further and reduce it to: type Constructor<T> = new (...args: any[]) => T;
function SerializeAnArrayOf<Type>(ctor: Constructor<Type>) {
return Serialize.Custom({
down: (instances: Array<Type>) => {
if (!instances) { return; }
return instances.map(instance => deflate(instance))
},
up: (jsonObjs: Array<any>) => {
if (!jsonObjs) { return; }
return jsonObjs.map(jsonObj => inflate(ctor, jsonObj))
}
})
} And then: @SerializeAnArrayOf(Tag) tags: Tag[]; Eventually I will add some form of helper to construct serializers for built-in containers like Array and Map. Probably in v3.0. |
I tried and see it doesn't work.
The text was updated successfully, but these errors were encountered: