Skip to content

DTO_JsonBase ​

Class · Group: Web Services

Learn it: DTOs guide

apex
@JsonAccess(serializable='always' deserializable='always') global inherited sharing virtual class DTO_JsonBase extends DTO_Base

Extends: DTO_Base

Known Derived Types: DTO_BaseTable, DTO_NameValues, DTO_ScheduledParameterDefinition

A Data Transfer Object (DTO) base class for JSON serialization and deserialization, providing a framework for transforming JSON data into structured objects and vice versa. Inherited by other DTO classes that require JSON handling capabilities.

Example

apex
@JsonAccess(serializable='always' deserializable='always')
public class DTO_Invoice extends DTO_JsonBase
{
    public String invoiceId;
    public String status;
    public override Type getObjectType()
    {
        return DTO_Invoice.class;
    }
}

Methods ​

MethodDescription
global override virtual DTO_Base deserialize(String dtoString)Deserializes a JSON string into an instance of the current DTO class.
global virtual Type getObjectType()Returns the type of the current DTO.
global override virtual String serialize()Serializes the current DTO instance into a JSON string, omitting null values to create a compact JSON representation.

deserialize ​

apex
global override virtual DTO_Base deserialize(String dtoString)

Deserializes a JSON string into an instance of the current DTO class. Uses the getObjectType method to identify the appropriate class for deserialization. The returned instance is populated with values from the provided JSON string.

Parameters

ParameterTypeDescription
dtoStringStringA JSON string representing the serialized data for the DTO.

Returns DTO_Base — A deserialized DTO instance populated with values from the JSON string.

Example

apex
DTO_Base result = instance.deserialize('value');

See Also DTO_JsonBase.getObjectType

getObjectType ​

apex
global virtual Type getObjectType()

Returns the type of the current DTO. It is used when JSON is turned back into an instance of your class. By default the package looks up your class by its name. It can find the class only when the class is global, or when you have registered a type resolver (a class that tells the framework where to find the Apex classes in your namespace). If neither is true, override this method in your DTO and return its type.

Returns Type — The class type of the DTO.

Throws

ExceptionDescription
IllegalArgumentExceptionif the package cannot find your class by its name and you did not override this method

Example

apex
public class DTO_Invoice extends DTO_JsonBase
{
    public override Type getObjectType()
    {
        return DTO_Invoice.class;
    }
}

serialize ​

apex
global override virtual String serialize()

Serializes the current DTO instance into a JSON string, omitting null values to create a compact JSON representation. Useful for sending structured data over web services in a lightweight format.

Returns String — A JSON string representation of the current DTO, excluding null values.

Example

apex
String result = instance.serialize();