0

I'm trying to perform some JSONConvert serializing, and I've ran into a brick wall. All my relevant objects are subclasses from a certain class, say class Super. A subclass of Super can have other Super-inherited members in the class, i.e.

public abstract class Super{}

public class Foo : Super{}

public class Bar : Super {
   public Foo foo;
}

My goal is that, when this is serialized, a custom JsonConverter is ran for all these members in order to prepare them individually for serialization,i.e. remove unnecessary data depending on the data inside the object. I've tried using the [JsonConverter] tag on the abstract class, which causes StackOverflowException.

Whenever I try to use a custom serializer it only runs for the parent object and none of the child attributes, so it doesn't allow,in this case, the 'foo' variable the possibility to be processed for serialization. It shouldn't run for non-Super properties either.

Is such a converter possible for JsonConvert? I've been looking around a lot, but haven't found anything like this.

Thanks.

  • Why would an abstract animal have concrete Dogs and Cats in it (and Dogs inside the Cats too)? – Ňɏssa Pøngjǣrdenlarp Sep 18 '18 at 23:26
  • Fair point, updated it to be more abstract. – Albert Sjlund Vliro Sep 18 '18 at 23:30
  • You can do tricks like the one shown [here](https://stackoverflow.com/a/29720068) but it's not ideal. Basically a `JsonConverter` is intended to completely manually serialize or deserialize a type, but you want it to tweak an in-progress (de)serialization. Can you give more details? Are you using `TypeNameHandling` or `PreserveObjectReferences`? – dbc Sep 18 '18 at 23:45
  • I don't really know about either of those, but I'll look into it. In order to avoid unnecessary data, if a node exists in database I only want to include a reference to it(ID), not the actual data which means during serialization I have to process all members. – Albert Sjlund Vliro Sep 19 '18 at 05:23

0 Answers0