Hello DotVVM team! I’ve run into a problem where I’m trying to add a NewtonSoft CustomCreationConverter in order to serialize additional type information for an abstract class I’m defining on one of my ViewModels. The issue is that, during viewmodel validation happening client side (before making POST call), dotvvm internal throws this error.
I’ve narrowed it down to the addition of the C# converter attribute as what triggers this error.
[Serializable]
[JsonConverter(typeof(AbstractCustomRuleJsonConverter))] // the addition of this line induces client side error
public abstract partial class AbstractCustomRule : CustomRule, ICustomRule
Seeing as this error was clientside, I decided to diff the serialized viewmodel, and it looks like as a side effect of adding the converter, the $type
property is missing in viewmodel json. See below image of left side with converter missing $type vs right side without converter including it.
A further confusing point is that the CustomCreationConverter
is only meant to modify deserialization but the initial page load serialization of ViewModel is what is being affected.
Below is my Converter impl
using System;
using CustomRules.DocumentRules;
using CustomRules.FolderRules;
using CustomRules.ScheduledRules;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
namespace CustomRules.Common;
public class AbstractCustomRuleJsonConverter : CustomCreationConverter<AbstractCustomRule>
{
private string _ruleTypeName;
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jsonObj = JToken.ReadFrom(reader);
_ruleTypeName = jsonObj["RuleTypeName"].ToString();
return base.ReadJson(jsonObj.CreateReader(), objectType, existingValue, serializer);
}
public override AbstractCustomRule Create(Type objectType)
{
return _ruleTypeName switch
{
"DocumentRule" => new DocumentRule(),
"FolderRule" => new FolderRule(),
"ScheduledRule" => new ScheduledRule(),
_ => throw new NotImplementedException(),
};
}
}