Debugging with JSON.serializePretty()

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:

[code language=”javascript”]
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);

[/code]

Here’s what will show in the debug log:

[code language=”javascript”]

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

[/code]

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.

[code language=”javascript”]

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));

[/code]

Here’s the new output:

[code language=”javascript”]

USER_DEBUG|[7]|DEBUG|{

"attributes" : {

"type" : "Account"

},

"Name" : "Test Account",

"BillingStreet" : "123 Test Dr",

"BillingCity" : "Test City",

"BillingState" : "Test State",

"BillingPostalCode" : "12345-6789"

}

[/code]

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:

[code language=”javascript”]

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;
}
}
}

[/code]

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

[code lanugage=”javascript”]

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

[/code]

And here is the output with it:

[code language=”javascript”]

USER_DEBUG|[2]|DEBUG|{

"iac" : {

"s" : "Foo bar",

"d" : 0.5

},

"awesomeString" : "Hello World",

"awesomeNumber" : 123

}

[/code]

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:

[code language=”javascript”]

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

[/code]

Your Visualforce page should have the following tag:

[code language=”javascript”]

<apex:pageMessages escape="false"/>

[/code]

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!