5

I'm part of a team building an API wrapper in Apex. Our service responses use snake case, but we wanted to follow style conventions and use camel case for our Apex variables. If the names don't match, however, the properties won't get set correctly upon deserialization of the response.

Does anyone know of a way to specify a particular name to use for serialization? In Java, we used Gson's SerializedName annotation (https://google.github.io/gson/apidocs/com/google/gson/annotations/SerializedName.html). I wasn't able to find anything similar for Apex, though.

Logan Patiño
  • 91
  • 1
  • 10

2 Answers2

0

There is no standard method for doing this via the JSON methods in Apex. I'd suggest the cleanest approach would be to create custom metadata records mapping your Salesforce and external field names, and iterating through those to replace JSON entries.

0

I think the only way is to apply deserialize untype approach:

Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(jsonInput);

You can then implement in your wrapper classes constructor that can handle mapping between incoming attributes and ones in class.

  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 19 '23 at 19:44