Skip to content

Commit

Permalink
Add support for conditional serialization to ZeroDepJson
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Bylund committed Dec 14, 2023
1 parent f7602f1 commit 607a5de
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Runtime/Game/LootLockerObfuscator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using LLlibs.ZeroDepJson;
using System;
using System.Collections.Generic;
using System.Text;
#if LOOTLOCKER_USE_NEWTONSOFTJSON
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
#else
using LLlibs.ZeroDepJson;
#endif

namespace LootLocker
{
Expand Down
55 changes: 55 additions & 0 deletions Runtime/Libraries/ZeroDepJson/ZeroDepJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,33 @@ private static bool TryGetObjectDefaultValue(PropertyDescriptor pd, out object v
return false;
}

private static bool TryGetObjectConditionalSerialization(Type type, MemberDefinition memberDefinition, out MethodInfo conditionalSerializationMethodInfo)
{
conditionalSerializationMethodInfo = null;
List<string> potentialConditionalSerializationMethodNames = new List<string>
{
"ShouldSerialize" + memberDefinition.EscapedWireName,
"ShouldSerialize" + memberDefinition.WireName,
"ShouldSerialize" + memberDefinition.Name
};
foreach (string methodName in potentialConditionalSerializationMethodNames)
{
try
{
MethodInfo methodInfo = type.GetMethod(methodName);
if (methodInfo != null && methodInfo.ReturnType == typeof(bool) && methodInfo.GetParameters().Length == 0)
{
conditionalSerializationMethodInfo = methodInfo;
break;
}
}
catch (AmbiguousMatchException e) { }
catch (ArgumentNullException e) { }
}

return conditionalSerializationMethodInfo != null;
}

private static string GetObjectName(PropertyDescriptor pd, string defaultName)
{
foreach (var att in pd.Attributes.Cast<Attribute>())
Expand Down Expand Up @@ -1649,6 +1676,22 @@ public virtual string EscapedWireName
/// </value>
public virtual object DefaultValue { get; set; }

/// <summary>
/// Gets or sets a value indicating whether this instance has conditional serialization.
/// </summary>
/// <value>
/// <c>true</c> if this instance has conditional serialization; otherwise, <c>false</c>.
/// </value>
public virtual bool HasConditionalSerialization { get; set; }

/// <summary>
/// Gets or sets conditional serialization method info
/// </summary>
/// <value>
/// The info of the method on the type that tells whether this property should be serialized
/// </value>
public virtual MethodInfo ConditionalSerializationMethod { get; set; }

/// <summary>
/// Gets or sets the accessor.
/// </summary>
Expand Down Expand Up @@ -3208,6 +3251,14 @@ public void WriteValues(TextWriter writer, object component, IDictionary<object,
continue;
}

if (member.HasConditionalSerialization && member.ConditionalSerializationMethod != null)
{
if (!(bool)member.ConditionalSerializationMethod.Invoke(component, new object[] { }))
{
continue;
}
}

if (!first)
{
writer.Write(',');
Expand Down Expand Up @@ -3452,6 +3503,8 @@ private static IEnumerable<MemberDefinition> EnumerateDefinitionsUsingReflection

ma.HasDefaultValue = TryGetObjectDefaultValue(info, out var defaultValue);
ma.DefaultValue = defaultValue;
ma.HasConditionalSerialization = TryGetObjectConditionalSerialization(type, ma, out MethodInfo conditionalSerializationMethodInfo);
ma.ConditionalSerializationMethod = conditionalSerializationMethodInfo;
ma.Accessor = (IMemberAccessor)Activator.CreateInstance(typeof(FieldInfoAccessor), info);
yield return ma;
}
Expand Down Expand Up @@ -3509,6 +3562,8 @@ private static IEnumerable<MemberDefinition> EnumerateDefinitionsUsingTypeDescri

ma.HasDefaultValue = TryGetObjectDefaultValue(descriptor, out var defaultValue);
ma.DefaultValue = defaultValue;
ma.HasConditionalSerialization = TryGetObjectConditionalSerialization(type, ma, out MethodInfo conditionalSerializationMethodInfo);
ma.ConditionalSerializationMethod = conditionalSerializationMethodInfo;
ma.Accessor = (IMemberAccessor)Activator.CreateInstance(typeof(PropertyDescriptorAccessor), descriptor);
yield return ma;
}
Expand Down

0 comments on commit 607a5de

Please sign in to comment.