Intercepting Member Serialization

Aug 12, 2011 at 9:37 PM
Edited Aug 12, 2011 at 10:15 PM

First, I want to thank you for making this.  I've been using it for about four months now and it's a fantastic tool.

However, with my latest feature addition I've hit a bit of a snag.  I would like to intercept member serialization and inspect the propertyinfo for a particular custom attribute.  If this does not exist I'd like serialization/deserialization to continue as normal, but if it does I'd like to take control over what is written out.  Really, what I'd love is just a JsonConverter which I could somehow inform of the existence of this attribute, but that seems rather unlikely :).

It seems like the best way to do this would be to make a custom IContractResolver/JsonObjectContract pair.  Is there a better way to go about this?

Thanks!
@Rickasaurus 

 

Edit:

I'm having trouble with figuring out how to correctly resolve a given PropertyInfo with a JsonProperty.  There are several layers of indirection on PropertyName, which is the key in the JsonPropertyCollection.  An easy way out here would be really helpful.  After that I'm thinking I can remove the properties with the attribute from the contract on the fly and stick in replacements that do what I want.

Aug 15, 2011 at 8:30 PM
Edited Aug 15, 2011 at 11:27 PM

Working on adding an attributes list to JsonProperty and an extra "JsonProperty" parameter to canconvert, readjson, and writejson for JsonConverter. I think this will make things easy. 

Aug 15, 2011 at 11:28 PM

It wasn't easy to push JsonProperty into all of the right places, but it's in now and works great.  Now I can do what I want by simply using a JsonConverter.

Aug 15, 2011 at 11:51 PM
Edited Aug 15, 2011 at 11:57 PM

I've added a github fork with my changes here: https://github.com/Rickasaurus/Newtonsoft.Json-Fork

It's not what I would call high enough quality for including in this project.  I didn't fix the comments and some of the method signatures could have fewer parameters.  It works well for what I needed though.