For Developers

June 8, 2016

Debugging with JSON.serializePretty()

John Pipkin

Printing debug statements in your code is essential, but it can be very painful when you don’t know what to print. Many times it’s best to see an entire class/SObject instance to be able to see all the properties in one place. Simply using System.debug() can be extremely hard to read, especially if what you are trying to print has a large amount of data.

Let’s take this piece of code:

Account a = new Account();
a.Name = 'Test Account';
a.BillingStreet = '123 Test Dr';
a.BillingCity = 'Test City';
a.BillingState = 'Test State';
a.BillingPostalCode = '12345-6789';
System.debug(a);

Here’s what will show in the debug log:


USER_DEBUG|[8]|DEBUG|Account:{Name=Test Account, BillingStreet=123 Test Dr, BillingCity=Test City, BillingState=Test State, BillingPostalCode=12345-6789}

Sure, It’s functional. It’s just not very readable. The JSON class has a method called “serializePretty” that takes a general Object as an input and returns a string. Whenever you use this method for system.debug(), it will put each property-value pair of your object on a new line. Now, let’s use JSON.serializePretty() to clean it up a bit.


Account a = new Account();
a.Name = 'Test Account';
a.BillingStreet = '123 Test Dr';
a.BillingCity = 'Test City';
a.BillingState = 'Test State';
a.BillingPostalCode = '12345-6789';
System.debug(JSON.serializePretty(a));

Here’s the new output:


USER_DEBUG|[7]|DEBUG|{

"attributes" : {

"type" : "Account"

},

"Name" : "Test Account",

"BillingStreet" : "123 Test Dr",

"BillingCity" : "Test City",

"BillingState" : "Test State",

"BillingPostalCode" : "12345-6789"

}

Having each property in a new line can be key whenever you have a wrapper class or any type of large object. Let’s take a look at this Apex class:


public class AwesomeClass {

public String awesomeString;
public Integer awesomeNumber;
public InnerAwesomeClass iac;

public AwesomeClass() {
this.awesomeString = 'Hello World';
this.awesomeNumber = 123;
this.iac = new InnerAwesomeClass('Foo bar',0.5);
}

public class InnerAwesomeClass{
public String s;
public Decimal d;

public InnerAwesomeClass(String str, Decimal dec){
this.s = str;
this.d = dec;
}
}
}

Here is the output to debug log without JSON.serializePretty():


USER_DEBUG|[2]|DEBUG|AwesomeClass:[awesomeNumber=123, awesomeString=Hello World, iac=InnerAwesomeClass:[d=0.5, s=Foo bar]]

And here is the output with it:


USER_DEBUG|[2]|DEBUG|{

"iac" : {

"s" : "Foo bar",

"d" : 0.5

},

"awesomeString" : "Hello World",

"awesomeNumber" : 123

}

Using this method has allowed me to quickly identify problems with very large class instances because I can quickly see all the property-value pairs.

This method has also been an integral part of my Visualforce development and debugging. If you create a PageMessage with the message as “JSON.serializePretty(this)” along with a replace method inside the controller, the page will give you a visual representation of the controller. For example:


ApexPages.addmessage(
new ApexPages.message(ApexPages.severity.INFO,
JSON.serializePretty(this).replaceAll('\\n','<br/>'));

Your Visualforce page should have the following tag:


<apex:pageMessages escape="false"/>

The result is that the pageMessage block will show each of the controller’s properties on separate lines, rather than jumbling them all together on the same line.

Enjoy and happy coding!

About the Author

John Pipkin

John is a Senior Developer at OpFocus. John specializes in Apex, Visualforce, integrations, jQuery, Visual Flows, and Lightning Processes, but particularly enjoys the power to customize pretty much anything on the Salesforce platform. When he is not working his magic on the Force.com platform, John enjoys creating musical magic, and plays the drums and guitar. Since he's a total rock star, John has 11+ Salesforce Certified designations. Go through this list if you want to feel immensely inadequate about your Salesforce skills... Administrator (201), Platform App Builder, Developer (401), Platform Developer I, Sales Cloud Consultant, Service Cloud Consultant, Community Cloud Consultant, Mobile Solution Architecture Designer, Integration Architecture Designer, Development Lifecycle & Deployment Designer, Identity & Access Management Designer, and System Architect certifications.

Related Posts