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

Is it possible to serialize/deserialize array? #8

Open
nguyenbathanh opened this issue Dec 21, 2018 · 4 comments
Open

Is it possible to serialize/deserialize array? #8

nguyenbathanh opened this issue Dec 21, 2018 · 4 comments
Assignees
Labels
documentation Documentation changes required question
Milestone

Comments

@nguyenbathanh
Copy link

class Topping {
  @Serialize() public type: string;
}

class Book {
  @Serialize() public name: string;
  @Serialize() public topping: Topping[];
}

I tried and see it doesn't work.

@DudaevAR
Copy link

DudaevAR commented Dec 22, 2018

class Topping {
  @Serialize() public type: string;
}

class Book {
  @Serialize() public name: string;
  @Serialize() public topping: Topping[];
}

I tried and see it doesn't work.

class Book {
  @Serialize() public name: string;
  @Serialize.Custom({down: (a: Array<any>) => a, up: b => b})
  public topping: Topping[];
}

You can use this workaround

@teq teq added the question label Dec 24, 2018
@teq
Copy link
Owner

teq commented Dec 24, 2018

@nguyenbathanh @DudaevAR

Automatic serialization of container types (like Map and Array) is not supported because reflection methods (reflect-metadata polyfill) do not allow to extract types of contained items. It is impossible to deserialize (inflate) new instances without knowledge about their types.

If Topping is seriablizable and you want to have recursive serialization/deserialization, then you can use something like this:

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[];
}

@teq teq added the documentation Documentation changes required label Dec 24, 2018
@teq teq self-assigned this Jan 7, 2019
@Schwankenson
Copy link

I tried something like this:

function getObjectArraySerializer (type) {
    return {
        down: (instances: Array<any>) => { 
            if (!instances) { return; }
            return instances.map(instance => deflate(instance)) 
        },
        up: (jsonObjs: Array<any>) => { 
            if (!jsonObjs) { return; }
            return jsonObjs.map(jsonObj => inflate(type, jsonObj)) 
        }
    }

}

function getArraySerializer () {
    return {down: (a: Array<any>) => a, up: b => b};
}

and then

@Serialize.Custom(getObjectArraySerializer(Tag)) tags: Tag[];
@Serialize.Custom(getArraySerializer()) content_ids: string[];

So I don`t have to copy and paste the long code there everytime. What do you think, is this a good solution?

@teq
Copy link
Owner

teq commented Jun 24, 2019

@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.

Repository owner locked as resolved and limited conversation to collaborators Jun 24, 2019
@teq teq added this to the 3.0 milestone Jul 18, 2019
@teq teq modified the milestones: 3.0, 3.1 Nov 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Documentation changes required question
Projects
None yet
Development

No branches or pull requests

4 participants