Home > Net >  How will the json object deserialization subclass?
How will the json object deserialization subclass?

Time:12-30

Sometimes encounter the following
?? 1. Define A type of A field, but in instantiation is used in its derived class B,
?? 2. Define A storage base type A collection, but in fact it contains all kinds of derived classes,
?? The above two cases in serialized when there is no problem, but when the deserialization process and cannot be transformed into a derived class,
Excuse me, how should do?

CodePudding user response:

Remember also met a, then use the Attribute to solve this, a back flip code...

CodePudding user response:

 
//belt type serialization deserialization
//string json=JsonConvert. SerializeObject (item ItemObject, new JsonSerializerSettings () {TypeNameHandling=TypeNameHandling. All}));
//var itemObject=JsonConvert. DeserializeObject (json, new JsonSerializerSettings () {TypeNameHandling=TypeNameHandling. All});

CodePudding user response:


I find a piece of code on the Internet, not only can solve the derived class serialization, also can solve the reference cycle, however, this code is for performance, a small string, and deserialization time will takes 3 seconds, so large coffee, please help to solve,

CodePudding user response:

Look at the method 2

https://blog.csdn.net/hanjun0612/article/details/59078428

CodePudding user response:

refer to 4th floor are nu month god reply:
look at the method 2

https://blog.csdn.net/hanjun0612/article/details/59078428

You this is to resolve the reference cycle? With it, can not be deserialized into a derived class

CodePudding user response:


JSON deserialize a string, takes 3 seconds, how to do it?
  •  Tags:  
  • C#
  • Related