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
77
78
79
//            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> Serializer implementation for IDynamicStep. </summary>
    /// It needs to act like an object serializer, which is why it inherits from it.
    /// It implementes TapSerializerPlugin, so it can explicitly override serializer behavior.
    internal class DynamicStepSerializer : ObjectSerializer
    {
        /// <summary> The order of this serializer. </summary>
        public override double Order { get { return new TestStepSerializer().Order + 1; } }
        
 
        /// <summary>
        /// Serializes a dynamic step.
        /// </summary>
        /// <param name="elem"></param>
        /// <param name="obj"></param>
        /// <param name="expectedType"></param>
        /// <returns></returns>
        public override bool Serialize(XElement elem, object obj, ITypeData expectedType)
        {
            if (obj is IDynamicStep == false) return false;
            if (serializing.Contains(elem)) return false;
            var step = (IDynamicStep)obj;
 
            try
            {
                serializing.Add(elem);
                return Serializer.Serialize(elem, obj, expectedType);
            }
            finally
            {
                serializing.Remove(elem);
                elem.SetAttributeValue("type", step.GetStepFactoryType().FullName);
            }
        }
 
        /// <summary> Deserialization implementation. </summary>
        public override bool Deserialize(XElement elem, ITypeData t, Action<object> setter)
        {
            if (t.DescendsTo(typeof(IDynamicStep)) == false)
                return false;
            
            try
            {
                IDynamicStep step = (IDynamicStep)t.CreateInstance(Array.Empty<object>());
                Serializer.Register(step);
                
                TryDeserializeObject(elem, TypeData.GetTypeData(step), setter, step, logWarnings: false);
                
                ITestStep genStep = step.GetStep();
                bool res = true;
                if (elem.IsEmpty == false)
                    res = TryDeserializeObject(elem, TypeData.GetTypeData(genStep), setter, genStep, logWarnings: false);
                else
                    setter(genStep);
                Serializer.GetSerializer<TestStepSerializer>().FixupStep(genStep, true);
                return res;
            }
            catch(Exception e)
            {
                Log.Error("Unable to deserialize step of type {0}. Error was: {1}", t, e.Message);
                
                Log.Debug(e);
                return true;
            }
        }
        
        HashSet<XElement> serializing = new HashSet<XElement>();
    }
 
}