Files
cours-420-301-ah/json-gnome-exemple.ipynb
T
2024-01-26 18:54:03 -05:00

1.6 KiB

In [27]:
class Person {

    constructor(name, age){
        this.name = name;
        this.age = age;
    }
    
    isMajor(){
        return this.age > 18;
    }
    
    toJSON(){
        return {
            type: "Person",
            age: this.age,
            name: this.name,
        }
    }
    
}

function deserializeObject(object){
    if(object.type === "Person"){
        return new Person(object.name, object.age);
    }
    if(object.type === "Gnome"){
        return new Gnome(object.name, object.age);
    }
    if(object.type === "Luttin"){
        return new Luttin(object.name, object.age);
    }
}

function deserializeArray(lst){
    const ret = [];
    for(const e of lst){
        ret.push(deserializeObject(e));
    }
    return ret;
}