2

Consider there is a class like

public class Test{
     [JsonProperty("name")]
     public string Name {get; set;}
     [JsonProperty("age")]
     public string Age {get; set;}
}

I want this to be serialized like this: {"name": "a", "age": "10"} There is a setting CamelCaseNamingStrategy in Json.net library. If I dont use the JsonProperty I can still achieve the above result. The example given above is a small, but what if we have to serialize/deserialize bigger models frequently. Will adding the JsonProperty attribute for each property be a good thing for performance?

Thanks in advance.. :)

Shekhar
  • 416
  • 2
  • 5
  • 13
  • Please read https://ericlippert.com/2012/12/17/performance-rant/. – dbc Jan 31 '18 at 10:27
  • After you're done, if you want my *guess*, it's that any performance overhead of adding some extra attributes vs using a naming strategy is de minimis compared to the benefits code clarity, readability, and simplicity. Structure your code in the simplest possible way, then profile it if a problem arises. I highly doubt the presence of absence of attributes will make a difference since [contract information is cached](https://stackoverflow.com/q/33557737/3744182). – dbc Jan 31 '18 at 10:32
  • And for simplicity I prefer using a contract resolver, then a [type-level naming strategy](https://stackoverflow.com/a/44805815/3744182), and then lastly individual `[JsonProperty]` attributes. But sometimes your framework doesn't give you easy access to the contract resolver so using attributes is simpler. – dbc Jan 31 '18 at 10:32
  • 2
    Most serialization tools have very good strategy cache implementations for dealing with things like this; I wouldn't *expect* it to make much difference, but: measure it! The only meaningful test is one that matches your scenario and data. – Marc Gravell Jan 31 '18 at 10:49

0 Answers0