1

I'm implementing a 3rd parties RestFul API. The majority of the Json properties in the request and response data is formatted with underscore characters instead of spaces and all lower case. For example

{
"message_id" : "QWERTY",
"other_reference" : 12345
}

I am not a fan of this format in my internal code, I would prefer my model to be the following.

public class data
{
string MessageId {get; set;}

int OtherReference {get; set;}
}

Is there an easy way to get the serialiser/de-serialiser to do the mapping for me (using System.Text.Json) ? I know I can use the attribute JsonPropertyName to do it, but that means adding the attribute to all properties (there are a lot of them), although this method does allow me to use more meaningful names in my code (some of the names in the Json could be a little confusing to someone visiting the code at a later date if they don't have the subject knowledge)

Thanks

Ian Boggs
  • 522
  • 3
  • 16
  • 1
    Camel Casing is done automatically, and snake casing isn't that different. See `SnakeCaseNamingPolicy` from [this answer](https://stackoverflow.com/a/58576400/3744182) by Muhammad Hannan to [Is there a built in way of using snake case as the naming policy for JSON in ASP.NET Core 3?](https://stackoverflow.com/q/58570189/3744182). That should work correctly say 95% of the time, but if you have property names for which it does something incorrect (e.g. inserts too many underscores into acronyms) you can add `[JsonPropertyName("the_correct_name")]` for those few. – dbc Jan 05 '23 at 16:01
  • In fact that may be a duplicate, agree? – dbc Jan 05 '23 at 16:02
  • Thanks for that. I didn't know it was called snake casing to be honest. It's good to know that is an option for me. I'm still left with the issue of the names used by the API may not mean anything to anyone else who looks at the code, but that is a separate issue – Ian Boggs Jan 06 '23 at 11:24

1 Answers1

1

Coming from Java, where you'd use some sort of annotation (JSON-B / Jackson), you'd do the same in C# with the JsonPropertyName you already mentioned.

In my opinion, that's also your best option here since you cannot write a decent algorithm to insert the underscores, that would require that algorithm to somewhat understand the language used and therefore be utterly overkill, prone to errors and not even easy to develop.

My recommendation therefore would be not to fiddle around with the actual properties, rather introduce another class and method to parse the third party's representation into yours if you need it that badly, other than that, JsonPropertyName is probably really the best option.

See: How to customize property names and values with System.Text.Json for further information.

maio290
  • 6,440
  • 1
  • 21
  • 38
  • Yeah I suspected it was the best method but i was wondering if there was a lazy way to do it. As you said, the logic of what to split and replace might be a nightmare with some of the properties. – Ian Boggs Jan 05 '23 at 12:09