<NME> polymorphism-custom-names.spec.ts
<BEF> ﻿/* Not supported?
import {isEqual} from "./object-compare";
import {JsonObject, JsonMember, TypedJSON} from "../typed-json";

@JsonObject
class Person {
    @JsonMember({ name: "first-name" })
    public firstName: string;

    @JsonMember({ name: "last-name" })
    public lastName: string;

    constructor();
        constructor(firstName?: string, lastName?: string) {
            if (firstName !== undefined && lastName !== undefined) {
                this.firstName = firstName;
                this.lastName = lastName;
            }
        }
    }
}

@JsonObject
class Employee extends Person {
    @JsonMember
    public salary: number;

    @JsonMember
    public joined: Date;

    constructor();
        constructor(firstName: string, lastName: string, salary: number, joined: Date);
        constructor(firstName?: string, lastName?: string, salary?: number, joined?: Date) {
            super(firstName, lastName);

            if (salary !== undefined && joined !== undefined) {
                this.salary = salary;
                this.joined = joined;
            }
        }
    }
}

@JsonObject({ name: "part-time-employee" })
class PartTimeEmployee extends Employee {
    @JsonMember({ name: "work-hours" })
    public workHours: number;
}

@JsonObject()
class Investor extends Person {
    @JsonMember({ name: "invest-amount" })
    public investAmount: number;

    constructor();
        constructor(firstName?: string, lastName?: string, investAmount?: number) {
            super(firstName, lastName);

            this.investAmount = investAmount ?? 0;
        }
    }
    }
}

@JsonObject({ name: "company", knownTypes: [PartTimeEmployee, Investor] })
class Company {
    @JsonMember
    public name: string;

    @JsonMember({ elements: Employee })
    public employees: Array<Employee>;

    @JsonMember
    public owner: Person;

    constructor() {
    }

    function test(log: boolean) {
        // Create a Company.
        const company = new Company();
        company.name = 'Json Types';

        switch (Math.floor(Math.random() * 4)) {
            case 0:
                company.owner = new Employee('John', 'White', 240000, new Date(1992, 5, 27));
                break;

            case 1:
                company.owner = new Investor('John', 'White', 1700000);
                break;

            case 2:
                company.owner = new PartTimeEmployee(
                    'John',
                    'White',
                    160000,
                    new Date(1992, 5, 27),
                );
                (company.owner as PartTimeEmployee).workHours = Math.floor(Math.random() * 40);
                break;

            default:
                company.owner = new Person('John', 'White');
                break;
        }

        // Add employees.
        for (let j = 0; j < 20; j++) {
            if (Math.random() < 0.2) {
                const newPartTimeEmployee = new PartTimeEmployee(
                    `firstname_${j}`,
                    `lastname_${j}`,
                    Math.floor(Math.random() * 80000),
                    new Date(Date.now() - Math.floor(Math.random() * 80000)),
                );

                newPartTimeEmployee.workHours = Math.floor(Math.random() * 40);

                company.employees.push(newPartTimeEmployee);
            } else {
                company.employees.push(new Employee(
                    `firstname_${j}`,
                    `lastname_${j}`,
        }
    }

    TypedJSON.config({
        enableTypeHints: true
    });

    var json = TypedJSON.stringify(company);
    var reparsed = TypedJSON.parse(json, Company);

    if (log) {
        const reparsed = TypedJSON.parse(json, Company);

        if (log) {
            console.log('Test: polymorphism with custom names...');
            console.log(company);
            console.log(JSON.parse(json));

    return isEqual(company, reparsed);
}
*/
    });
});

<MSG> Add name option to members

<DFF> @@ -1,13 +1,12 @@
-﻿/* Not supported?
-import {isEqual} from "./object-compare";
-import {JsonObject, JsonMember, TypedJSON} from "../typed-json";
+﻿import { isEqual } from "./object-compare";
+import { jsonObject, jsonMember, jsonArrayMember, TypedJSON } from "../js/typedjson";
 
-@JsonObject
+@jsonObject
 class Person {
-    @JsonMember({ name: "first-name" })
+    @jsonMember({ name: "first-name" })
     public firstName: string;
 
-    @JsonMember({ name: "last-name" })
+    @jsonMember({ name: "last-name" })
     public lastName: string;
 
     constructor();
@@ -20,12 +19,12 @@ class Person {
     }
 }
 
-@JsonObject
+@jsonObject
 class Employee extends Person {
-    @JsonMember
+    @jsonMember
     public salary: number;
 
-    @JsonMember
+    @jsonMember
     public joined: Date;
 
     constructor();
@@ -41,15 +40,15 @@ class Employee extends Person {
     }
 }
 
-@JsonObject({ name: "part-time-employee" })
+@jsonObject({ name: "part-time-employee" })
 class PartTimeEmployee extends Employee {
-    @JsonMember({ name: "work-hours" })
+    @jsonMember({ name: "work-hours" })
     public workHours: number;
 }
 
-@JsonObject()
+@jsonObject()
 class Investor extends Person {
-    @JsonMember({ name: "invest-amount" })
+    @jsonMember({ name: "invest-amount" })
     public investAmount: number;
 
     constructor();
@@ -62,15 +61,15 @@ class Investor extends Person {
     }
 }
 
-@JsonObject({ name: "company", knownTypes: [PartTimeEmployee, Investor] })
+@jsonObject({ name: "company", knownTypes: [PartTimeEmployee, Investor] })
 class Company {
-    @JsonMember
+    @jsonMember
     public name: string;
 
-    @JsonMember({ elements: Employee })
+    @jsonArrayMember(Employee, { name: 'company-employees' })
     public employees: Array<Employee>;
 
-    @JsonMember
+    @jsonMember
     public owner: Person;
 
     constructor() {
@@ -125,11 +124,7 @@ export function test(log: boolean) {
         }
     }
 
-    TypedJSON.config({
-        enableTypeHints: true
-    });
-
-    var json = TypedJSON.stringify(company);
+    var json = TypedJSON.stringify(company, Company);
     var reparsed = TypedJSON.parse(json, Company);
 
     if (log) {
@@ -142,4 +137,9 @@ export function test(log: boolean) {
 
     return isEqual(company, reparsed);
 }
-*/
+
+describe('polymorphic custom names', function() {
+    it('should work', function () {
+        expect(test(false)).toBeTruthy();
+    });
+});

Add name option to members