JamesNK/Newtonsoft.Json — pull request #375 by BrandonLWhite (2014-09-12 17:16:44+00:00)
Title: Implemented support for optional constructors in JsonConverters for passing in the MemberInfo object.

Hi,

This PR adds the capability of passing an attribute provider object instance to an optional initializing constructor in JsonConverters. This opens up possibilities for configuring custom JsonConverters.

The limitation I've run into on occasion when using a custom JsonConverter, is that I cannot configure or otherwise parameterize the behavior of my converter. This has led people to kludges such as creating multiple child classes of their JsonConverter for each combination of options they need.

This PR doesn't quite give you the ability to pass any arbitrary arguments to your JsonConverter instance constructor -- I've made no changes to the JsonConverterAttribute. Instead, I've added the ability to invoke your JsonConverter's initializing constructor, if defined, with the "attribute provider" object. For properties and fields, this will be a MemberInfo derived instance. For a class, this will be Type.

Armed with this additional context, JsonConverters can now choose to behave differently according to the member being serialized instead of being blind to the context in which they're being used. Primarily, the JsonConverter can look at any other Attributes defined for the member.

I've included two unit test: `MemberInfoPassedToJsonConvertConstructor` simply exercises the features and confirms that the JsonConverter got its initializing ctor invoked. `CustomDoubleRounding` is more of a demonstration that uses a practical application of this feature, including the use of a custom Attribute to control the behavior of the JsonConverter.

Thanks, and please let me know if you require any further changes for this PR to be suitable. I think this simple change could help out a lot of folks.