Skip to content

Commit b7f300d

Browse files
committed
Made .toJSON() implementations instance methods taking a POJO argument
Made .fromJSON() implementations return a POJO Updated the unit tests
1 parent 2efd6f2 commit b7f300d

File tree

4 files changed

+30
-52
lines changed

4 files changed

+30
-52
lines changed

src/classes/LabeledFaceDescriptors.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,18 @@ export class LabeledFaceDescriptors {
1818
public get label(): string { return this._label }
1919
public get descriptors(): Float32Array[] { return this._descriptors }
2020

21-
public static toJSON(ld: LabeledFaceDescriptors): string {
22-
return `{"label":"${ld.label}","descriptors":[${ld.descriptors.map((d) => `[${Array.from(d)}]`)}]}`;
21+
public toJSON(): any {
22+
return {
23+
label: this.label,
24+
descriptors: this.descriptors.map((d) => Array.from(d))
25+
};
2326
}
2427

25-
public static fromJSON(jsonString: string): LabeledFaceDescriptors {
26-
return LabeledFaceDescriptors.fromPOJO(JSON.parse(jsonString));
27-
}
28-
29-
public static fromPOJO(pojo: any): LabeledFaceDescriptors {
30-
const descriptors = pojo.descriptors.map((d: any) => {
28+
public static fromJSON(json: any): LabeledFaceDescriptors {
29+
const descriptors = json.descriptors.map((d: any) => {
3130
return new Float32Array(d);
3231
});
33-
return new LabeledFaceDescriptors(pojo.label, descriptors);
32+
return new LabeledFaceDescriptors(json.label, descriptors);
3433
}
3534

3635
}

src/globalApi/FaceMatcher.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,17 @@ export class FaceMatcher {
6767
: new FaceMatch('unknown', bestMatch.distance)
6868
}
6969

70-
public static toJSON(matcher: FaceMatcher): string {
71-
return `{"distanceThreshold":${matcher.distanceThreshold},"labeledDescriptors":[${matcher.labeledDescriptors.map((ld) => LabeledFaceDescriptors.toJSON(ld))}]}`;
70+
public toJSON(): any {
71+
return {
72+
distanceThreshold: this.distanceThreshold,
73+
labeledDescriptors: this.labeledDescriptors.map((ld) => ld.toJSON())
74+
};
7275
}
7376

74-
public static fromJSON(jsonString: string): FaceMatcher {
75-
return FaceMatcher.fromPOJO(JSON.parse(jsonString));
76-
}
77-
78-
public static fromPOJO(pojo: any): FaceMatcher {
79-
const labeledDescriptors = pojo.labeledDescriptors
80-
.map((ld: any) => LabeledFaceDescriptors.fromPOJO(ld));
81-
return new FaceMatcher(labeledDescriptors, pojo.distanceThreshold);
77+
public static fromJSON(json: any): FaceMatcher {
78+
const labeledDescriptors = json.labeledDescriptors
79+
.map((ld: any) => LabeledFaceDescriptors.fromJSON(ld));
80+
return new FaceMatcher(labeledDescriptors, json.distanceThreshold);
8281
}
8382

8483
}

test/tests/classes/LabeledFaceDescriptors.test.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,14 @@ describe('globalApi', () => {
99
const f1 = new Float32Array([1, 2, 3]);
1010
const f2 = new Float32Array([4, 5, 6]);
1111

12-
it('toJSON()', () => {
13-
expect(LabeledFaceDescriptors.toJSON(new LabeledFaceDescriptors(l1, [f1,f2]))).toBe(json);
12+
it('JSON.stringify()', () => {
13+
expect(JSON.stringify(new LabeledFaceDescriptors(l1, [f1,f2]))).toBe(json);
14+
expect(JSON.stringify({ ld: new LabeledFaceDescriptors(l1, [f1,f2]) })).toBe(`{"ld":${json}}`);
15+
expect(JSON.stringify([ new LabeledFaceDescriptors(l1, [f1,f2]) ])).toBe(`[${json}]`);
1416
});
1517

1618
it('fromJSON()', () => {
17-
const ld = LabeledFaceDescriptors.fromJSON(json);
18-
19-
expect(ld.label).toBe(l1);
20-
expect(ld.descriptors.length).toBe(2);
21-
expect(ld.descriptors[0]).toEqual(f1);
22-
expect(ld.descriptors[1]).toEqual(f2);
23-
});
24-
25-
it('fromPOJO()', () => {
26-
const ld = LabeledFaceDescriptors.fromPOJO(JSON.parse(json));
19+
const ld = LabeledFaceDescriptors.fromJSON(JSON.parse(json));
2720

2821
expect(ld.label).toBe(l1);
2922
expect(ld.descriptors.length).toBe(2);
@@ -32,7 +25,7 @@ describe('globalApi', () => {
3225
});
3326

3427
it('toJSON() => fromJSON()', () => {
35-
const ld = LabeledFaceDescriptors.fromJSON(LabeledFaceDescriptors.toJSON(new LabeledFaceDescriptors(l1, [f1,f2])));
28+
const ld = LabeledFaceDescriptors.fromJSON(new LabeledFaceDescriptors(l1, [f1,f2]).toJSON());
3629

3730
expect(ld.label).toBe(l1);
3831
expect(ld.descriptors.length).toBe(2);

test/tests/globalApi/FaceMatcher.test.ts

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ describe('globalApi', () => {
1818
new LabeledFaceDescriptors(l2, [f3, f4])
1919
];
2020

21-
it('toJSON()', () => {
22-
expect(FaceMatcher.toJSON(new FaceMatcher(lds, dt))).toBe(json);
21+
it('JSON.stringify()', () => {
22+
expect(JSON.stringify(new FaceMatcher(lds, dt))).toBe(json);
23+
expect(JSON.stringify({ m: new FaceMatcher(lds, dt) })).toBe(`{"m":${json}}`);
24+
expect(JSON.stringify([ new FaceMatcher(lds, dt) ])).toBe(`[${json}]`);
2325
});
2426

2527
it('fromJSON()', () => {
26-
const fm = FaceMatcher.fromJSON(json);
28+
const fm = FaceMatcher.fromJSON(JSON.parse(json));
2729

2830
expect(fm.distanceThreshold).toBe(dt);
2931
expect(fm.labeledDescriptors.length).toBe(2);
@@ -37,23 +39,8 @@ describe('globalApi', () => {
3739
expect(fm.labeledDescriptors[1].descriptors[1]).toEqual(f4);
3840
});
3941

40-
it('fromPOJO()', () => {
41-
const fm = FaceMatcher.fromPOJO(JSON.parse(json));
42-
43-
expect(fm.distanceThreshold).toBe(dt);
44-
expect(fm.labeledDescriptors.length).toBe(2);
45-
expect(fm.labeledDescriptors[0].label).toBe(l1);
46-
expect(fm.labeledDescriptors[0].descriptors.length).toBe(2);
47-
expect(fm.labeledDescriptors[0].descriptors[0]).toEqual(f1);
48-
expect(fm.labeledDescriptors[0].descriptors[1]).toEqual(f2);
49-
expect(fm.labeledDescriptors[1].label).toBe(l2);
50-
expect(fm.labeledDescriptors[1].descriptors.length).toBe(2);
51-
expect(fm.labeledDescriptors[1].descriptors[0]).toEqual(f3);
52-
expect(fm.labeledDescriptors[1].descriptors[1]).toEqual(f4);
53-
});
54-
55-
it('JSON.stringify() => fromJSON()', () => {
56-
const fm = FaceMatcher.fromJSON(FaceMatcher.toJSON(new FaceMatcher(lds, dt)));
42+
it('toJSON() => fromJSON()', () => {
43+
const fm = FaceMatcher.fromJSON(new FaceMatcher(lds, dt).toJSON());
5744

5845
expect(fm.distanceThreshold).toBe(dt);
5946
expect(fm.labeledDescriptors.length).toBe(2);

0 commit comments

Comments
 (0)