I have a library I'm interfacing with which I have absolutely no control over. The objects are formatted with property names like this:
{"my property": "my value"}
and
{"my-property": "my value"}
Using json.net I can add an attribute to the properties in the class that represents each message to map those property names to a property in my class:
public class MyClass
{
[JsonProperty(PropertyName="my-prop")]
public String MyProperty {get; set;}
}
This is fine, but the issue is that I have to use these 'magic strings' on a large number of classes and I really despise magic strings.
The bottom line:
Is there any way to change all of these properties into a C# readable string so as to have json.net map those properties automatically?
i.e.:
{"my property": "my value"}
Should change to:
{"MyProperty": "my value"}
During deserialization, which would then flow that value to the MyProperty member in MyClass without annotating the property with a hard coded string value.