public static Task <T> DeserializerAsync <T>(Java.IO.File file) #endif { return(Task.Run(async() => { // Wait for file to be free while (FileUtils.IsFileLocked(file)) { await Task.Delay(100).ConfigureAwait(false); } Stream fStream = null; StreamReader sReader = null; JsonReader reader = null; var obj = default(T); #if __ANDROID__ var mFile = new AtomicFile(file); try { fStream = mFile.OpenRead(); #else try { fStream = new FileStream(file.Path, FileMode.Open, FileAccess.Read); #endif sReader = new StreamReader(fStream); reader = new JsonTextReader(sReader); var serializer = JsonSerializer.Create(DefaultSettings); obj = serializer.Deserialize <T>(reader); } catch (Exception ex) { Logger.WriteLine(LoggerLevel.Error, ex, "SimpleWeather: JSONParser: error deserializing or with file"); obj = default(T); } finally { reader?.Close(); #if __ANDROID__ mFile?.Dispose(); #endif } return obj; })); }
public static void Serializer(Object obj, Java.IO.File file) { Task.Run(async() => { // Wait for file to be free while (FileUtils.IsFileLocked(file)) { await Task.Delay(100).ConfigureAwait(false); } var mFile = new AtomicFile(file); Stream fStream = null; JsonTextWriter writer = null; try { fStream = mFile.StartWrite(); writer = new JsonTextWriter(new StreamWriter(fStream)) { CloseOutput = false, }; var serializer = JsonSerializer.Create(DefaultSettings); serializer.Serialize(writer, obj); await writer.FlushAsync().ConfigureAwait(false); mFile.FinishWrite(fStream); } catch (Exception ex) { Logger.WriteLine(LoggerLevel.Error, ex, "SimpleWeather: JSONParser: error serializing or with file"); if (mFile != null && fStream != null) { mFile.FailWrite(fStream); } } finally { mFile?.Dispose(); writer?.Close(); } }).ConfigureAwait(false); }