1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-05 21:18:13 -05:00

DRY up code

This commit is contained in:
jaasen-livefront 2025-04-04 14:41:16 -07:00
parent 57f41ee7bc
commit 2ea0377dfc
No known key found for this signature in database

View File

@ -1014,47 +1014,25 @@ public class CipherService : ICipherService
private string SerializeCipherData(CipherData data) private string SerializeCipherData(CipherData data)
{ {
if (data is CipherLoginData cipherLoginData) return data switch
{ {
return JsonSerializer.Serialize<CipherLoginData>(cipherLoginData); CipherLoginData loginData => JsonSerializer.Serialize(loginData),
} CipherIdentityData identityData => JsonSerializer.Serialize(identityData),
if (data is CipherIdentityData cipherIdentityData) CipherCardData cardData => JsonSerializer.Serialize(cardData),
{ CipherSecureNoteData noteData => JsonSerializer.Serialize(noteData),
return JsonSerializer.Serialize<CipherIdentityData>(cipherIdentityData); _ => throw new ArgumentException("Unsupported cipher data type.", nameof(data))
} };
if (data is CipherCardData cipherCardData)
{
return JsonSerializer.Serialize<CipherCardData>(cipherCardData);
}
if (data is CipherSecureNoteData cipherSecureNoteData)
{
return JsonSerializer.Serialize<CipherSecureNoteData>(cipherSecureNoteData);
}
return null;
} }
private CipherData DeserializeCipherData(Cipher cipher) private CipherData DeserializeCipherData(Cipher cipher)
{ {
if (cipher.Type == CipherType.Login) return cipher.Type switch
{ {
return JsonSerializer.Deserialize<CipherLoginData>(cipher.Data); CipherType.Login => JsonSerializer.Deserialize<CipherLoginData>(cipher.Data),
} CipherType.Identity => JsonSerializer.Deserialize<CipherIdentityData>(cipher.Data),
CipherType.Card => JsonSerializer.Deserialize<CipherCardData>(cipher.Data),
if (cipher.Type == CipherType.Identity) CipherType.SecureNote => JsonSerializer.Deserialize<CipherSecureNoteData>(cipher.Data),
{ _ => throw new ArgumentException("Unsupported cipher type.", nameof(cipher))
return JsonSerializer.Deserialize<CipherIdentityData>(cipher.Data); };
}
if (cipher.Type == CipherType.Card)
{
return JsonSerializer.Deserialize<CipherCardData>(cipher.Data);
}
if (cipher.Type == CipherType.SecureNote)
{
return JsonSerializer.Deserialize<CipherSecureNoteData>(cipher.Data);
}
return null;
} }
} }