简体   繁体   中英

c# & json.NET - Call constructor BEFORE deserializing members

I just started working with Json.NET, and it looks like a great package! However, I'm coming across a strange issue. It serializes objects just the way I expect, and when loading, it USUALLY works. However, one of my objects is acting very strangely in that it FIRST tries to deserialize its members, and THEN calls the constructor. This causes problems, because the member objects rely on their owner being initialized before they can be created.

I have set a JsonConstructor attribute for the constructor I want, but I'm not sure how to avoid this strange problem. If you would like more specifics on what I'm trying to do, please see my post at CodePlex: http://json.codeplex.com/discussions/348577

I got around this by using C#'s built-in DataContractSerializer. This method actually doesn't call any initializer, but allows you to set up a method that is run when deserializing. More information here: http://msdn.microsoft.com/en-us/library/ms731073.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM