# Adding custom NewtonSoft JsonConverter breaks client side viewmodel serialization

**URL:** <https://forum.dotvvm.com/t/adding-custom-newtonsoft-jsonconverter-breaks-client-side-viewmodel-serialization/183>\
**Category:** Discussion in English\
**Created:** [March 29, 2024, 5:50pm UTC](https://forum.dotvvm.com/t/adding-custom-newtonsoft-jsonconverter-breaks-client-side-viewmodel-serialization/183 "2024-03-29T17:50:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ishoe](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/ishoe/32/38_2.png) [@ishoe](https://forum.dotvvm.com/u/ishoe)\
**Post date:** [March 29, 2024, 5:50pm UTC](https://forum.dotvvm.com/t/adding-custom-newtonsoft-jsonconverter-breaks-client-side-viewmodel-serialization/183/1 "2024-03-29T17:50:13Z")

</div>

Hello DotVVM team! I’ve run into a problem where I’m trying to add a NewtonSoft [CustomCreationConverter](https://www.newtonsoft.com/json/help/html/customcreationconverter.htm) 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.

 ![image](https://forum.dotvvm.com/uploads/default/original/1X/78e86e4e3bd1b0bfc9edb0ffdc97a48bfee8ca79.png)

I’ve narrowed it down to the addition of the C# converter attribute as what triggers this error.

```auto
[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.

 ![image](https://forum.dotvvm.com/uploads/default/original/1X/ee2afcd8c86405bd0ea4ab6d77a79abedfbc30dd.png)

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

```auto
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(),
		};
	}
}

```

---

<div class="post-metadata">

**Author:** ![exyi](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/exyi/32/9_2.png) [@exyi](https://forum.dotvvm.com/u/exyi)\
**Post date:** [April 4, 2024, 11:20am UTC](https://forum.dotvvm.com/t/adding-custom-newtonsoft-jsonconverter-breaks-client-side-viewmodel-serialization/183/2 "2024-04-04T11:20:35Z")

</div>

Hello! The crash you are getting is definitely a bug, we’re fixing that in [#1803](https://github.com/riganti/dotvvm/pull/1803).

However, I’m not sure that actually solves the problem for you. Custom converters are generally quite tricky, since DotVVM client-side essentially assumes that the view model is serialized by the DotVVM converter. In your case, I guess the CustomCreationConverter should let the DotVVM ViewModelJsonConverter do the serialization and only call your code for deserialization. I have no idea what that doesn’t work, could you please try inheriting from the general JsonConverter and manually handing the serialization to `ViewModelJsonConverter`. Something like the following should do the trick

```auto
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
    serializer.Converters.OfType<ViewModelJsonConverter>().First().WriteJson(writer, value, serializer);
}

```

---

<div class="post-metadata">

**Author:** ![ishoe](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/ishoe/32/38_2.png) [@ishoe](https://forum.dotvvm.com/u/ishoe)\
**Post date:** [April 4, 2024, 5:37pm UTC](https://forum.dotvvm.com/t/adding-custom-newtonsoft-jsonconverter-breaks-client-side-viewmodel-serialization/183/3 "2024-04-04T17:37:33Z")

</div>

Thanks for the response @exyi. I also had a similar thought of extending the DotVVM one but I ended up finding a different root cause in that, my abstract class property was also in a class ambiguous `object` dictionary (a migration we’re doing of WebForms’ ViewState.)

Once I promoted my property as first class citizen on my ViewModel, the original converter worked and didn’t seem to conflict with DotVVM’s.

It’s still odd to me that `$type` would be missing when I was still using the dictionary and specifically, the addition of my converter would remove `$type`. Either way, I’m in a good state now and I appreciate your time in this matter.
