Newer Version Available
Roundtrip Serialization and Deserialization
Using the JSON class methods, you can perform roundtrip serialization and deserialization
of your JSON content.
The JSON class contains methods that enable you to serialize objects into JSON formatted strings. It also contains methods to deserialize JSON strings back into objects.
Sample: Serializing and Deserializing a List of Invoices
This sample creates a list of InvoiceStatement objects and serializes the list. Next, the serialized JSON string
is used to deserialize the list again and the sample verifies that
the new list contains the same invoices that were present in the original
list.
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class JSONRoundTripSample {
18
19 public class InvoiceStatement {
20 Long invoiceNumber;
21 Datetime statementDate;
22 Decimal totalPrice;
23
24 public InvoiceStatement(Long i, Datetime dt, Decimal price)
25 {
26 invoiceNumber = i;
27 statementDate = dt;
28 totalPrice = price;
29 }
30 }
31
32 public static void SerializeRoundtrip() {
33 Datetime dt = Datetime.now();
34 // Create a few invoices.
35 InvoiceStatement inv1 = new InvoiceStatement(1,Datetime.valueOf(dt),1000);
36 InvoiceStatement inv2 = new InvoiceStatement(2,Datetime.valueOf(dt),500);
37 // Add the invoices to a list.
38 List<InvoiceStatement> invoices = new List<InvoiceStatement>();
39 invoices.add(inv1);
40 invoices.add(inv2);
41
42 // Serialize the list of InvoiceStatement objects.
43 String JSONString = JSON.serialize(invoices);
44 System.debug('Serialized list of invoices into JSON format: ' + JSONString);
45
46 // Deserialize the list of invoices from the JSON string.
47 List<InvoiceStatement> deserializedInvoices =
48 (List<InvoiceStatement>)JSON.deserialize(JSONString, List<InvoiceStatement>.class);
49 System.assertEquals(invoices.size(), deserializedInvoices.size());
50 Integer i=0;
51 for (InvoiceStatement deserializedInvoice :deserializedInvoices) {
52 system.debug('Deserialized:' + deserializedInvoice.invoiceNumber + ','
53 + deserializedInvoice.statementDate.formatGmt('MM/dd/yyyy HH:mm:ss.SSS')
54 + ', ' + deserializedInvoice.totalPrice);
55 system.debug('Original:' + invoices[i].invoiceNumber + ','
56 + invoices[i].statementDate.formatGmt('MM/dd/yyyy HH:mm:ss.SSS')
57 + ', ' + invoices[i].totalPrice);
58 i++;
59 }
60 }
61}JSON Serialization Considerations
The following describes differences in behavior for the serialize method. Those differences depend on the Salesforce API version of the Apex code saved.
- Serialization of queried sObject with additional fields set
- For Apex saved using Salesforce API version 27.0 and earlier, if queried sObjects have additional fields set, these fields aren’t included in the serialized JSON string returned by the serialize method. Starting with Apex saved using Salesforce API version 28.0, the additional fields are included in the serialized JSON string.
- This example adds a field to a contact after it has been queried,
and then serializes the contact. The assertion statement verifies
that the JSON string contains the additional field. This assertion
passes for Apex saved using Salesforce API version 28.0 and later.
1Contact con = [SELECT Id, LastName, AccountId FROM Contact LIMIT 1]; 2// Set additional field 3con.FirstName = 'Joe'; 4String jsonstring = Json.serialize(con); 5System.debug(jsonstring); 6System.assert(jsonstring.contains('Joe') == true); - Serialization of aggregate query result fields
- For Apex saved using Salesforce API version 27.0, results of aggregate queries don’t include the fields in the SELECT statement when serialized using the serialize method. For earlier API versions or for API version 28.0 and later, serialized aggregate query results include all fields in the SELECT statement.
- This is an example of an aggregate query that returns two fields,
the count of ID fields and the account name.
1String jsonString = JSON.serialize( 2 Database.query('SELECT Count(Id),Account.Name FROM Contact WHERE Account.Name != null GROUP BY Account.Name LIMIT 1')); 3 System.debug(jsonString); 4 5// Expected output in API v 26 and earlier or v28 and later 6// [{"attributes":{"type":"AggregateResult"},"expr0":2,"Name":"acct1"}] - Serialization of empty fields
- Starting with API version 28.0, null fields aren’t serialized and aren’t included in the
JSON string, unlike in earlier versions. This change doesn’t affect
deserializing JSON strings with JSON methods, such as deserialize(jsonString, apexType). This change is
noticeable when you inspect the JSON string. For
example:
1String jsonString = JSON.serialize( 2 [SELECT Id, Name, Website FROM Account WHERE Website = null LIMIT 1]); 3System.debug(jsonString); 4 5// In v27.0 and earlier, the string includes the null field and looks like the following. 6// {"attributes":{...},"Id":"001D000000Jsm0WIAR","Name":"Acme","Website":null} 7 8// In v28.0 and later, the string doesn’t include the null field and looks like 9// the following. 10// {"attributes":{...},"Name":"Acme","Id":"001D000000Jsm0WIAR"}}