using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using MQTTnet.Server; using Newtonsoft.Json; namespace MQTTnet.TestApp.UniversalWindows { public class JsonServerStorage : IMqttServerStorage { private readonly string _filename = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Retained.json"); public async Task SaveRetainedMessagesAsync(IList messages) { await Task.CompletedTask; var json = JsonConvert.SerializeObject(messages); File.WriteAllText(_filename, json); } public async Task> LoadRetainedMessagesAsync() { await Task.CompletedTask; if (!File.Exists(_filename)) { return new List(); } try { var json = File.ReadAllText(_filename); return JsonConvert.DeserializeObject>(json); } catch { return new List(); } } public void Clear() { if (File.Exists(_filename)) { File.Delete(_filename); } } } }