By default, beans implement the JsonSerializable interface.
As such, they can automatically be casted to JSON using the json_encode
function.
Serialization works this way:
So a typical serialized bean might look like this:
{
"id": 4, // Primary keys are serialized
"name": "Bill Shakespeare", // Normal columns are serialized
"createdAt": "2015-10-24T13:57:13+00:00", // Dates are serialized in ISO 8601 format
"email": "bill@shakespeare.com",
"country": { // Foreign keys (like country_id) are transformed into the represented object
"id": "2", // Note: foreign keys of embedded objects are ignored.
"label": "UK"
},
"roles": [ // Many to many relationships are embedded too.
{ // Note: many to many relationships of embedded objects are ignored.
"id": 2,
"name": "Writers"
}
]
}
If you want to customize JSON serialization, you have 2 options:
jsonSerialize
method in each bean class (the method is defined in the abstract bean)Found a typo? Something is wrong in this documentation? Just fork and edit it!