1

Is there a way to set the JsonProperty attribute when we don't have access to the inherited class?

public abstract class NotModifiable {

   public Guid Id { get; protected set; }
}

public class Modifiable : NotModifiable {
   
   [JsonProperty("name")]
   public string Name {get; set;}


}

Basically if there is another way to achieve the same behaviour as:

[JsonProperty("id")]
public Guid Id { get; protected set; }

Without accessing it.

Thanks

LoyalPotato
  • 127
  • 1
  • 9
  • You can find the answer [Here](https://stackoverflow.com/questions/6550409/how-to-add-attributes-to-a-base-classs-properties) – Majid Qafouri Aug 04 '21 at 12:01
  • Use a [custom contract resolver](https://www.newtonsoft.com/json/help/html/ContractResolver.htm#CustomIContractResolverExamples). See e.g. [JSON.net ContractResolver vs. JsonConverter](https://stackoverflow.com/a/41094764/3744182), [Json.NET deserialize or serialize json string and map properties to different property names defined at runtime](https://stackoverflow.com/a/38112291/3744182), [Json.net on deserialize change property type and name](https://stackoverflow.com/q/58471909/3744182), ... – dbc Aug 04 '21 at 14:33
  • ... [How can I tell Json.NET to ignore properties in a 3rd-party object?](https://stackoverflow.com/q/25749509/3744182). Do those answer your question? – dbc Aug 04 '21 at 14:34
  • ... Looking further, [Deserializing public property with non-public setter in json.net](https://stackoverflow.com/q/28802288/3744182) looks to be a perfect duplicate for the requirement to deserialize a third-party type with a property that has a protected setter. – dbc Aug 04 '21 at 14:36
  • Incidentally, if you want camel case, make your contract resolver inherit from `DefaultContractResolver` and set `resolver.NamingStrategy = new CamelCaseNamingStrategy();`. Do **not** inherit from `CamelCasePropertyNamesContractResolver` for reasons explained in [Json.Net: Html Helper Method not regenerating](https://stackoverflow.com/a/30743234/3744182). – dbc Aug 04 '21 at 14:44

0 Answers0