From 992cf033f2f09ed9036b7b7df179e8ea84e06b7f Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 8 Apr 2019 20:48:18 -0400 Subject: [PATCH] support more object types by using JSON strings --- src/Core/Services/PreferencesStorageService.cs | 6 ++++-- src/Core/Services/SecureStorageService.cs | 12 ++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Core/Services/PreferencesStorageService.cs b/src/Core/Services/PreferencesStorageService.cs index c1deae0ba..ccaee3bef 100644 --- a/src/Core/Services/PreferencesStorageService.cs +++ b/src/Core/Services/PreferencesStorageService.cs @@ -1,4 +1,5 @@ using Bit.Core.Abstractions; +using Newtonsoft.Json; using System; using System.Threading.Tasks; @@ -49,7 +50,8 @@ namespace Bit.Core.Services } else { - throw new Exception("Unsupported object type for preferences."); + var val = Xamarin.Essentials.Preferences.Get(formattedKey, default(string)); + return Task.FromResult(JsonConvert.DeserializeObject(val)); } } @@ -88,7 +90,7 @@ namespace Bit.Core.Services } else { - throw new Exception("Unsupported object type for preferences."); + Xamarin.Essentials.Preferences.Set(formattedKey, JsonConvert.SerializeObject(obj)); } return Task.FromResult(0); } diff --git a/src/Core/Services/SecureStorageService.cs b/src/Core/Services/SecureStorageService.cs index dcede487e..8a18f7b94 100644 --- a/src/Core/Services/SecureStorageService.cs +++ b/src/Core/Services/SecureStorageService.cs @@ -1,4 +1,5 @@ using Bit.Core.Abstractions; +using Newtonsoft.Json; using System; using System.Threading.Tasks; @@ -10,16 +11,16 @@ namespace Bit.Core.Services public async Task GetAsync(string key) { + var formattedKey = string.Format(_keyFormat, key); + var val = await Xamarin.Essentials.SecureStorage.GetAsync(formattedKey); var objType = typeof(T); if(objType == typeof(string)) { - var formattedKey = string.Format(_keyFormat, key); - var val = await Xamarin.Essentials.SecureStorage.GetAsync(formattedKey); return (T)(object)val; } else { - throw new Exception("Unsupported object type for secure storage."); + return JsonConvert.DeserializeObject(val); } } @@ -30,16 +31,15 @@ namespace Bit.Core.Services await RemoveAsync(key); return; } - + var formattedKey = string.Format(_keyFormat, key); var objType = typeof(T); if(objType == typeof(string)) { - var formattedKey = string.Format(_keyFormat, key); await Xamarin.Essentials.SecureStorage.SetAsync(formattedKey, obj as string); } else { - throw new Exception("Unsupported object type for secure storage."); + await Xamarin.Essentials.SecureStorage.SetAsync(formattedKey, JsonConvert.SerializeObject(obj)); } }