admin管理员组文章数量:1123098
I'm trying to use JMS Serializer on a new PHP 8 project and somehow I'm not able to get any of the annotations to work. This is my simplified settings:
composer.json
{
"require": {
"jms/serializer": "^3.32"
}
}
test.php
<?php
require_once __DIR__.'/vendor/autoload.php';
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\SerializerBuilder;
/** @ExclusionPolicy("all") */
class Test {
/** @Expose */
private string $name;
private int $value;
public function getName(): string {
return $this->name;
}
public function setName(string $name): void {
$this->name = $name;
}
public function getValue(): int {
return $this->value;
}
public function setValue(int $value): void {
$this->value = $value;
}
}
$test = new Test();
$test->setName("John Doe");
$test->setValue(100);
$serializer = SerializerBuilder::create()->build();
echo $serializer->serialize($test, 'json');
If I execute the code above the output is the following:
{"name":"John Doe","value":100}
Instead I was expecting to see:
{"name":"John Doe"}
I also tried a million of combinations of other annotation, and no one seem to work. Any clues? Many thanks in advance!
I'm trying to use JMS Serializer on a new PHP 8 project and somehow I'm not able to get any of the annotations to work. This is my simplified settings:
composer.json
{
"require": {
"jms/serializer": "^3.32"
}
}
test.php
<?php
require_once __DIR__.'/vendor/autoload.php';
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\SerializerBuilder;
/** @ExclusionPolicy("all") */
class Test {
/** @Expose */
private string $name;
private int $value;
public function getName(): string {
return $this->name;
}
public function setName(string $name): void {
$this->name = $name;
}
public function getValue(): int {
return $this->value;
}
public function setValue(int $value): void {
$this->value = $value;
}
}
$test = new Test();
$test->setName("John Doe");
$test->setValue(100);
$serializer = SerializerBuilder::create()->build();
echo $serializer->serialize($test, 'json');
If I execute the code above the output is the following:
{"name":"John Doe","value":100}
Instead I was expecting to see:
{"name":"John Doe"}
I also tried a million of combinations of other annotation, and no one seem to work. Any clues? Many thanks in advance!
Share Improve this question asked 4 hours ago DocDbgDocDbg 4492 gold badges5 silver badges13 bronze badges1 Answer
Reset to default 0Use PHP8 Attributes. Then it works. Annotations have been optional since 3.30.0. If you install doctrine/annotations
, it should also work with annotations. See documentation.
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
#[ExclusionPolicy("all")]
class Test
{
#[Expose]
private string $name;
/* ... */
}
本文标签: phpJMS serializer annotations ignoredStack Overflow
版权声明:本文标题:php - JMS serializer annotations ignored - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736545997a1944447.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论