Contains methods for serializing Apex objects into JSON format and deserializing JSON content that was serialized using the serialize method in this class.
Use the methods in the System.JSON class to perform round-trip JSON serialization and deserialization of Apex objects.
The following are static methods of the System.JSON class.
| Method | Arguments | Return Type | Description |
|---|---|---|---|
| createGenerator | Boolean pretty | System.JSONGenerator | Returns a new JSON generator. The pretty argument determines whether the JSON generator creates JSON content in pretty-print format with the content indented. Set to true to create indented content. |
| createParser | String jsonString | System.JSONParser | Returns a new JSON parser. The jsonString argument is the JSON content to parse. |
| deserialize | String jsonString System.TypeapexType |
Any type | Deserializes the specified JSON string into an Apex object of the specified type. The jsonString argument is the JSON content to deserialize. The apexType argument is the Apex type of the object that this method creates after deserializing the JSON content. The following example deserializes a Decimal value. Decimal n = (Decimal)JSON.deserialize(
'100.1', Decimal.class);
System.assertEquals(n, 100.1); |
| serialize | Any type object | String | Serializes Apex objects into JSON content. The object argument is the Apex object to serialize. The following example serializes
a new Datetime value. Datetime dt = Datetime.newInstance(
Date.newInstance(
2011, 3, 22),
Time.newInstance(
1, 15, 18, 0));
String str = JSON.serialize(dt);
System.assertEquals(
'"2011-03-22T08:15:18.000Z"',
str); |
| serializePretty | Any type object | String | Serializes Apex objects into JSON content and generates indented content using
the pretty-print format. The object argument is the Apex object to serialize. |
public class JSONRoundTripSample { public class InvoiceStatement { Long invoiceNumber; Datetime statementDate; Decimal totalPrice; public InvoiceStatement(Long i, Datetime dt, Decimal price) { invoiceNumber = i; statementDate = dt; totalPrice = price; } } public static void SerializeRoundtrip() { Datetime dt = Datetime.now(); // Create a few invoices. InvoiceStatement inv1 = new InvoiceStatement(1,Datetime.valueOf(dt),1000); InvoiceStatement inv2 = new InvoiceStatement(2,Datetime.valueOf(dt),500); // Add the invoices to a list. List<InvoiceStatement> invoices = new List<InvoiceStatement>(); invoices.add(inv1); invoices.add(inv2); // Serialize the list of InvoiceStatement objects. String JSONString = JSON.serialize(invoices); System.debug('Serialized list of invoices into JSON format: ' + JSONString); // Deserialize the list of invoices from the JSON string. List<InvoiceStatement> deserializedInvoices = (List<InvoiceStatement>)JSON.deserialize(JSONString, List<InvoiceStatement>.class); System.assertEquals(invoices.size(), deserializedInvoices.size()); Integer i=0; for (InvoiceStatement deserializedInvoice :deserializedInvoices) { system.debug('Deserialized:' + deserializedInvoice.invoiceNumber + ',' + deserializedInvoice.statementDate.formatGmt('MM/dd/yyyy HH:mm:ss.SSS') + ', ' + deserializedInvoice.totalPrice); system.debug('Original:' + invoices[i].invoiceNumber + ',' + invoices[i].statementDate.formatGmt('MM/dd/yyyy HH:mm:ss.SSS') + ', ' + invoices[i].totalPrice); i++; } } }