DTO_JsonBase
Class · Group: Web Services
Learn it: DTOs guide
@JsonAccess(serializable='always' deserializable='always') global inherited sharing virtual class DTO_JsonBase extends DTO_BaseExtends: 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
@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
| Method | Description |
|---|---|
| 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
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
| Parameter | Type | Description |
|---|---|---|
dtoString | String | A JSON string representing the serialized data for the DTO. |
Returns DTO_Base — A deserialized DTO instance populated with values from the JSON string.
Example
DTO_Base result = instance.deserialize('value');See Also DTO_JsonBase.getObjectType
getObjectType
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
| Exception | Description |
|---|---|
| IllegalArgumentException | if the package cannot find your class by its name and you did not override this method |
Example
public class DTO_Invoice extends DTO_JsonBase
{
public override Type getObjectType()
{
return DTO_Invoice.class;
}
}serialize
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
String result = instance.serialize();