dotvvm icon indicating copy to clipboard operation
dotvvm copied to clipboard

Json Serialization - Ingore property if it's circular reference

Open MSRiganti opened this issue 9 years ago • 5 comments

Add into DotVVM Framework an extension, attribute (or anything else) which forces the use of Newtonsoft.Json.PreserveReferencesHandling setting. Specifies reference handling options for the JsonSerializer. https://www.newtonsoft.com/json/help/html/PreserveReferencesHandlingObject.htm

What about attribute?

[ThisViewModelPreserveReferenceWithSetting( enum Newtonsoft.Json.PreserveReferencesHandling )]
public class DefaultViewModel
{
    public ObjectWithCircularReferences MyObject { get; set; }
...
public class DefaultViewModel
{
    [ThisPropertyPreserveReferenceWithSetting( enum Newtonsoft.Json.PreserveReferencesHandling )]
    public ObjectWithCircularReferences MyObject { get; set; }
...

Thank you.

A question that sometimes drives me hazy: am I or are the others crazy? A. E.

MSRiganti avatar Sep 19 '17 10:09 MSRiganti

Unfortunately, I don't think it's possible to implement the reference handling correctly on the client side. We could either ignore it and simply send the $ref fields back to the server on postbacks, but it will break all value bindings using the object replaced by $ref field, break all staticCommands using the object and it can't be checked when the bindings are compiled. Another possibility would be to implement support for that in the client-side serializer and unwrap the $refed objects, but having the same objects multiple times in ViewModel is not at all good idea because it often completely breaks the page on further serializations/deserializations and leads to unexpected data bindings. And I don't want to implement it.

And with the VDOM update, the client side ViewModel architecture will be very different, it will basically support shared instances in the viewModel, but will never mutate them, which is different behavior from the server-side architecture and will also lead to unexpected behavior.

exyi avatar Sep 20 '17 11:09 exyi

Replace All ' $ref ' objects as a null objects. Use it (Allow it) just for properties [Bind(Direction.ServerToClientFirstRequest)]. This data can be sent to the client and can be displayed/deserialized as is.

MSRiganti avatar Sep 20 '17 11:09 MSRiganti

That means that you couldn't send the data back to server nor display it on the client, what would be the use case? Do I understand you correctly? This would only lead to unexpected behavior.

exyi avatar Sep 20 '17 17:09 exyi

If you don 't want, no one will change it. :-) But it is possible to implement sending data with circular reference to the client. The Data is serializable.

Developers! Developers! Developers! S. B.

MSRiganti avatar Sep 21 '17 06:09 MSRiganti

I think it would be fine to have an attribute, that would instruct the serializer to ignore this property it's a backreference - it would basically behave like [Bind(None)] but iff the object is already present in its ancestors.

exyi avatar Sep 25 '17 12:09 exyi