2

Many RESTful services provide responses that use a naming style/convention that doesn't align with what most of us use in our class and member names (lower case with underscore vs camel case, etc).

Often times that are a lot of fields returned that are unnecessary. Creating objects to directly deserialize into doesn't always make sense, nor would it provide compliance from a style/convention perspective.

In these cases, what do you do? What approach do you take to ensure conformity? I was thinking of creating classes with the members I need using the style/convention I wanted, and having a factory that handled deserialization into a dictionary and extracting/assigning the values I need, but I'm betting the community here has a plethora of better approaches.

joelc
  • 2,687
  • 5
  • 40
  • 60
  • The JSON library will probably have a way to do it automatically, for example: https://stackoverflow.com/questions/19445730/how-can-i-return-camelcase-json-serialized-by-json-net-from-asp-net-mvc-controll – Evan Trimboli Nov 01 '17 at 22:25
  • 2
    Also this: https://stackoverflow.com/q/15915503/809357 – trailmax Nov 01 '17 at 23:36
  • 1
    Well you can design a class to deserialize to and omit any fields you don't want. Furthermore you can name your properties whatever you want and use aliases to map the json's name to yours. See here [link](https://stackoverflow.com/questions/15915503/net-newtonsoft-json-deserialize-map-to-a-different-property-name) – Sean O'Neil Nov 01 '17 at 23:38
  • 1
    If you are using [tag:json.net], you can select or create [naming strategies](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Serialization_NamingStrategy.htm) that specify rules to map names from your conventions to those of your service providers. See e.g. [How to apply a general rule for remapping all property names when serializing with Json.NET?](https://stackoverflow.com/q/46476903/3744182), [C# JSON.NET convention that follows Ruby property naming conventions?](https://stackoverflow.com/a/3938546) or https://www.newtonsoft.com/json/help/html/NamingStrategyCamelCase.htm – dbc Nov 02 '17 at 19:38

0 Answers0