function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
alexbalexb 

Apex REST - returning Object v JSON.serialize

In Apex REST, regarding returning values, what's the difference between

     [return leadList;]

and

     [return JSON.serialize(leadList);]?

 

I'm seeing they serialize differently.

The former returns:

     [{City"Fargo", FirstName"Test", LastName"McTesty", State"ND"}]

The latter returns:

     [{"attributes":{"type":"Lead"},"State":"ND","FirstName":"Test","LastName":"McTesty","City":"Fargo"}]

 

The docs say: "An Apex method with a non-void return type will have the return value serialized into RestResponse.responseBody.", but what does this mean? What method of serialization is used?

 

So, it seems that if we use the JSON serializer, we'll get some extra information beyond just the fields I want to send. Why does the JSON serializer add the extra 'attributes' property? It seems this extra complexity means that we shouldn't use the JSON serializer for REST responses, sound right? My Javascript framework seems to be choking on the latter.

 

Or a better question: What serialization technique is used by the former, and why is it different from the latter?

 

*edit*

After testing a bit more with custom objects, not sObjects, it seems that the former method will resolve easily to JS Object by a JS client, while the latter method will resolve to a JSON string. 

Henry AkpalaHenry Akpala

If you are returning a salesforce object you don't need to serialize it because the system will do it for you. If you try to serialize it yourself, you get all the attributes that is present in an object but no necessary usefull to the system that is call your service.  If you are returning just strings or custom class object then you need to serialize.

 

Regards

-H

pramod8585pramod8585

hi ,

      I got a json object  {"iD":"","Code":"","userID":"","pwd":"","errDesc":"","Status":"","errNumber":0}.in the request it is taking id and code as  required field and in response we get the userid it is randomly generated.

i got no idea about this json object can u help me in this regard

thank you;

pramod

Henry AkpalaHenry Akpala

You need to create a new post, and clearly explain what the issue is;

Are you recieving a rest request?

alexbalexb

Not a serious issue, just looking for an explanation on why these produce different serialization results. This difference isn't documented anywhere, so I thought it would be helpful to raise awareness.

Ken KoellnerKen Koellner
I'm looking at the same behavior (and might consider starting another thread).  I tried two signatures--
  @HttpGet global static String doGet()
and
  @HttpGet global static List<Account> doGet()
and tested both using workbench.developerforce.com REST utility.

What I found was the JSON was identical except in the String example, all the quotes were escaped.  That's because SF things it's a String object and serializes it as such.  So the result is pretty useless when I serialize using the JSON.serialize method.

With serialize, the JSON looks like --
"[{\"attributes\":{\"type\":\"Account\",\"url\":\"/services/data/v29.0/sobjects/Account/001c000000VlbnZAAR\"},\"Name\":\"Thi is an Account Name\",\"Id\":\"001c000000VlbnZAAR\"},  rest of elements not shown

When return is a List<Account> JSON looks like --
[ { "attributes" : { "type" : "Account", "url" : "/services/data/v29.0/sobjects/Account/001c000000VlbnZAAR" }, "Name" : "This is an Account Name", "Id" : "001c000000VlbnZAAR" },

Michal KaparMichal Kapar
RestContext.response.addHeader('Content-Type', 'application/json');
RestContext.response.responseBody = Blob.valueOf('{ "s" : "Hello", "i" : 1234 }');

viz. http://salesforce.stackexchange.com/questions/8751/how-can-i-return-json-object-from-rest-service