我试图将json反序列化为实体,然后合并实体.
我相信我过去有这个工作,我会发送ID和我希望更新的任何字段.例如:
在我的DB中:
| id | first | last | city |
| 1 | Jimmy | James | Seattle |
Run Code Online (Sandbox Code Playgroud)
然后我将反序列化以下json并合并实体
$json = { "id" : 1, "city": "chicago"}
$customer = $serializer->deserialize($json, 'App\CustomerBundle\Entity\Customer', 'json');
$em->merge($customer);
Run Code Online (Sandbox Code Playgroud)
预期的结果将是:
| id | first | last | city |
| 1 | Jimmy | James | Chicago |
Run Code Online (Sandbox Code Playgroud)
但是我得到以下内容:
| id | first | last | city |
| 1 | null | null | Chicago |
Run Code Online (Sandbox Code Playgroud)
就像我说的,我相信我在某些时候有这个工作,我不确定这是否与jms_serializer
or有关em->merge
.
$customer->getFirst() …
我正在尝试编写一个REST API,它在Symfony2中使用来自PUT请求的JSON.将JSON反序列化为实体类型 - 但是如果JSON中的属性类型与实体的相应属性不匹配,则JMS Serializer似乎强制来自JSON的类型而不是抛出异常.
例如 …
{ "id" : "123" }
Run Code Online (Sandbox Code Playgroud)
......会导致......
int(123)
Run Code Online (Sandbox Code Playgroud)
...如果属性id
在实体中定义为整数.
但我希望JMS Serializer能够抛出异常.有谁知道如何实现这一目标?
我发现JMS Serializer的类型处理的一个问题是:
{ "id" : "n123" }
Run Code Online (Sandbox Code Playgroud)
会导致......
int(0)
Run Code Online (Sandbox Code Playgroud)
这是完全不受欢迎的.
有人可以指出我正确的方向吗?
symfony jmsserializerbundle json-deserialization jms-serializer
我正在将JMS Serializer用于 PHP 项目,但偶然发现了一个问题。
看代码
<?php
use JMS\Serializer\Annotation as Serializer;
/**
* @Serializer\ExclusionPolicy("all")
*/
class Order
{
/**
* @var int
* @Serializer\Type("integer")
* @Serializer\Expose
*/
private $id;
/**
* @var Product[]
* @Serializer\Type("array<Product>")
* @Serializer\Expose
*/
private $products;
/**
* @var float
* @Serializer\Type("float")
* @Serializer\Expose
*/
private $total;
private $someInternalProperty;
function __construct($products)
{
$this->id = rand(0, 100);
$this->products = $products;
$this->total = rand(100, 1000);
$this->someInternalProperty = 'Flag';
}
}
/**
* @Serializer\ExclusionPolicy("all")
*/
class Product
{ …
Run Code Online (Sandbox Code Playgroud) 我在我的项目中使用 JMS\Serializer 并且我只想在其中的数组为空时忽略一个属性。
我试过类似的东西:
@JMS\Exclude(if="count('$this->required') === 0")
or
@JMS\Exclude(if="empty('required')")
Run Code Online (Sandbox Code Playgroud)
但出现语法错误。
谁可以帮我这个事?
谢谢。
我在使用 JMS Serializer 时遇到了一些问题 - 我需要用混合类型的score
值反序列化脏 JSON。例如:
{ label: "hello", score: 50 }
Run Code Online (Sandbox Code Playgroud)
或者
{ label: "hello", score: true }
Run Code Online (Sandbox Code Playgroud)
如果我输入@Type("int")
,当值是 a 时boolean
,它会被反序列化为1
or 0
...
我想得到100
何时值是true
,0
何时值是false
。
我如何在反序列化时管理这种混合类型?
我的课:
class Lorem
{
/**
* @Type("string")
* @SerializedName("label")
* @var string
*/
protected $label;
/**
* @Type("int")
* @SerializedName("score")
* @var int
*/
protected $score;
}
Run Code Online (Sandbox Code Playgroud) 我发现这个线程提出了一种巧妙的方法来全局覆盖 config.yml 中的默认命名策略
但这是针对 symfony 3 而我在 symfony 4 上,所以我config > packages > jms_serializer.yaml
将这行添加到我的,但这根本没有效果。
jms_serializer:
visitors:
xml_serialization:
format_output: '%kernel.debug%'
property_naming:
id: 'jms_serializer.identical_property_naming_strategy'
Run Code Online (Sandbox Code Playgroud)
有谁明白为什么?
我SerializerInterface $serializer
使用序列化程序进行依赖注入,因为无法从AbstractController
SF4 中调用服务。Controller
在 SF4 中已弃用。
您好我想动态更改序列化上下文的组.
代码 :
/**
* @Rest\Get("", name="bap_api_space_query")
* @Rest\View(serializerGroups={"Default", "space_dashboard", "dashboard_resource"})
*
* @ApiDoc(resource=true,description="List all spaces this user has access to")
*/
public function queryAction(Request $request)
{
$user = $this->getUser()->reload();
$organization = $user->getOrganization();
// depending the request, remove or add serialized group
// for example $view->setSerializationGroups('dashboard');
return $organization->getSpaces();
}
Run Code Online (Sandbox Code Playgroud)
如代码中所述,我想在控制器中删除或添加组.有办法吗?