|
| 1 | +## Output Format |
| 2 | + |
| 3 | +### Creating the Serialize Class |
| 4 | + |
| 5 | +```php |
| 6 | +use Astral\Serialize\Serialize; |
| 7 | +use DateTime; |
| 8 | + |
| 9 | +class UserLoginLog extends Serialize { |
| 10 | + public string $remark, |
| 11 | + public DateTime $create_time; |
| 12 | +} |
| 13 | + |
| 14 | +class User extends Serialize { |
| 15 | + public string $name, |
| 16 | + public int $age, |
| 17 | + public UserLoginLog $login_log |
| 18 | +} |
| 19 | + |
| 20 | +// Create an object |
| 21 | +$user = User::from([ |
| 22 | + 'name' => 'Jon', |
| 23 | + 'age' => 30 |
| 24 | +], login_log: new UserLoginLog(remark:'Test Data',create_time: DateTime::createFromFormat('Y-m-d','2008-09-01'))); |
| 25 | +```` |
| 26 | + |
| 27 | +### Outputting the Object |
| 28 | + |
| 29 | +```php |
| 30 | +// $user is an object by default |
| 31 | +echo $user->name; // Output: Jon |
| 32 | +echo $user->age; // Output: 30 |
| 33 | +echo $user->login_log->remark; // Output 'Test Data' |
| 34 | +echo $user->login_log->create_time; // Output DateTime Object |
| 35 | + |
| 36 | +``` |
| 37 | + |
| 38 | +### Outputting an Array |
| 39 | + |
| 40 | +```php |
| 41 | +// Convert to an array |
| 42 | +$vols = $user->toArray(); |
| 43 | +echo $vols['name']; // Output: Jon |
| 44 | +echo $vols['age']; // Output: 30 |
| 45 | +echo $vols['login_log']['remark']; // Output 'Test Data' |
| 46 | +echo $vols['login_log']['create_time']; // Output 2008-09-01 00:00:00 |
| 47 | +// Content of $vols: |
| 48 | +// [ |
| 49 | +// 'name' => 'Jon', |
| 50 | +// 'age' => 30, |
| 51 | +// 'login_log' => [ |
| 52 | +// [ |
| 53 | +// 'remark' => 'Test Data', |
| 54 | +// 'create_time' => '2008-09-01 00:00:00' |
| 55 | +// ] |
| 56 | +// ] |
| 57 | +// ] |
| 58 | +``` |
| 59 | + |
| 60 | +### Outputting a JSON String |
| 61 | + |
| 62 | +1. The `Serialize` class implements `JsonSerializable` by default. Similar to a `Laravel` `Controller` you can directly return the object, and the framework will output the `JSON` information correctly |
| 63 | +2. By default, the JSON output from `Serialize` includes `data` `code` and `message` f you need to [replace/modify/add] these, please refer to the configuration information [Response Data Structure Definition](../openapi/config.md) |
| 64 | + |
| 65 | +#### Outputting JSON Information |
| 66 | + |
| 67 | +- You can use the API `toJsonString` |
| 68 | +- Alternatively, you can directly use `json_decode` |
| 69 | + |
| 70 | +```php |
| 71 | +echo $user->toJsonString(); |
| 72 | +echo json_decode($user); |
| 73 | +// Both outputs are the same |
| 74 | +// {"code":200,"message":"success","data":{"name":"Jon","age":30,"login_log":{"remark":"Test Data","create_time":"2008-09-01 00:00:00"}} |
| 75 | +``` |
| 76 | + |
| 77 | +#### Setting Output Code/Message |
| 78 | + |
| 79 | +```php |
| 80 | +$user->setCode(500); |
| 81 | +$user->setMessage('Operation failed'); |
| 82 | +echo json_decode($user); |
| 83 | +// Output |
| 84 | +// {"code":500,"message":"Operation failed","data":{"name":"Jon","age":30,"login_log":{"remark":"Test Data","create_time":"2008-09-01 00:00:00"}} |
| 85 | +``` |
| 86 | + |
| 87 | +#### Setting Custom JSON Outer Layer |
| 88 | + |
| 89 | +`withResponses` can temporarily add or modify custom return information. To add global return information, you can configure it in the [Response Data Structure Definition](../openapi/config.md) |
| 90 | + |
| 91 | +```php |
| 92 | +$user->withResponses([ |
| 93 | + "code"=> ["description"=>"Return Code", "value"=>401], |
| 94 | + "message"=> ["description"=>"Return Message", "value"=>"Operation successful"], |
| 95 | + "error"=> ["description"=>"Return Error", "value"=>0], |
| 96 | +]); |
| 97 | +// Output |
| 98 | +// {"code":401,"message":"Operation successful","error":0,"data":{"name":"Jon","age":30,"login_log":{"remark":"Test Data","create_time":"2008-09-01 00:00:00"}} |
| 99 | +``` |
| 100 | + |
| 101 | +#### Outputting JSON Without Outer Layer Information |
| 102 | + |
| 103 | +Use`withoutResponseToJsonString` to return JSON data containing only the object’s properties. |
| 104 | + |
| 105 | +```php |
| 106 | +$user->withoutResponseToJsonString(); |
| 107 | +// Output |
| 108 | +// {"name":"Jon","age":30,"login_log":{"remark":"Test Data","create_time":"2008-09-01 00:00:00"} |
| 109 | +``` |
0 commit comments