public static async Task ToXml(this IMultiSerializableObject obj, Stream rawStream, FTHSerializableOptions options = null) { if (obj == null) { return; } var StreamOptions = GetSaveStreamOptions(options); using (var ms = new MemoryStream()) { XmlSerializer xsSubmit = new XmlSerializer(obj.GetType()); var xml = ""; using (var sww = new StringWriter()) { using (XmlTextWriter writer = new XmlTextWriter(sww)) { if (options.Formatted) { writer.Formatting = System.Xml.Formatting.Indented; writer.Indentation = 4; } xsSubmit.Serialize(writer, obj); xml = sww.ToString(); using (var sw = new StreamWriter(ms, Fi.StandardEncoding)) { sw.Write(xml); ms.Seek(0, SeekOrigin.Begin); await StreamOptions .Process(ms, async (usableStream) => { await Task.Yield(); await usableStream.CopyToAsync(rawStream); }); } } } } }
public static async Task FromXml(this IMultiSerializableObject obj, Stream rawStream, FTHSerializableOptions options = null) { if (obj == null) { return; } var StreamOptions = GetOpenStreamOptions(options); await StreamOptions .Process(rawStream, async (usableStream) => { await Task.Yield(); var serializer = new XmlSerializer(obj.GetType()); // this is necessary because XML Deserializer is a bitch using (StreamReader reader = new StreamReader(usableStream, Fi.StandardEncoding)) { var retv = serializer.Deserialize(reader); Fi.Tech.MemberwiseCopy(retv, obj); } }); }
public static async Task FromJson(this IMultiSerializableObject obj, Stream rawStream, FTHSerializableOptions options = null) { if (obj == null) { return; } var StreamOptions = GetOpenStreamOptions(options); await StreamOptions .Process(rawStream, async (usableStream) => { using (var reader = new StreamReader(usableStream, Fi.StandardEncoding)) { var json = await reader.ReadToEndAsync(); try { var parse = JsonConvert.DeserializeObject(json, obj.GetType()); Fi.Tech.MemberwiseCopy(parse, obj); } catch (Exception x) { Fi.Tech.WriteLine("Error parsing JSON File: " + x.Message); throw x; } } }); }