I am using Json.NET to serialize objects (commands) and send them via service bus. Some of the serialized objects gets too large to be send via service bus. I want to tag some properties with a custom attribute like TooLargeForServiceBusAttribute. Now I want to rename these properties in the serialized json and replace the value. The goal is to swap the large content of the property to an external store and add the id of the content in the external store to the serialized json string.
Example
class CommandWithTooLargeProperty { [TooLargeForServiceBus] public string SomeProperty { get; set; } } I want the serialized json to be as follows:
{ SomeProperty_EXTERNAL_STORE_ID = '10000000-2000-3000-4000-500000000000' } How can I hook into the serialization process of Json.NET to get what I want? I cannot use a custom converter because some of my command classes are already decorated with a custom converter and my described mechanism has to be transparent for every class I am serializing.
My first though was to write a class inheriting from JsonTextWriter to rename the property but I do not want to rename every property but only the properties which are decorated with TooLargeForServiceBusAttribute and the JsonTextWriter I do not have access to the property of the source object.
And I have to inject something like IExternalStore into the serialization pipeline to save the content of the swapped properties to an external store.
The deserialization process has to do the reverse work of the serialization: taking the id from SomeProperty_EXTERNAL_STORE_ID, loading the content from IExternalStore and setting the loaded value to the property of the deserialized object.
{"SomeProperty" : { "$External_Store_Id" : "10000000-2000-3000-4000-500000000000" } }