JSONGenerator Class

Contains methods used to serialize objects into JSON content using the standard JSON encoding.

Namespace

System

Usage

The System.JSONGenerator class is provided to enable the generation of standard JSON-encoded content and gives you more control on the structure of the JSON output.

JSONGenerator Methods

The following are methods for JSONGenerator. All are instance methods.

close()

Closes the JSON generator.

Signature

public Void close()

Return Value

Type: Void

Usage

No more content can be written after the JSON generator is closed.

getAsString()

Returns the generated JSON content.

Signature

public String getAsString()

Return Value

Type: String

Usage

This method closes the JSON generator if it isn't closed already.

isClosed()

Returns true if the JSON generator is closed; otherwise, returns false.

Signature

public Boolean isClosed()

Return Value

Type: Boolean

writeBlob(blobValue)

Writes the specified Blob value as a base64-encoded string.

Signature

public Void writeBlob(Blob blobValue)

Parameters

  • blobValue: Type: Blob

Return Value

Type: Void

writeBlobField(fieldName, blobValue)

Writes a field name and value pair using the specified field name and BLOB value.

Signature

public Void writeBlobField(String fieldName, Blob blobValue)

Parameters

Return Value

Type: Void

writeBoolean(blobValue)

Writes the specified Boolean value.

Signature

public Void writeBoolean(Boolean blobValue)

Parameters

Return Value

Type: Void

writeBooleanField(fieldName, booleanValue)

Writes a field name and value pair using the specified field name and Boolean value.

Signature

public Void writeBooleanField(String fieldName, Boolean booleanValue)

Parameters

Return Value

Type: Void

writeDate(dateValue)

Writes the specified date value in the ISO-8601 format.

Signature

public Void writeDate(Date dateValue)

Parameters

  • dateValue: Type: Date

Return Value

Type: Void

writeDateField(fieldName, dateValue)

Writes a field name and value pair using the specified field name and date value. The date value is written in the ISO-8601 format.

Signature

public Void writeDateField(String fieldName, Date dateValue)

Parameters

Return Value

Type: Void

writeDateTime(datetimeValue)

Writes the specified date and time value in the ISO-8601 format.

Signature

public Void writeDateTime(Datetime datetimeValue)

Parameters

Return Value

Type: Void

writeDateTimeField(fieldName, datetimeValue)

Writes a field name and value pair using the specified field name and date and time value. The date and time value is written in the ISO-8601 format.

Signature

public Void writeDateTimeField(String fieldName, Datetime datetimeValue)

Parameters

Return Value

Type: Void

writeEndArray()

Writes the ending marker of a JSON array (']').

Signature

public Void writeEndArray()

Return Value

Type: Void

writeEndObject()

Writes the ending marker of a JSON object ('}').

Signature

public Void writeEndObject()

Return Value

Type: Void

writeFieldName(fieldName)

Writes a field name.

Signature

public Void writeFieldName(String fieldName)

Parameters

Return Value

Type: Void

writeId(identifier)

Writes the specified ID value.

Signature

public Void writeId(ID identifier)

Parameters

  • identifier: Type: ID

Return Value

Type: Void

writeIdField(fieldName, identifier)

Writes a field name and value pair using the specified field name and identifier value.

Signature

public Void writeIdField(String fieldName, Id identifier)

Parameters

  • fieldName: Type: String
  • identifier: Type: ID

Return Value

Type: Void

writeNull()

Writes the JSON null literal value.

Signature

public Void writeNull()

Return Value

Type: Void

writeNullField(fieldName)

Writes a field name and value pair using the specified field name and the JSON null literal value.

Signature

public Void writeNullField(String fieldName)

Parameters

Return Value

Type: Void

writeNumber(number)

Writes the specified decimal value.

Signature

public Void writeNumber(Decimal number)

Parameters

Return Value

Type: Void

writeNumber(number)

Writes the specified double value.

Signature

public Void writeNumber(Double number)

Parameters

Return Value

Type: Void

writeNumber(number)

Writes the specified integer value.

Signature

public Void writeNumber(Integer number)

Parameters

Return Value

Type: Void

writeNumber(number)

Writes the specified long value.

Signature

public Void writeNumber(Long number)

Parameters

  • number: Type: Long

Return Value

Type: Void

writeNumberField(fieldName, number)

Writes a field name and value pair using the specified field name and decimal value.

Signature

public Void writeNumberField(String fieldName, Decimal number)

Parameters

Return Value

Type: Void

writeNumberField(fieldName, number)

Writes a field name and value pair using the specified field name and double value.

Signature

public Void writeNumberField(String fieldName, Double number)

Parameters

Return Value

Type: Void

writeNumberField(fieldName, number)

Writes a field name and value pair using the specified field name and integer value.

Signature

public Void writeNumberField(String fieldName, Integer number)

Parameters

Return Value

Type: Void

writeNumberField(fieldName, number)

Writes a field name and value pair using the specified field name and long value.

Signature

public Void writeNumberField(String fieldName, Long number)

Parameters

Return Value

Type: Void

writeObject(anyObject)

Writes the specified Apex object in JSON format.

Signature

public Void writeObject(Object anyObject)

Parameters

  • anyObject: Type: Object

Return Value

Type: Void

writeObjectField(fieldName, value)

Writes a field name and value pair using the specified field name and Apex object.

Signature

public Void writeObjectField(String fieldName, Object value)

Parameters

  • fieldName: Type: String
  • value: Type: Object

Return Value

Type: Void

writeStartArray()

Writes the starting marker of a JSON array ('[').

Signature

public Void writeStartArray()

Return Value

Type: Void

writeStartObject()

Writes the starting marker of a JSON object ('{').

Signature

public Void writeStartObject()

Return Value

Type: Void

writeString(stringValue)

Writes the specified string value.

Signature

public Void writeString(String stringValue)

Parameters

Return Value

Type: Void

writeStringField(fieldName, stringValue)

Writes a field name and value pair using the specified field name and string value.

Signature

public Void writeStringField(String fieldName, String stringValue)

Parameters

Return Value

Type: Void

writeTime(timeValue)

Writes the specified time value in the ISO-8601 format.

Signature

public Void writeTime(Time timeValue)

Parameters

  • timeValue: Type: Time

Return Value

Type: Void

writeTimeField(fieldName, timeValue)

Writes a field name and value pair using the specified field name and time value in the ISO-8601 format.

Signature

public Void writeTimeField(String fieldName, Time timeValue)

Parameters

Return Value

Type: Void