< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.Serialization.MyXmlSerializer
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/Serialization/MyXmlSerializer.cs
Line coverage
89%
Covered lines: 25
Uncovered lines: 3
Coverable lines: 28
Total lines: 108
Line coverage: 89.2%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
GetSerializer(...)50%22100%
SerializeToWriter(...)100%11100%
DeserializeFromStream(...)100%11100%
SerializeToStream(...)100%11100%
SerializeToFile(...)100%11100%
DeserializeFromFile(...)100%11100%
DeserializeFromBytes(...)100%210%

File(s)

/srv/git/jellyfin/Emby.Server.Implementations/Serialization/MyXmlSerializer.cs

#LineLine coverage
 1using System;
 2using System.Collections.Concurrent;
 3using System.IO;
 4using System.Xml;
 5using System.Xml.Serialization;
 6using MediaBrowser.Model.IO;
 7using MediaBrowser.Model.Serialization;
 8
 9namespace Emby.Server.Implementations.Serialization
 10{
 11    /// <summary>
 12    /// Provides a wrapper around third party xml serialization.
 13    /// </summary>
 14    public class MyXmlSerializer : IXmlSerializer
 15    {
 16        // Need to cache these
 17        // http://dotnetcodebox.blogspot.com/2013/01/xmlserializer-class-may-result-in.html
 2118        private readonly ConcurrentDictionary<string, XmlSerializer> _serializers = new();
 19
 20        private XmlSerializer GetSerializer(Type type)
 12721            => _serializers.GetOrAdd(
 12722                type.FullName ?? throw new ArgumentException($"Invalid type {type}."),
 12723                static (_, t) => new XmlSerializer(t),
 12724                type);
 25
 26        /// <summary>
 27        /// Serializes to writer.
 28        /// </summary>
 29        /// <param name="obj">The obj.</param>
 30        /// <param name="writer">The writer.</param>
 31        private void SerializeToWriter(object obj, XmlWriter writer)
 32        {
 12633            var netSerializer = GetSerializer(obj.GetType());
 12634            netSerializer.Serialize(writer, obj);
 12635        }
 36
 37        /// <summary>
 38        /// Deserializes from stream.
 39        /// </summary>
 40        /// <param name="type">The type.</param>
 41        /// <param name="stream">The stream.</param>
 42        /// <returns>System.Object.</returns>
 43        public object? DeserializeFromStream(Type type, Stream stream)
 44        {
 145            using (var reader = XmlReader.Create(stream))
 46            {
 147                var netSerializer = GetSerializer(type);
 148                return netSerializer.Deserialize(reader);
 49            }
 150        }
 51
 52        /// <summary>
 53        /// Serializes to stream.
 54        /// </summary>
 55        /// <param name="obj">The obj.</param>
 56        /// <param name="stream">The stream.</param>
 57        public void SerializeToStream(object obj, Stream stream)
 58        {
 12659            using (var writer = new StreamWriter(stream, null, IODefaults.StreamWriterBufferSize, true))
 12660            using (var textWriter = new XmlTextWriter(writer))
 61            {
 12662                textWriter.Formatting = Formatting.Indented;
 12663                SerializeToWriter(obj, textWriter);
 12664            }
 12665        }
 66
 67        /// <summary>
 68        /// Serializes to file.
 69        /// </summary>
 70        /// <param name="obj">The obj.</param>
 71        /// <param name="file">The file.</param>
 72        public void SerializeToFile(object obj, string file)
 73        {
 10574            using (var stream = new FileStream(file, FileMode.Create, FileAccess.Write))
 75            {
 10576                SerializeToStream(obj, stream);
 10577            }
 10578        }
 79
 80        /// <summary>
 81        /// Deserializes from file.
 82        /// </summary>
 83        /// <param name="type">The type.</param>
 84        /// <param name="file">The file.</param>
 85        /// <returns>System.Object.</returns>
 86        public object? DeserializeFromFile(Type type, string file)
 87        {
 4388            using (var stream = File.OpenRead(file))
 89            {
 190                return DeserializeFromStream(type, stream);
 91            }
 192        }
 93
 94        /// <summary>
 95        /// Deserializes from bytes.
 96        /// </summary>
 97        /// <param name="type">The type.</param>
 98        /// <param name="buffer">The buffer.</param>
 99        /// <returns>System.Object.</returns>
 100        public object? DeserializeFromBytes(Type type, byte[] buffer)
 101        {
 0102            using (var stream = new MemoryStream(buffer, 0, buffer.Length, false, true))
 103            {
 0104                return DeserializeFromStream(type, stream);
 105            }
 0106        }
 107    }
 108}