2

This question already has an answer here:

I have an APEX class that should have a property named "number" but since this is a restricted keyword in Salesforce, I'm not allowed to do that. I have found a solution where I should name it number1 and then make a .replace() on serialized JSON string. But this does not seem safe. Is there any safer way like annotating this property "number1" with value "number" so that it generates JSON element named "number"?

marked as duplicate by Adrian Larson apex Jan 22 at 14:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Unlike Spring Salesforce does not have annotation for that. You can serialize JSON Untyped and parse manually – Pranay Jaiswal Jan 22 at 12:17
0

Unlike Java, Apex does not support defining custom annotation types. You can only use a set of pre-defined annotations in Apex. Thus your options are limited here.

For the scenario you are into, where your JSON has a reserved keyword, the option you are looking at (replacing the attribute) is the one which works best here. I can tell you from my personal experiences that this is the best approach and is not unsafe. E.g., if you have a number attribute in your JSON and that you cannot deserialize it in Apex, the pre-requisite would be to replace the attribute say with myNumber or any other specific pattern you want to.

As long as you have a wrapper class written for your JSON with the aforementioned approach, I willrecommend to go that route. The other option is to individually parse the contents may prove to be more cumbersome only for such specific scenarios.

Not the answer you're looking for? Browse other questions tagged or ask your own question.