# \[view models / page structuring series\] Exception: Can not deserialize view model, Use the \[JsonConstructor\] attribute

**URL:** <https://forum.dotvvm.com/t/view-models-page-structuring-series-exception-can-not-deserialize-view-model-use-the-jsonconstructor-attribute/150>\
**Category:** Discussion in English\
**Created:** [February 19, 2024, 5:05pm UTC](https://forum.dotvvm.com/t/view-models-page-structuring-series-exception-can-not-deserialize-view-model-use-the-jsonconstructor-attribute/150 "2024-02-19T17:05:32Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarianV](https://forum.dotvvm.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@MarianV](https://forum.dotvvm.com/u/MarianV)\
**Post date:** [February 19, 2024, 5:05pm UTC](https://forum.dotvvm.com/t/view-models-page-structuring-series-exception-can-not-deserialize-view-model-use-the-jsonconstructor-attribute/150/1 "2024-02-19T17:05:32Z")

</div>

This is a very persistent exception I’m facing when selecting a value in any of my dropdowns that previously worked, before the whole thing with restructuring the sub view models as to be nullable and instantiated at PostBack.

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

I’ve already decorated my single constructor as suggested:  
 ![image](https://forum.dotvvm.com/uploads/default/original/1X/64d39cee3cbfdf9ab837f54cfd8bb7c372af3d12.png)

Still DEIing/backtracking on latest changes… looking for a non-serializable type…

Any hint/insight would be greatly appreciated.

---

<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:** [February 19, 2024, 5:52pm UTC](https://forum.dotvvm.com/t/view-models-page-structuring-series-exception-can-not-deserialize-view-model-use-the-jsonconstructor-attribute/150/2 "2024-02-19T17:52:52Z")

</div>

It is trying to construct `Operation`, not the `MeterOperation` where you have the constructor.

Even if `MeterOperation` inherits from `Operation`, DotVVM will not try to instantiate any other type than the declared property type. The “polymorphism” only works (somewhat) if you create the instance before deserialization.

EDIT - solution: make sure it is Newtonsoft.Json.JsonConstructor

---

<div class="post-metadata">

**Author:** ![MarianV](https://forum.dotvvm.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@MarianV](https://forum.dotvvm.com/u/MarianV)\
**Post date:** [February 19, 2024, 5:57pm UTC](https://forum.dotvvm.com/t/view-models-page-structuring-series-exception-can-not-deserialize-view-model-use-the-jsonconstructor-attribute/150/3 "2024-02-19T17:57:34Z")

</div>

sorry, there is no `MeterOperation`, only `Operation` in this thread/scenario.

---

<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:** [February 19, 2024, 6:51pm UTC](https://forum.dotvvm.com/t/view-models-page-structuring-series-exception-can-not-deserialize-view-model-use-the-jsonconstructor-attribute/150/4 "2024-02-19T18:51:29Z")

</div>

Ok, sorry, I made the wrong assumption. This is indeed very strange, we are not picky about constructors if you annotate it as JsonConstructor (the contructor selection is done here: [dotvvm/src/Framework/Framework/ViewModel/Serialization/ViewModelSerializationMapper.cs at main · riganti/dotvvm · GitHub](https://github.com/riganti/dotvvm/blob/main/src/Framework/Framework/ViewModel/Serialization/ViewModelSerializationMapper.cs#L61)).

One idea is that you might have used JsonConstructorAttribute from `System.Text.Json` instead from `Newtonsoft.Json`.

---

<div class="post-metadata">

**Author:** ![MarianV](https://forum.dotvvm.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@MarianV](https://forum.dotvvm.com/u/MarianV)\
**Post date:** [February 19, 2024, 6:52pm UTC](https://forum.dotvvm.com/t/view-models-page-structuring-series-exception-can-not-deserialize-view-model-use-the-jsonconstructor-attribute/150/5 "2024-02-19T18:52:53Z")

</div>

back,

This is related to a piece of code shown in another post:  
 ![image](https://forum.dotvvm.com/uploads/default/original/1X/76129bf721ee937762e66be9690648e081d4d6bb.png)

I thought that if `OperationInstance` will be nullable, I will instantiate it only when it is needed. That is here, in the command handler for button click.

The property:  
 ![image](https://forum.dotvvm.com/uploads/default/original/1X/3276209be6bc80a3fcaacd5b3d5d9f00f7069160.png)

It crashes.

But what I’ve discovered is that if go like this:  
 ![image](https://forum.dotvvm.com/uploads/default/original/1X/f023cf18d60a64255b5d97c8a4cc3d4f5e3bea5c.png)

It works.  
And if I go instead like this:  
 ![image](https://forum.dotvvm.com/uploads/default/original/1X/e2be084af7123d52573a9785c3710d6a7cf85e73.png)

It also crashes.

LE:  
My desire is to instantiate it only when it is needed. Apparently it needs instantiation always. Any idea?

---

<div class="post-metadata">

**Author:** ![MarianV](https://forum.dotvvm.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@MarianV](https://forum.dotvvm.com/u/MarianV)\
**Post date:** [February 19, 2024, 6:57pm UTC](https://forum.dotvvm.com/t/view-models-page-structuring-series-exception-can-not-deserialize-view-model-use-the-jsonconstructor-attribute/150/6 "2024-02-19T18:57:00Z")

</div>

> [@exyi](#):
>
> One idea is that you might have used JsonConstructorAttribute from `System.Text.Json` instead from `Newtonsoft.Json`.

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

---

<div class="post-metadata">

**Author:** ![MarianV](https://forum.dotvvm.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@MarianV](https://forum.dotvvm.com/u/MarianV)\
**Post date:** [February 19, 2024, 7:07pm UTC](https://forum.dotvvm.com/t/view-models-page-structuring-series-exception-can-not-deserialize-view-model-use-the-jsonconstructor-attribute/150/7 "2024-02-19T19:07:30Z")

</div>

holy mother of piglets! This seems to be it. Working if like this:  
 ![image](https://forum.dotvvm.com/uploads/default/original/1X/382a5615a9cc84e63debd23b5cd70439fc74f9d0.png)  
But!  
Unfortunately I have to renounce using primary constructors if I must go with this attribute ☹  
❓ What do you think? Can we have best of both worlds?

Meanwhile, I will revert all this mess and return with a confirmation…

---

<div class="post-metadata">

**Author:** ![MarianV](https://forum.dotvvm.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@MarianV](https://forum.dotvvm.com/u/MarianV)\
**Post date:** [February 19, 2024, 7:48pm UTC](https://forum.dotvvm.com/t/view-models-page-structuring-series-exception-can-not-deserialize-view-model-use-the-jsonconstructor-attribute/150/8 "2024-02-19T19:48:16Z")

</div>

**sidequest**  
I found about how to place that attr on the primary constructor 🫠 Sweet!

Like this:  
 ![image](https://forum.dotvvm.com/uploads/default/original/1X/48976408cfe0f291b1981121e91ca1d154f1fe81.png)  
(see this: [https://stackoverflow.com/a/77552185/2603874](https://stackoverflow.com/a/77552185/2603874))

---

<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:** [February 19, 2024, 8:23pm UTC](https://forum.dotvvm.com/t/view-models-page-structuring-series-exception-can-not-deserialize-view-model-use-the-jsonconstructor-attribute/150/9 "2024-02-19T20:23:44Z")

</div>

Ok, great! This JsonConstructor requirement is there for backwards compatibility with DotVVM 4.0, so I’d very happily just allow primary constructor by default, since it could not be used by the old code. Unfortunately, it seems that I cannot easily tell it apart using reflection 😕

If the constructor parameters are used in method/property bodies, C# creates compiler generated fields named `<{parameterName}>P`. We could tell it apart by this, but no fields are generated if the parameter is only used to initialize properties or other fields, so it wouldn’t be reliable. If you (or anyone else) would have ideas, they are definitely welcome

[sharplab with the experiments](https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA+ABATARgLABQhGAzAATZkDCAFAJYB2ALmQIYA0ZjLwn3ZYAJRkA3oTISK5fgEEyAXgB8bMgGoywNQIDc4yXokYUZALI1hIlfLI5tZAL6FHRAqQpYyAEXrM2Fg1JcvnLWrLoEzsTklACiPiys/gSSgbKiZADmMEx2AM7ZdvYKbOH2QA=)

---

<div class="post-metadata">

**Author:** ![MarianV](https://forum.dotvvm.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@MarianV](https://forum.dotvvm.com/u/MarianV)\
**Post date:** [February 19, 2024, 8:26pm UTC](https://forum.dotvvm.com/t/view-models-page-structuring-series-exception-can-not-deserialize-view-model-use-the-jsonconstructor-attribute/150/10 "2024-02-19T20:26:02Z")

</div>

I just use `JsonConstructor` to make the app work and not crash when I PostBack, that’s all. I would be happy to not use this attr but what alternative there is?

---

<div class="post-metadata">

**Author:** ![MarianV](https://forum.dotvvm.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@MarianV](https://forum.dotvvm.com/u/MarianV)\
**Post date:** [February 19, 2024, 8:42pm UTC](https://forum.dotvvm.com/t/view-models-page-structuring-series-exception-can-not-deserialize-view-model-use-the-jsonconstructor-attribute/150/11 "2024-02-19T20:42:19Z")

</div>

Now I have another problem 🐢, with the nested view models that have properties from the parent view model fed in them through the primary constructor (see below code).

Crash log:

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

I tried to find these exceptions on the web and no luck, suggesting me that they are very specific to DotVVM, more so with the declaring type being `DotVVM.Framework`.

And the failing primary constructor of the SVM:  
 ![image](https://forum.dotvvm.com/uploads/default/original/1X/b45507814b5175bd089e67839911f84b4dc74b7b.png)

Here, `InboundSVM` is a sub view model (S-V-M) part of the view model of the MarkupControl.  
The `SectionNewAAASVM` is instantiated here:  
 ![image](https://forum.dotvvm.com/uploads/default/original/1X/27cddeb792a61a8b18f8bbbc262b086744cfbf8e.png)  
At PostBack, in a method called by the parent view model of the MarkupControl (the grandparent view model, that is), here:  
 ![image](https://forum.dotvvm.com/uploads/default/original/1X/055118fd7d65f8efc2e350190b9b8161d09b807e.png)

Aaaand, those things passed in (InboundParams, UserAlerts etc.) are properties in the VM of the MarkupControl:  
 ![image](https://forum.dotvvm.com/uploads/default/original/1X/18c926cb1cd49fe1dc458925d74a30baf18ac7b0.png)

I don’t know what to do about this, I suck at dependency injection and I’m going in circles here… Any idea?

PS:  
The reason for passing InboundParams, UserAlerts etc. is that the SVMs need to read from or write to them.

PS2:  
And InboundParams, UserAlerts etc. are local tot the VM of the MarkupControl, I don’t want to expose them and flood Program.cs with registering them along with many others from other pages, as services and complicating things  
 ![image](https://forum.dotvvm.com/uploads/default/original/1X/75551e28bedb8cc12d36709a7000815297435897.png)

---

<div class="post-metadata">

**Author:** ![MarianV](https://forum.dotvvm.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@MarianV](https://forum.dotvvm.com/u/MarianV)\
**Post date:** [February 19, 2024, 9:05pm UTC](https://forum.dotvvm.com/t/view-models-page-structuring-series-exception-can-not-deserialize-view-model-use-the-jsonconstructor-attribute/150/12 "2024-02-19T21:05:06Z")

</div>

UPDATE

if I remove `inboundParams`, the exception is the same, only that it now targets the next param in line: `userAlerts`.

PS:  
Same for `popupCache` (which is the only one with its type declared in the MarkupControl’s VM aka the popup aka `OperationInstance` aka instance of `Operation`)

PS2:

also tried (with no luck)  
 ![image](https://forum.dotvvm.com/uploads/default/original/1X/cea6bdae5d0651251a0e918c6df746aee04fd80e.png)

---

<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:** [February 19, 2024, 9:19pm UTC](https://forum.dotvvm.com/t/view-models-page-structuring-series-exception-can-not-deserialize-view-model-use-the-jsonconstructor-attribute/150/13 "2024-02-19T21:19:09Z")

</div>

So, DotVVM needs some way to get the values to pass into the constructor it needs to call. There are two options

1. It is a registered service (which it isn’t, it’s a view model)
2. It is a property from the received JSON (which it isn’t, we can’t serialize loops in JSON)

Options A: Some people do put all the root view models into DI as Scoped services, then you can get the root view model in any nested VM. This works fine (although I’m personally not a fan), but I’d advice agains registering all the nested view models as well - it would get tricky when you’d have more than one instance of it (will be a fun debugging session).

There are tools ([Scrutor](https://github.com/khellang/Scrutor)) to help with the service registration, which registers them based on some conventions (I’d use an empty marker interface or attribute, anything else is the same shit as [COME FROM](https://en.wikipedia.org/wiki/INTERCAL))

Option B is to remove these arguments from the JsonConstructor and fill them in using properties from the parent, in the `Load` method (`Load` of the parent if always called before the children). I’d **probably** do this

Option C is pre-initialize the object, so DotVVM doesn’t have to call the constructors. From the other context, I suppose this is problematic in your case. However, AFAIK, if DotVVM gets null from the client, it will replace your pre-initialized object with the null, so I it’s a possibility if the other options are bad for other reasons.

---

<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:** [February 19, 2024, 9:20pm UTC](https://forum.dotvvm.com/t/view-models-page-structuring-series-exception-can-not-deserialize-view-model-use-the-jsonconstructor-attribute/150/14 "2024-02-19T21:20:03Z")

</div>

> [@MarianV](#):
>
> also tried (with no luck)  
> ![image](https://forum.dotvvm.com/uploads/default/original/1X/cea6bdae5d0651251a0e918c6df746aee04fd80e.png)

This would work, if you had a property “InboundParams222” coming from the client.

---

<div class="post-metadata">

**Author:** ![MarianV](https://forum.dotvvm.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@MarianV](https://forum.dotvvm.com/u/MarianV)\
**Post date:** [February 19, 2024, 9:22pm UTC](https://forum.dotvvm.com/t/view-models-page-structuring-series-exception-can-not-deserialize-view-model-use-the-jsonconstructor-attribute/150/15 "2024-02-19T21:22:33Z")

</div>

what is “client” ? \_\_\_\_\_\_

LE: oh! “client-side”

---

<div class="post-metadata">

**Author:** ![MarianV](https://forum.dotvvm.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@MarianV](https://forum.dotvvm.com/u/MarianV)\
**Post date:** [February 19, 2024, 9:25pm UTC](https://forum.dotvvm.com/t/view-models-page-structuring-series-exception-can-not-deserialize-view-model-use-the-jsonconstructor-attribute/150/16 "2024-02-19T21:25:06Z")

</div>

> [@exyi](#):
>
> COME FROM

![image](https://forum.dotvvm.com/uploads/default/original/1X/e192fdd4666d5e2dbf5146fc5b4ae3560bc9315c.png)  
:)) dat language, such code

---

<div class="post-metadata">

**Author:** ![MarianV](https://forum.dotvvm.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@MarianV](https://forum.dotvvm.com/u/MarianV)\
**Post date:** [February 19, 2024, 9:37pm UTC](https://forum.dotvvm.com/t/view-models-page-structuring-series-exception-can-not-deserialize-view-model-use-the-jsonconstructor-attribute/150/17 "2024-02-19T21:37:37Z")

</div>

Option B seems the winner, what a dum-dum to not realize it :)) (…although I will have to think its scalability later)

---

<div class="post-metadata">

**Author:** ![MarianV](https://forum.dotvvm.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@MarianV](https://forum.dotvvm.com/u/MarianV)\
**Post date:** [February 20, 2024, 6:29pm UTC](https://forum.dotvvm.com/t/view-models-page-structuring-series-exception-can-not-deserialize-view-model-use-the-jsonconstructor-attribute/150/18 "2024-02-20T18:29:23Z")

</div>

> [@MarianV](#):
>
> and return with a confirmation…

@exyi  
Entire structure is working now, complete with validation.  
Thank you

---

<div class="post-metadata">

**Author:** ![MarianV](https://forum.dotvvm.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@MarianV](https://forum.dotvvm.com/u/MarianV)\
**Post date:** [February 21, 2024, 12:23am UTC](https://forum.dotvvm.com/t/view-models-page-structuring-series-exception-can-not-deserialize-view-model-use-the-jsonconstructor-attribute/150/19 "2024-02-21T00:23:51Z")

</div>

> [@MarianV](#):
>
> Option B seems the winner

My reasoning being: keep things meant to be local / encapsulated as such as much as possible, not spreading them out.
