Apex REST response with reserved words
Use JSON2Apex with the 'Create explicit parse code' option enabled. This will produce code like so:
//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//
public class MyList {
// ...boilerplate...
public class List_Z {
public String id {get;set;}
public List_Z(JSONParser parser) {
while (parser.nextToken() != JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != JSONToken.VALUE_NULL) {
if (text == 'id') {
id = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'List_Z consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public List<List_Z> list {get;set;}
public MyList(JSONParser parser) {
while (parser.nextToken() != JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != JSONToken.VALUE_NULL) {
if (text == 'list') {
list = new List<List_Z>();
while (parser.nextToken() != JSONToken.END_ARRAY) {
list.add(new List_Z(parser));
}
} else {
System.debug(LoggingLevel.WARN, 'Root consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
//...etc...
Just replace all occurrences of the list
member variable with list_z
(or whatever you like), and you're off to the races:
//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
// Hand-modified by @metadaddy :-)
//
public class MyList {
// ...boilerplate...
public class List_Z {
public String id {get;set;}
public List_Z(JSONParser parser) {
while (parser.nextToken() != JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != JSONToken.VALUE_NULL) {
if (text == 'id') {
id = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'List_Z consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public List<List_Z> list_z {get;set;}
public MyList(JSONParser parser) {
while (parser.nextToken() != JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != JSONToken.VALUE_NULL) {
if (text == 'list') {
list_z = new List<List_Z>();
while (parser.nextToken() != JSONToken.END_ARRAY) {
list_z.add(new List_Z(parser));
}
} else {
System.debug(LoggingLevel.WARN, 'Root consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
//...etc...
Note - you'll also need to move the test code into its own class. Superfell or I need to go back and fix that. We should also rename the member variable when it clashes with a reserved word.
Anyway - now you can create a MyList from JSON like so:
MyList l = MyList.parse(json);
Salesforce Apex and other languages like Java, C etc will give you errors when you try to use reserved words.
However as you said that this response is really required so here is what I suggest.
Rename the list to something unique example: list_replace_me
global List<DataWrapperClass> list_replace_me;
Instead of returning the wrapper from your GET method, return a string.
global static string getList() { //Manually serialize this object new ResponseWrapperClass(); }
Manually serialize the object by
JsonGenerator
class to get a string, or useJSON.serialize(inputobject)
which return a string.Replace "list_replace_me" with "list" using
String.replace
function.- Return the new String after replacing.
Json Generator