134 lines
5.0 KiB
C#
134 lines
5.0 KiB
C#
namespace Jellyfin.Plugin.OIDC_Auth;
|
|
|
|
/// <summary>
|
|
/// For some reason, the generic Dictionary in .net 2.0 is not XML serializable. The following code snippet is a xml
|
|
/// serializable generic dictionary. The dictionary is serializable by implementing the IXmlSerializable interface.
|
|
/// Also see https://weblogs.asp.net/pwelter34/444961 for additional information.
|
|
/// </summary>
|
|
/// <typeparam name="TKey">Type of the dictionary key.</typeparam>
|
|
/// <typeparam name="TValue">Type of the dictionary value.</typeparam>
|
|
[System.Xml.Serialization.XmlRootAttribute("dictionary")]
|
|
public class SerializableDictionary<TKey, TValue>
|
|
: System.Collections.Generic.Dictionary<TKey, TValue>, System.Xml.Serialization.IXmlSerializable {
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="SerializableDictionary{TKey,TValue}" /> class.
|
|
/// </summary>
|
|
public SerializableDictionary() {
|
|
// Empty
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="SerializableDictionary{TKey,TValue}" /> class.
|
|
/// </summary>
|
|
/// <param name="dictionary">Dictionary to convert from.</param>
|
|
public SerializableDictionary(System.Collections.Generic.IDictionary<TKey, TValue> dictionary) : base(dictionary) {
|
|
// Empty
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="SerializableDictionary{TKey,TValue}" /> class.
|
|
/// </summary>
|
|
/// <param name="dictionary">Dictionary to convert from.</param>
|
|
/// <param name="comparer">Comparer for the dictionary.</param>
|
|
public SerializableDictionary(
|
|
System.Collections.Generic.IDictionary<TKey, TValue> dictionary,
|
|
System.Collections.Generic.IEqualityComparer<TKey> comparer
|
|
) : base(
|
|
dictionary, comparer) {
|
|
// Empty
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="SerializableDictionary{TKey,TValue}" /> class.
|
|
/// </summary>
|
|
/// <param name="comparer">Comparer for the dictionary.</param>
|
|
public SerializableDictionary(System.Collections.Generic.IEqualityComparer<TKey> comparer) : base(comparer) {
|
|
// Empty
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="SerializableDictionary{TKey,TValue}" /> class.
|
|
/// </summary>
|
|
/// <param name="capacity">Capacity of the dictionary.</param>
|
|
public SerializableDictionary(int capacity) : base(capacity) {
|
|
// Empty
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="SerializableDictionary{TKey,TValue}" /> class.
|
|
/// </summary>
|
|
/// <param name="capacity">Capacity of the dictionary.</param>
|
|
/// <param name="comparer">Comparer for the dictionary.</param>
|
|
public SerializableDictionary(int capacity, System.Collections.Generic.IEqualityComparer<TKey> comparer) : base(
|
|
capacity, comparer) {
|
|
// Empty
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the schema of the XML object.
|
|
/// </summary>
|
|
/// <returns>Nothing.</returns>
|
|
public System.Xml.Schema.XmlSchema GetSchema() {
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads XML and changes this object to be an instance of that data.
|
|
/// </summary>
|
|
/// <param name="reader">The XML reader to read from.</param>
|
|
public void ReadXml(System.Xml.XmlReader reader) {
|
|
System.Xml.Serialization.XmlSerializer keySerializer = new(typeof(TKey));
|
|
System.Xml.Serialization.XmlSerializer valueSerializer = new(typeof(TValue));
|
|
|
|
bool wasEmpty = reader.IsEmptyElement;
|
|
reader.Read();
|
|
|
|
if (wasEmpty) {
|
|
return;
|
|
}
|
|
|
|
while (reader.NodeType != System.Xml.XmlNodeType.EndElement) {
|
|
reader.ReadStartElement("item");
|
|
|
|
reader.ReadStartElement("key");
|
|
TKey key = (TKey)keySerializer.Deserialize(reader);
|
|
reader.ReadEndElement();
|
|
|
|
reader.ReadStartElement("value");
|
|
TValue value = (TValue)valueSerializer.Deserialize(reader);
|
|
reader.ReadEndElement();
|
|
|
|
this.Add(key, value);
|
|
|
|
reader.ReadEndElement();
|
|
reader.MoveToContent();
|
|
}
|
|
|
|
reader.ReadEndElement();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes XML to the XML writer from this object.
|
|
/// </summary>
|
|
/// <param name="writer">An instance of the XmlWriter class.</param>
|
|
public void WriteXml(System.Xml.XmlWriter writer) {
|
|
System.Xml.Serialization.XmlSerializer keySerializer = new(typeof(TKey));
|
|
System.Xml.Serialization.XmlSerializer valueSerializer = new(typeof(TValue));
|
|
|
|
foreach (TKey key in this.Keys) {
|
|
writer.WriteStartElement("item");
|
|
|
|
writer.WriteStartElement("key");
|
|
keySerializer.Serialize(writer, key);
|
|
writer.WriteEndElement();
|
|
|
|
writer.WriteStartElement("value");
|
|
TValue value = this[key];
|
|
valueSerializer.Serialize(writer, value);
|
|
writer.WriteEndElement();
|
|
|
|
writer.WriteEndElement();
|
|
}
|
|
}
|
|
}
|