Let exceptions boil up

This commit is contained in:
Matt Nadareski
2023-07-30 09:00:15 -04:00
parent 9ae8036b0a
commit 378f9cbfd1
15 changed files with 1107 additions and 1321 deletions

View File

@@ -17,21 +17,13 @@ namespace SabreTools.Serialization
/// <returns>True on successful serialization, false otherwise</returns>
public static bool SerializeToFile(T? obj, string path)
{
try
{
using var stream = SerializeToStream(obj);
if (stream == null)
return false;
using var fs = File.OpenWrite(path);
stream.CopyTo(fs);
return true;
}
catch
{
// TODO: Handle logging the exception
using var stream = SerializeToStream(obj);
if (stream == null)
return false;
}
using var fs = File.OpenWrite(path);
stream.CopyTo(fs);
return true;
}
/// <summary>
@@ -41,31 +33,23 @@ namespace SabreTools.Serialization
/// <returns>Stream containing serialized data on success, null otherwise</returns>
public static Stream? SerializeToStream(T? obj)
{
try
{
// If the object is null
if (obj == null)
return null;
// Setup the serializer and the reader
var serializer = new XmlSerializer(typeof(T));
var settings = new XmlWriterSettings
{
CheckCharacters = false,
};
var stream = new MemoryStream();
var streamWriter = new StreamWriter(stream);
var xmlWriter = XmlWriter.Create(streamWriter, settings);
// Perform the deserialization and return
serializer.Serialize(xmlWriter, obj);
return stream;
}
catch
{
// TODO: Handle logging the exception
// If the object is null
if (obj == null)
return null;
}
// Setup the serializer and the reader
var serializer = new XmlSerializer(typeof(T));
var settings = new XmlWriterSettings
{
CheckCharacters = false,
};
var stream = new MemoryStream();
var streamWriter = new StreamWriter(stream);
var xmlWriter = XmlWriter.Create(streamWriter, settings);
// Perform the deserialization and return
serializer.Serialize(xmlWriter, obj);
return stream;
}
}
}