|
|
|
@ -23,6 +23,20 @@ namespace Emby.Server.Implementations.Serialization
|
|
|
|
|
static (_, t) => new XmlSerializer(t),
|
|
|
|
|
type);
|
|
|
|
|
|
|
|
|
|
private static void ThrowCollectibleException(NotSupportedException e)
|
|
|
|
|
{
|
|
|
|
|
var isCollectibleException = string.Equals(e.Message, "A non-collectible assembly may not reference a collectible assembly.", StringComparison.Ordinal);
|
|
|
|
|
if (isCollectibleException)
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException(
|
|
|
|
|
$"It seems you are using the server's XML serializer in your plugin.\n" +
|
|
|
|
|
$"This serializer is only used by the server to handle the plugin's config file " +
|
|
|
|
|
$"and is not intended for the plugin to use directly.\n" +
|
|
|
|
|
$"Please create your own XML serializer instance to handle plugin specific files.",
|
|
|
|
|
e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serializes to writer.
|
|
|
|
|
/// </summary>
|
|
|
|
@ -42,10 +56,18 @@ namespace Emby.Server.Implementations.Serialization
|
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
|
public object? DeserializeFromStream(Type type, Stream stream)
|
|
|
|
|
{
|
|
|
|
|
using (var reader = XmlReader.Create(stream))
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (var reader = XmlReader.Create(stream))
|
|
|
|
|
{
|
|
|
|
|
var netSerializer = GetSerializer(type);
|
|
|
|
|
return netSerializer.Deserialize(reader);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (NotSupportedException e)
|
|
|
|
|
{
|
|
|
|
|
var netSerializer = GetSerializer(type);
|
|
|
|
|
return netSerializer.Deserialize(reader);
|
|
|
|
|
ThrowCollectibleException(e);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -56,11 +78,19 @@ namespace Emby.Server.Implementations.Serialization
|
|
|
|
|
/// <param name="stream">The stream.</param>
|
|
|
|
|
public void SerializeToStream(object obj, Stream stream)
|
|
|
|
|
{
|
|
|
|
|
using (var writer = new StreamWriter(stream, null, IODefaults.StreamWriterBufferSize, true))
|
|
|
|
|
using (var textWriter = new XmlTextWriter(writer))
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
textWriter.Formatting = Formatting.Indented;
|
|
|
|
|
SerializeToWriter(obj, textWriter);
|
|
|
|
|
using (var writer = new StreamWriter(stream, null, IODefaults.StreamWriterBufferSize, true))
|
|
|
|
|
using (var textWriter = new XmlTextWriter(writer))
|
|
|
|
|
{
|
|
|
|
|
textWriter.Formatting = Formatting.Indented;
|
|
|
|
|
SerializeToWriter(obj, textWriter);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (NotSupportedException e)
|
|
|
|
|
{
|
|
|
|
|
ThrowCollectibleException(e);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -71,9 +101,17 @@ namespace Emby.Server.Implementations.Serialization
|
|
|
|
|
/// <param name="file">The file.</param>
|
|
|
|
|
public void SerializeToFile(object obj, string file)
|
|
|
|
|
{
|
|
|
|
|
using (var stream = new FileStream(file, FileMode.Create, FileAccess.Write))
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (var stream = new FileStream(file, FileMode.Create, FileAccess.Write))
|
|
|
|
|
{
|
|
|
|
|
SerializeToStream(obj, stream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (NotSupportedException e)
|
|
|
|
|
{
|
|
|
|
|
SerializeToStream(obj, stream);
|
|
|
|
|
ThrowCollectibleException(e);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -85,9 +123,17 @@ namespace Emby.Server.Implementations.Serialization
|
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
|
public object? DeserializeFromFile(Type type, string file)
|
|
|
|
|
{
|
|
|
|
|
using (var stream = File.OpenRead(file))
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return DeserializeFromStream(type, stream);
|
|
|
|
|
using (var stream = File.OpenRead(file))
|
|
|
|
|
{
|
|
|
|
|
return DeserializeFromStream(type, stream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (NotSupportedException e)
|
|
|
|
|
{
|
|
|
|
|
ThrowCollectibleException(e);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -99,9 +145,17 @@ namespace Emby.Server.Implementations.Serialization
|
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
|
public object? DeserializeFromBytes(Type type, byte[] buffer)
|
|
|
|
|
{
|
|
|
|
|
using (var stream = new MemoryStream(buffer, 0, buffer.Length, false, true))
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (var stream = new MemoryStream(buffer, 0, buffer.Length, false, true))
|
|
|
|
|
{
|
|
|
|
|
return DeserializeFromStream(type, stream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (NotSupportedException e)
|
|
|
|
|
{
|
|
|
|
|
return DeserializeFromStream(type, stream);
|
|
|
|
|
ThrowCollectibleException(e);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|