0

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

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
LoyalPotato
  • 401
  • 4
  • 15
  • 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
  • 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