Avoid nested toJSON calls

This commit is contained in:
Thomas Rittson 2022-06-28 07:50:26 +10:00
parent 483673b03f
commit be7ce7b04b
10 changed files with 29 additions and 41 deletions

View File

@ -56,9 +56,8 @@ export class SymmetricCryptoKey {
}
}
toJSON(): string {
const obj = Utils.copyToNewObject(this, { keyB64: null });
return JSON.stringify(obj);
toJSON() {
return { keyB64: this.keyB64 };
}
static fromJSON(obj: any): SymmetricCryptoKey {

View File

@ -43,10 +43,8 @@ export class AttachmentView implements View {
return 0;
}
toJSON(): string {
const obj = Utils.copyToNewObject(this, propertyMap);
obj.key = this.key == null ? null : JSON.stringify(this.key);
return JSON.stringify(obj);
toJSON() {
return Utils.copyToNewObject(this, propertyMap);
}
static fromJSON(obj: any): AttachmentView {

View File

@ -90,9 +90,8 @@ export class CardView extends ItemView {
return year.length === 2 ? "20" + year : year;
}
toJSON(): string {
const obj = Utils.copyToNewObject(this, propertyMap);
return JSON.stringify(obj);
toJSON() {
return Utils.copyToNewObject(this, propertyMap);
}
static fromJSON(obj: any): CardView {

View File

@ -28,6 +28,13 @@ const propertyMap: any = {
localData: null,
collectionIds: null,
reprompt: null,
revisionDate: null,
deletedDate: null,
attachments: null,
fields: null,
passwordHistory: null,
};
export class CipherView implements View {
@ -150,18 +157,9 @@ export class CipherView implements View {
return this.linkedFieldOptions.get(id)?.i18nKey;
}
toJSON(): string {
toJSON() {
const obj = Utils.copyToNewObject(this, propertyMap);
// Dates
obj.revisionDate = this.revisionDate?.toISOString();
obj.deletedDate = this.deletedDate?.toISOString();
// Nested objects
obj.attachments = JSON.stringify(this.attachments);
obj.fields = JSON.stringify(this.fields);
obj.passwordHistory = JSON.stringify(this.passwordHistory);
switch (this.type) {
case CipherType.Card:
obj.card = JSON.stringify(this.card);
@ -179,7 +177,7 @@ export class CipherView implements View {
break;
}
return JSON.stringify(obj);
return obj;
}
static fromJSON(obj: any): CipherView {

View File

@ -38,9 +38,8 @@ export class FieldView implements View {
return this.value != null ? "••••••••" : null;
}
toJSON(): string {
const obj = Utils.copyToNewObject(this, propertyMap);
return JSON.stringify(obj);
toJSON() {
return Utils.copyToNewObject(this, propertyMap);
}
static fromJSON(obj: any): FieldView {

View File

@ -161,9 +161,8 @@ export class IdentityView extends ItemView {
return addressPart2;
}
toJSON(): string {
const obj = Utils.copyToNewObject(this, propertyMap);
return JSON.stringify(obj);
toJSON() {
return Utils.copyToNewObject(this, propertyMap);
}
static fromJSON(obj: any): IdentityView {

View File

@ -132,9 +132,8 @@ export class LoginUriView implements View {
: this.uri;
}
toJSON(): string {
const obj = Utils.copyToNewObject(this, propertyMap);
return JSON.stringify(obj);
toJSON() {
return Utils.copyToNewObject(this, propertyMap);
}
static fromJSON(obj: any) {

View File

@ -11,6 +11,8 @@ const propertyMap: any = {
password: null,
totp: null,
autofillOnPageLoad: null,
passwordRevisionDate: null,
uris: null,
};
export class LoginView extends ItemView {
@ -68,11 +70,8 @@ export class LoginView extends ItemView {
return this.uris != null && this.uris.length > 0;
}
toJSON(): string {
const obj = Utils.copyToNewObject(this, propertyMap);
obj.passwordRevisionDate = this.passwordRevisionDate?.toISOString();
obj.uris = this.uris == null ? null : JSON.stringify(this.uris);
return JSON.stringify(obj);
toJSON() {
return Utils.copyToNewObject(this, propertyMap);
}
static fromJSON(obj: any): LoginView {

View File

@ -20,9 +20,8 @@ export class PasswordHistoryView implements View {
this.lastUsedDate = ph.lastUsedDate;
}
toJSON(): string {
const obj = Utils.copyToNewObject(this, propertyMap);
return JSON.stringify(obj);
toJSON() {
return Utils.copyToNewObject(this, propertyMap);
}
static fromJSON(obj: any): PasswordHistoryView {

View File

@ -24,9 +24,8 @@ export class SecureNoteView extends ItemView {
return null;
}
toJSON(): string {
const obj = Utils.copyToNewObject(this, propertyMap);
return JSON.stringify(obj);
toJSON() {
return Utils.copyToNewObject(this, propertyMap);
}
static fromJSON(obj: any): SecureNoteView {