Skip to content

Commit

Permalink
Rename constant identifier and clarify its use
Browse files Browse the repository at this point in the history
  • Loading branch information
glopesdev committed Jul 10, 2024
1 parent 2d9db3c commit 3d92c04
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Bonsai.Core/Expressions/IncludeWorkflowBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public sealed class IncludeWorkflowBuilder : VariableArgumentExpressionBuilder,
{
const char AssemblySeparator = ':';
internal const string BuildUriPrefix = "::build:";
const string DefaultSerializerPropertyName = "Property";
const string PlaceholderSerializerPropertyName = "__Property__";
static readonly XElement[] EmptyProperties = new XElement[0];
static readonly XmlSerializerNamespaces DefaultSerializerNamespaces = GetXmlSerializerNamespaces();

Expand Down Expand Up @@ -243,7 +243,7 @@ void DeserializeProperty(XElement element, PropertyDescriptor property)
}

var previousName = element.Name;
element.Name = XName.Get(DefaultSerializerPropertyName, element.Name.NamespaceName);
element.Name = XName.Get(PlaceholderSerializerPropertyName, element.Name.NamespaceName);
try
{
var serializer = PropertySerializer.GetXmlSerializer(property.PropertyType);
Expand Down Expand Up @@ -431,6 +431,9 @@ public override Expression Build(IEnumerable<Expression> arguments)
return workflow.BuildNested(arguments, includeContext);
}

// We serialize all properties using the same placeholder root name to allow us to reuse
// a single serializer for each different property type. This means we need to rename
// the actual XElement name before or after serialization and deserialization.
static class PropertySerializer
{
static readonly Dictionary<Type, XmlSerializer> serializerCache = new();
Expand All @@ -443,7 +446,7 @@ internal static XmlSerializer GetXmlSerializer(Type type)
{
if (!serializerCache.TryGetValue(type, out serializer))
{
var xmlRoot = new XmlRootAttribute(DefaultSerializerPropertyName) { Namespace = Constants.XmlNamespace };
var xmlRoot = new XmlRootAttribute(PlaceholderSerializerPropertyName) { Namespace = Constants.XmlNamespace };
serializer = new XmlSerializer(type, xmlRoot);
serializerCache.Add(type, serializer);
}
Expand Down

0 comments on commit 3d92c04

Please sign in to comment.