如何使用 Symfony Serializer 将字符串转换为 int
How convert string to int with Symfony Serializer
我正在尝试将一个数组反规范化为一个对象,但是当 denormalize 方法遇到一个数字代码字段时,它会出错,因为它是数组中的一个字符串,也许现在修复它
{
"name": "Exception",
"message": "The type of the \"code\" attribute for class \"App\Http\Response\Flight\DriverResponse\" must be one of \"int\" (\"string\" given).",
"code": 0,
"type": "Symfony\Component\Serializer\Exception\NotNormalizableValueException",
"file": "/app/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php",
"line": 445,
"stack-trace": [
"#0 /app/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php(331): Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->validateAndDenormalize('App\\Http\\Respon...', 'code', '1', 'array', Array)",
"#1 /app/vendor/symfony/serializer/Serializer.php(185): Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->denormalize(Array, 'App\\Http\\Respon...', 'array', Array)",
"#2 /app/src/Http/Response/Transformer.php(96): Symfony\Component\Serializer\Serializer->denormalize(Array, 'App\\Http\\Respon...', 'array')",
"#3 [internal function]: App\Http\Response\Transformer->App\Http\Response\{closure}(Array)",
"#4 /app/src/Http/Response/Transformer.php(98): array_map(Object(Closure), Array)",
"#5 /app/src/Http/Controllers/Api/Flight/DriverController.php(124): App\Http\Response\Transformer->transform(Object(yii\data\SqlDataProvider), 'drivers', 'App\\Http\\Respon...')",
"#6 [internal function]: App\Http\Controllers\Api\Flight\DriverController->actionIndex()",
"#7 /app/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)",
"#8 /app/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)",
"#9 /app/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('index', Array)",
"#10 /app/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('Api/Flight/driv...', Array)",
"#11 /app/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))",
"#12 /app/web/index.php(14): yii\base\Application->run()",
"#13 {main}"
]
}
我修好了,只需要通过这个设置[AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true]
$this->serializer->denormalize(
$array,
$class,
'array',
[AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true]
);
我正在尝试将一个数组反规范化为一个对象,但是当 denormalize 方法遇到一个数字代码字段时,它会出错,因为它是数组中的一个字符串,也许现在修复它
{
"name": "Exception",
"message": "The type of the \"code\" attribute for class \"App\Http\Response\Flight\DriverResponse\" must be one of \"int\" (\"string\" given).",
"code": 0,
"type": "Symfony\Component\Serializer\Exception\NotNormalizableValueException",
"file": "/app/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php",
"line": 445,
"stack-trace": [
"#0 /app/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php(331): Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->validateAndDenormalize('App\\Http\\Respon...', 'code', '1', 'array', Array)",
"#1 /app/vendor/symfony/serializer/Serializer.php(185): Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->denormalize(Array, 'App\\Http\\Respon...', 'array', Array)",
"#2 /app/src/Http/Response/Transformer.php(96): Symfony\Component\Serializer\Serializer->denormalize(Array, 'App\\Http\\Respon...', 'array')",
"#3 [internal function]: App\Http\Response\Transformer->App\Http\Response\{closure}(Array)",
"#4 /app/src/Http/Response/Transformer.php(98): array_map(Object(Closure), Array)",
"#5 /app/src/Http/Controllers/Api/Flight/DriverController.php(124): App\Http\Response\Transformer->transform(Object(yii\data\SqlDataProvider), 'drivers', 'App\\Http\\Respon...')",
"#6 [internal function]: App\Http\Controllers\Api\Flight\DriverController->actionIndex()",
"#7 /app/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)",
"#8 /app/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)",
"#9 /app/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('index', Array)",
"#10 /app/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('Api/Flight/driv...', Array)",
"#11 /app/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))",
"#12 /app/web/index.php(14): yii\base\Application->run()",
"#13 {main}"
]
}
我修好了,只需要通过这个设置[AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true]
$this->serializer->denormalize(
$array,
$class,
'array',
[AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true]
);