// 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 { /// Serializer implementation for IDynamicStep. /// 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 { /// The order of this serializer. public override double Order { get { return new TestStepSerializer().Order + 1; } } /// /// Serializes a dynamic step. /// /// /// /// /// 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); } } /// Deserialization implementation. public override bool Deserialize(XElement elem, ITypeData t, Action setter) { if (t.DescendsTo(typeof(IDynamicStep)) == false) return false; try { IDynamicStep step = (IDynamicStep)t.CreateInstance(Array.Empty()); 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().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 serializing = new HashSet(); } }