// Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Collections.Generic;
using System.Xml.Linq;
namespace OpenTap.Plugins
{
/// For serializing/deserializing KeyValuePairs (mostly for use with Dictionaries).
/// It requires that the generic arguments of the KeyValuePair can be serialized.
internal class KeyValuePairSerializer : TapSerializerPlugin
{
/// Creates a Key and a Value node in the XML.
public override bool Serialize(XElement node, object obj, ITypeData _expectedType)
{
if(obj == null || false == obj.GetType().DescendsTo(typeof(KeyValuePair<,>)))
{
return false;
}
if (_expectedType is TypeData expectedType2 && expectedType2.Type is Type expectedType)
{
var key = new XElement("Key");
var value = new XElement("Value");
bool keyok = Serializer.Serialize(key, _expectedType.GetMember("Key").GetValue(obj), TypeData.FromType(expectedType.GetGenericArguments()[0]));
bool valueok = Serializer.Serialize(value, _expectedType.GetMember("Value").GetValue(obj), TypeData.FromType(expectedType.GetGenericArguments()[1]));
if (!keyok || !valueok)
return false;
node.Add(key);
node.Add(value);
return true;
}
return false;
}
/// Looks for a Key and a Value node in the XML.
public override bool Deserialize(XElement node, ITypeData _t, Action