JSONGenerator Class

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

System

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.

See Also

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

Closes the JSON generator.

public Void close()

Type: Void

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

Returns the generated JSON content.

public String getAsString()

Type: String

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

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

public Boolean isClosed()

Type: Boolean

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

public Void writeBlob(Blob blobValue)

  • blobValue: Type: Blob

Type: Void

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

public Void writeBlobField(String fieldName, Blob blobValue)

Type: Void

Writes the specified Boolean value.

public Void writeBoolean(Boolean blobValue)

Type: Void

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

public Void writeBooleanField(String fieldName, Boolean booleanValue)

Type: Void

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

public Void writeDate(Date dateValue)

  • dateValue: Type: Date

Type: Void

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.

public Void writeDateField(String fieldName, Date dateValue)

Type: Void

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

public Void writeDateTime(Datetime datetimeValue)

Type: Void

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.

public Void writeDateTimeField(String fieldName, Datetime datetimeValue)

Type: Void

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

public Void writeEndArray()

Type: Void

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

public Void writeEndObject()

Type: Void

Writes a field name.

public Void writeFieldName(String fieldName)

Type: Void

Writes the specified ID value.

public Void writeId(ID identifier)

  • identifier: Type: ID

Type: Void

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

public Void writeIdField(String fieldName, Id identifier)

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

Type: Void

Writes the JSON null literal value.

public Void writeNull()

Type: Void

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

public Void writeNullField(String fieldName)

Type: Void

Writes the specified decimal value.

public Void writeNumber(Decimal number)

Type: Void

Writes the specified double value.

public Void writeNumber(Double number)

Type: Void

Writes the specified integer value.

public Void writeNumber(Integer number)

Type: Void

Writes the specified long value.

public Void writeNumber(Long number)

  • number: Type: Long

Type: Void

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

public Void writeNumberField(String fieldName, Decimal number)

Type: Void

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

public Void writeNumberField(String fieldName, Double number)

Type: Void

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

public Void writeNumberField(String fieldName, Integer number)

Type: Void

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

public Void writeNumberField(String fieldName, Long number)

Type: Void

Writes the specified Apex object in JSON format.

public Void writeObject(Object anyObject)

  • anyObject: Type: Object

Type: Void

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

public Void writeObjectField(String fieldName, Object value)

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

Type: Void

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

public Void writeStartArray()

Type: Void

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

public Void writeStartObject()

Type: Void

Writes the specified string value.

public Void writeString(String stringValue)

Type: Void

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

public Void writeStringField(String fieldName, String stringValue)

Type: Void

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

public Void writeTime(Time timeValue)

  • timeValue: Type: Time

Type: Void

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

public Void writeTimeField(String fieldName, Time timeValue)

Type: Void