chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//            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
{
    /// <summary> For serializing/deserializing KeyValuePairs (mostly for use with Dictionaries). 
    /// It requires that the generic arguments of the KeyValuePair can be serialized.</summary>
    internal class KeyValuePairSerializer : TapSerializerPlugin
    {
        /// <summary> Creates a Key and a Value node in the XML.</summary>
        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;
        }
 
        /// <summary> Looks for a Key and a Value node in the XML. </summary>
        public override bool Deserialize(XElement node, ITypeData _t, Action<object> setter)
        {
            Type t = (_t as TypeData)?.Type;
            if (t == null) return false;
            if (false == t.DescendsTo(typeof(KeyValuePair<,>)))
                return false;
 
            var key = node.Element("Key");
            var value = node.Element("Value");
            bool gotkey = false, gotvalue = false;
            object key_value = null, value_value = null;
 
            // this is a bit complicated
            // because the evaluation of deserialization of 
            // sub items might be defered.
            bool keyok = Serializer.Deserialize(key, x =>
            {
                key_value = x;
                gotkey = true;
                if (gotvalue)
                    setter(Activator.CreateInstance(t, key_value, value_value));
            }, t.GetGenericArguments()[0]);
            if (!keyok)
                return false;
            bool valueok = Serializer.Deserialize(value, x =>
            {
                value_value = x;
                gotvalue = true;
                if (gotkey)
                    setter(Activator.CreateInstance(t, key_value, value_value));
            } , t.GetGenericArguments()[1]);
            
            return valueok;
        }
    }
 
}