具有异常 属性 名称的 Symfony4 序列化程序问题
Symfony4 Serializer problem with unusual property name
我在 Symfony4 上做了一个 REST API,所以我想用 Symfony4 的默认序列化器序列化我的实体。
但是我的实体具有不寻常的属性名称,这使得序列化程序给我带来了错误的结果。
我试过 NameConverterInterface
也试过 CamelCaseToSnakeCaseNameConverter
没有好的结果...
我的应用程序中的每个实体都有这种属性,所以带@annotation 的解决方案帮不了我
class Product implements EntityInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer", name="PROD_PKEY")
*/
private $PROD_PKEY;
/**
* @ORM\Column(type="string", length=50)
*/
private $PROD_Name;
/**
* @ORM\Column(type="string", length=50)
*/
private $PROD_Code;
以及我如何使用序列化程序:
$product = new Product();
$product->setPRODName("Name");
$product->setPRODCode("Code");
$json = $this->serializer->serialize($product, 'json');
$json的内容是:
{
"pRODName": "Name",
"pRODCode": "Code",
}
但我期待这样的事情:
{
"PROD_Name": "Name",
"PROD_Code": "Code",
}
简单地等于我实体中的属性名称,我不明白为什么第一个字母变成小写而我的下划线消失了...
感谢您的帮助!
我认为您可能需要创建自定义序列化程序,我经常使用 jmsserializer
捆绑包并且没有遇到任何问题
https://symfony.com/doc/current/serializer/custom_normalizer.html
在 Symfony 中,您可以实现自定义 NameConverter 来转换 json 表示中的字段名称。
按照这些思路应该可以解决问题:
<?php namespace App\Service;
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
class YourCustomNameConverter implements AdvancedNameConverterInterface
{
public function normalize($propertyName, string $class = null, string $format = null, array $context = [])
{
preg_match('/^([a-z]?[A-Z]+)([A-Z]{1}[_a-zA-Z]+)$/', $propertyName, $matches);
if (strstr($propertyName, 'PKEY')) {
return ucfirst(substr_replace($propertyName, '_', -4, 0));
} elseif (count($matches)) {
array_shift($matches);
$matches[0] = ucfirst($matches[0]);
return implode('_', $matches);
} else {
return $propertyName;
}
}
public function denormalize($propertyName, string $class = null, string $format = null, array $context = [])
{
return $propertyName;
}
}
我在 Symfony4 上做了一个 REST API,所以我想用 Symfony4 的默认序列化器序列化我的实体。
但是我的实体具有不寻常的属性名称,这使得序列化程序给我带来了错误的结果。
我试过 NameConverterInterface
也试过 CamelCaseToSnakeCaseNameConverter
没有好的结果...
我的应用程序中的每个实体都有这种属性,所以带@annotation 的解决方案帮不了我
class Product implements EntityInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer", name="PROD_PKEY")
*/
private $PROD_PKEY;
/**
* @ORM\Column(type="string", length=50)
*/
private $PROD_Name;
/**
* @ORM\Column(type="string", length=50)
*/
private $PROD_Code;
以及我如何使用序列化程序:
$product = new Product();
$product->setPRODName("Name");
$product->setPRODCode("Code");
$json = $this->serializer->serialize($product, 'json');
$json的内容是:
{
"pRODName": "Name",
"pRODCode": "Code",
}
但我期待这样的事情:
{
"PROD_Name": "Name",
"PROD_Code": "Code",
}
简单地等于我实体中的属性名称,我不明白为什么第一个字母变成小写而我的下划线消失了...
感谢您的帮助!
我认为您可能需要创建自定义序列化程序,我经常使用 jmsserializer
捆绑包并且没有遇到任何问题
https://symfony.com/doc/current/serializer/custom_normalizer.html
在 Symfony 中,您可以实现自定义 NameConverter 来转换 json 表示中的字段名称。
按照这些思路应该可以解决问题:
<?php namespace App\Service;
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
class YourCustomNameConverter implements AdvancedNameConverterInterface
{
public function normalize($propertyName, string $class = null, string $format = null, array $context = [])
{
preg_match('/^([a-z]?[A-Z]+)([A-Z]{1}[_a-zA-Z]+)$/', $propertyName, $matches);
if (strstr($propertyName, 'PKEY')) {
return ucfirst(substr_replace($propertyName, '_', -4, 0));
} elseif (count($matches)) {
array_shift($matches);
$matches[0] = ucfirst($matches[0]);
return implode('_', $matches);
} else {
return $propertyName;
}
}
public function denormalize($propertyName, string $class = null, string $format = null, array $context = [])
{
return $propertyName;
}
}