This project has moved and is read-only. For the latest updates, please go here.

Intercepting Member Serialization

Aug 12, 2011 at 10:37 PM
Edited Aug 12, 2011 at 11: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?




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 9:30 PM
Edited Aug 16, 2011 at 12:27 AM

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 16, 2011 at 12:28 AM

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 16, 2011 at 12:51 AM
Edited Aug 16, 2011 at 12:57 AM

I've added a github fork with my changes here:

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.