6

I'm working on documentation for a number of APEX REST API endpoints which returned user-defined types. I'm struggling to write the documentation since the order of how things are serialized into JSON seems to change every time I compile the class.

The JSON docs and google aren't provided much insight. Has anyone figured out how Salesforce serializes user-defined types?

As an example, given the following response type

public class Example {
  public String foo { get; private set; }
  public String bar { get; private set; }
}

How do I know if it'll serialize to

{"foo": "value", "bar": "value"}

Or

{"bar": "value", "foo": "value"}

While the above example doesn't really make much a difference several endpoints have very large response types and ideally I'd like to be able to have the docs match whatever Salesforce sends back.

2

This critical update may make your question a little easier to work around. If you choose to enable it there isn't a defined order for iterating maps, but whatever that order is it will be consistent. So while you can't really predict what the json output will be you can just look at what it is, and know that it will continue to come that way.

0

This is your class

public class Example {
  public String foo { get;set; }
  public String bar { get;set; }
}

Step 1 - Create the instance of the class and populate the values as below :

Example ex = new Example();
ex.foo = 'This is my JSON Serializer';
ex.bar = 'Just using JSON Helper Methods';

Step 2 : invoke the static JSON Helper methods as below passing the instance of class you created:

String formattedJSON = **JSON.serialize**(ex);
System.debug('formattedJSON-->>>'+formattedJSON);

Sep 3 : Get the result :)

08:11:56:083 USER_DEBUG [6]|DEBUG|formattedJSON-->>>{"foo":"This is my JSON Serializer","bar":"Just using JSON Helper Methods"}

  • You might mention that the order (or even presence) of the values is version dependent. For example, last time I tried serializing, everything came out in order, and trying it just now resulted in it coming out in declaration order. – sfdcfox Mar 5 '15 at 2:54
  • @sfdcfox that's the rub, it seems to change all the time. Don't really care so much about the order, just wish it would be consistent ... – Ralph Callaway Mar 5 '15 at 3:41
  • @kaw this is kind of circular definition, I can test the endpoint and see the serialization, the difficulty is that it's not consistent and doesn't appear to have any predictable order (i.e. alphabetical, declaration order, etc.) – Ralph Callaway Mar 5 '15 at 3:42
  • i would believe that should not matter much given that json implementation also internally makes use of map kind of a structure and sequence would always vary in case of map implementation. This would be true for every kind of language where map is being used. – Kaw Sumit Mar 5 '15 at 3:47
  • i would believe that should not matter much given that json implementation also internally makes use of map kind of a structure and sequence would always vary in case of map implementation. This would be true for every kind of language where map is being used. – Kaw Sumit Mar 5 '15 at 3:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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