4

We use Java for backend programming and C# for app programming.

We offen exchange information with the server using json serialization framework such as NewtonJson(C#) and Gson(Java)

In the Java world, the programme guide are using camelCasing in which case the name of user should be userName but according to the C# guide they use PascalCasing when it should be UserName in C#.

So when using json deserialization and serialization on the app side or the server side, it will mess up.

So we are considering to change the app sides program convention to camelCase.

Or is there anyway else to solve this problem? Or what your team treats this issue?

Cœur
  • 37,241
  • 25
  • 195
  • 267
armnotstrong
  • 8,605
  • 16
  • 65
  • 130
  • According to my experience, you can have your C# code with dotnet naming convention (PascalCase for properties) but to expose a Web API you can configure your json serializer to return all properties names in camelCase; If it's bad or not will depend on your architect's point of view, so, In your company have design guidelines? – H. Herzl Jun 21 '17 at 03:48
  • 1
    @GhostCat thanks, my English is poor, glad to know that I could describe the problem out clear :D – armnotstrong Jun 21 '17 at 08:13

1 Answers1

2

There are multiple options here:

"Aliasing"

Any decent framework should allow you to use annotations to use a different name within the JSON representation and the actual field names!

Using gson for example:

public class Example {
  @SerializedName("lisp-style-name")
  private String someField.... 

In other words: make sure that all JSON "field names" follow such a convention - then you are independent of the underlying programming language.

The big downside is of course that have to annotate each field - maybe even on both sides. On the other hand, the above is really explicit - you understand what is supposed to happen just by looking into your "data" class declaration.

Custom naming support

Both libraries, gson and NewtonJson support policies. So instead of doing the mapping manually, you can instruct the framework to transform field names.

See here for how to do that with gson; for C#, you got that duplicated question.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • that other suggestion is pretty lame – GorvGoyl Jun 21 '17 at 04:02
  • 2
    this is ugly, Newtonsoft lets you set a policy for casing rather than manually try and spec it – Keith Nicholas Jun 21 '17 at 04:04
  • Writing good answers on the mobile isn't exactly easy; I have further enhanced my answer, and hope it is good enough now to at least undo the the downvote (not sure if that was you or @JerryGoyal) – GhostCat Jun 21 '17 at 06:17