Home > OS >  Serialize nested typescript classes to json
Serialize nested typescript classes to json

Time:11-23

I can not serialize a nested set of classes to json:

import { readFileSync  } from 'fs'

class Address {
    constructor(
        public readonly street: string,
        public readonly city: string){}
}

class Person {
    constructor(
        public readonly address: Address,
        public readonly name: string){}
}

class School {
    private students = new Map<string, Person>();
    public add(student: Person): School {
        this.students.set(student.name, student);
        return this;
    }
    public toJson(filename: string) {        
    }
}

const p1 = new Person(new Address("5th Ave.", "NYC"), "moish");
const p2 = new Person(new Address("remi", "Boston"), "dave");
const p3 = new Person(new Address("Dart", "Boston"), "uzi");

let school = new School();
school.add(p1).add(p2).add(p3)
console.log(school); // <--- good ! but wrong format            
  • Related