Commit b7f300db by Jonathan Derrough

Made .toJSON() implementations instance methods taking a POJO argument

Made .fromJSON() implementations return a POJO Updated the unit tests
parent 2efd6f27
......@@ -18,19 +18,18 @@ export class LabeledFaceDescriptors {
public get label(): string { return this._label }
public get descriptors(): Float32Array[] { return this._descriptors }
public static toJSON(ld: LabeledFaceDescriptors): string {
return `{"label":"${ld.label}","descriptors":[${ld.descriptors.map((d) => `[${Array.from(d)}]`)}]}`;
public toJSON(): any {
return {
label: this.label,
descriptors: this.descriptors.map((d) => Array.from(d))
};
}
public static fromJSON(jsonString: string): LabeledFaceDescriptors {
return LabeledFaceDescriptors.fromPOJO(JSON.parse(jsonString));
}
public static fromPOJO(pojo: any): LabeledFaceDescriptors {
const descriptors = pojo.descriptors.map((d: any) => {
public static fromJSON(json: any): LabeledFaceDescriptors {
const descriptors = json.descriptors.map((d: any) => {
return new Float32Array(d);
});
return new LabeledFaceDescriptors(pojo.label, descriptors);
return new LabeledFaceDescriptors(json.label, descriptors);
}
}
\ No newline at end of file
......@@ -67,18 +67,17 @@ export class FaceMatcher {
: new FaceMatch('unknown', bestMatch.distance)
}
public static toJSON(matcher: FaceMatcher): string {
return `{"distanceThreshold":${matcher.distanceThreshold},"labeledDescriptors":[${matcher.labeledDescriptors.map((ld) => LabeledFaceDescriptors.toJSON(ld))}]}`;
public toJSON(): any {
return {
distanceThreshold: this.distanceThreshold,
labeledDescriptors: this.labeledDescriptors.map((ld) => ld.toJSON())
};
}
public static fromJSON(jsonString: string): FaceMatcher {
return FaceMatcher.fromPOJO(JSON.parse(jsonString));
}
public static fromPOJO(pojo: any): FaceMatcher {
const labeledDescriptors = pojo.labeledDescriptors
.map((ld: any) => LabeledFaceDescriptors.fromPOJO(ld));
return new FaceMatcher(labeledDescriptors, pojo.distanceThreshold);
public static fromJSON(json: any): FaceMatcher {
const labeledDescriptors = json.labeledDescriptors
.map((ld: any) => LabeledFaceDescriptors.fromJSON(ld));
return new FaceMatcher(labeledDescriptors, json.distanceThreshold);
}
}
\ No newline at end of file
......@@ -9,21 +9,14 @@ describe('globalApi', () => {
const f1 = new Float32Array([1, 2, 3]);
const f2 = new Float32Array([4, 5, 6]);
it('toJSON()', () => {
expect(LabeledFaceDescriptors.toJSON(new LabeledFaceDescriptors(l1, [f1,f2]))).toBe(json);
it('JSON.stringify()', () => {
expect(JSON.stringify(new LabeledFaceDescriptors(l1, [f1,f2]))).toBe(json);
expect(JSON.stringify({ ld: new LabeledFaceDescriptors(l1, [f1,f2]) })).toBe(`{"ld":${json}}`);
expect(JSON.stringify([ new LabeledFaceDescriptors(l1, [f1,f2]) ])).toBe(`[${json}]`);
});
it('fromJSON()', () => {
const ld = LabeledFaceDescriptors.fromJSON(json);
expect(ld.label).toBe(l1);
expect(ld.descriptors.length).toBe(2);
expect(ld.descriptors[0]).toEqual(f1);
expect(ld.descriptors[1]).toEqual(f2);
});
it('fromPOJO()', () => {
const ld = LabeledFaceDescriptors.fromPOJO(JSON.parse(json));
const ld = LabeledFaceDescriptors.fromJSON(JSON.parse(json));
expect(ld.label).toBe(l1);
expect(ld.descriptors.length).toBe(2);
......@@ -32,7 +25,7 @@ describe('globalApi', () => {
});
it('toJSON() => fromJSON()', () => {
const ld = LabeledFaceDescriptors.fromJSON(LabeledFaceDescriptors.toJSON(new LabeledFaceDescriptors(l1, [f1,f2])));
const ld = LabeledFaceDescriptors.fromJSON(new LabeledFaceDescriptors(l1, [f1,f2]).toJSON());
expect(ld.label).toBe(l1);
expect(ld.descriptors.length).toBe(2);
......
......@@ -18,12 +18,14 @@ describe('globalApi', () => {
new LabeledFaceDescriptors(l2, [f3, f4])
];
it('toJSON()', () => {
expect(FaceMatcher.toJSON(new FaceMatcher(lds, dt))).toBe(json);
it('JSON.stringify()', () => {
expect(JSON.stringify(new FaceMatcher(lds, dt))).toBe(json);
expect(JSON.stringify({ m: new FaceMatcher(lds, dt) })).toBe(`{"m":${json}}`);
expect(JSON.stringify([ new FaceMatcher(lds, dt) ])).toBe(`[${json}]`);
});
it('fromJSON()', () => {
const fm = FaceMatcher.fromJSON(json);
const fm = FaceMatcher.fromJSON(JSON.parse(json));
expect(fm.distanceThreshold).toBe(dt);
expect(fm.labeledDescriptors.length).toBe(2);
......@@ -37,23 +39,8 @@ describe('globalApi', () => {
expect(fm.labeledDescriptors[1].descriptors[1]).toEqual(f4);
});
it('fromPOJO()', () => {
const fm = FaceMatcher.fromPOJO(JSON.parse(json));
expect(fm.distanceThreshold).toBe(dt);
expect(fm.labeledDescriptors.length).toBe(2);
expect(fm.labeledDescriptors[0].label).toBe(l1);
expect(fm.labeledDescriptors[0].descriptors.length).toBe(2);
expect(fm.labeledDescriptors[0].descriptors[0]).toEqual(f1);
expect(fm.labeledDescriptors[0].descriptors[1]).toEqual(f2);
expect(fm.labeledDescriptors[1].label).toBe(l2);
expect(fm.labeledDescriptors[1].descriptors.length).toBe(2);
expect(fm.labeledDescriptors[1].descriptors[0]).toEqual(f3);
expect(fm.labeledDescriptors[1].descriptors[1]).toEqual(f4);
});
it('JSON.stringify() => fromJSON()', () => {
const fm = FaceMatcher.fromJSON(FaceMatcher.toJSON(new FaceMatcher(lds, dt)));
it('toJSON() => fromJSON()', () => {
const fm = FaceMatcher.fromJSON(new FaceMatcher(lds, dt).toJSON());
expect(fm.distanceThreshold).toBe(dt);
expect(fm.labeledDescriptors.length).toBe(2);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment