假设我想创建一个类似于以下内容的XML响应:
<?xml version="1.0" encoding="utf?8"?>
<product xmlns="urn:com.acme.prods" xmlns:atom="http://www.w3.org/2005/xlink">
<id>1234</id>
<name>Red Stapler</name>
<price currency="EUR">3.14</price>
<atom:link rel="payment" type="application/com.acme.shop+xml"
href="http://acme.com/products/1234/payment" />
</product>
Run Code Online (Sandbox Code Playgroud)
给定一个类似于以下内容的域模型:
<?php
// Product.php
namespace Acme\Bundle\ProductBundle\Entity;
use Acme\Bundle\ProductBundle\Money\Money;
class Product
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $name;
/**
* @var Money
*/
private $price;
[..]
}
Run Code Online (Sandbox Code Playgroud)
和金钱类一样:
<?php
// Money.php
namespace Acme\Bundle\ProductBundle\Money;
class Money
{
/**
* @var string
*/
private $currency;
/**
*
*/
private $amount;
}
Run Code Online (Sandbox Code Playgroud)
现在,我的问题.创建如下所示的响应非常简单
<?xml version="1.0" encoding="utf?8"?>
<product>
<id>1234</id> …Run Code Online (Sandbox Code Playgroud) 我有一个实体,我通常使用JMS Serializer包进行序列化.我必须在序列化中添加一些字段,这些字段不驻留在实体本身中,而是通过一些数据库查询收集.
我的想法是创建一个自定义对象,用实体字段填充字段并添加自定义对象.但是对于每个变体(我使用很多序列化组)而言,这似乎有点棘手且昂贵.
有没有更好/标准的方法来做到这一点?用工厂?前/后序列化事件?
也许我可以监听序列化并检查实体类型和序列化组添加自定义字段?但是,不是对每个实体进行查询,而是收集相关实体的所有数据然后将其添加到它们中更好.任何帮助表示赞赏
我试图使用JMSSerializer作为一个独立的库来将API的JSON响应映射到我的模型类,并且遇到了一些问题.
执行以下代码会导致异常:
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use JMS\Serializer\Annotation AS JMS;
class Trii {
/**
* User ID for this session
* @JMS\SerializedName("userID")
* @JMS\Annotation(getter="getUserId")
* @JMS\Type("string")
* @var string
*/
private $userId;
public function getUserId() {
return $this->userId;
}
public function setUserId($userId) {
$this->userId = $userId;
}
}
$serializer = \JMS\Serializer\SerializerBuilder::create()->setDebug(true)->build();
$object = $serializer->deserialize('{"userID":"Trii"}', 'Trii', 'json');
var_dump($object);
?>
Run Code Online (Sandbox Code Playgroud)
这是例外
Doctrine\Common\Annotations\AnnotationException: [Semantical Error] The annotation "@JMS\Serializer\Annotation\SerializedName" in property Trii::$userId does not exist, or could not be auto-loaded.
Run Code Online (Sandbox Code Playgroud)
我通过composer为项目安装了以下库
{
"require": …Run Code Online (Sandbox Code Playgroud) 我仔细阅读了这篇文章:https://github.com/schmittjoh/serializer/issues/77但是没有找到任何方法在JSON中为JOS序列化器的FOS Rest包中序列化空值(意味着只显示Doctrine的关键)对象,即使它为null).
我在composer.json中使用以下配置
"jms/serializer-bundle": "0.12.*@dev",
"friendsofsymfony/rest-bundle": "0.13.*@dev",
Run Code Online (Sandbox Code Playgroud)
JMS序列化程序配置
#jms-serializer
jms_serializer:
visitors:
json:
options: 0 # json_encode options bitmask
serialize_null: true
Run Code Online (Sandbox Code Playgroud)
或者FOS Rest bunde配置
fos_rest:
view:
serialize_null: true
Run Code Online (Sandbox Code Playgroud)
不行.我没有使用视图我是"view_response_listener:'force'"所以如果可以提供配置中的解决方案,那将有所帮助,谢谢.
我正在尝试使用JMS Serializer序列化实体关系.
这是实体:
class Ad
{
/**
* @Type("string")
* @Groups({"manage"})
*
* @var string
*/
private $description;
/**
* @Type("Acme\SearchBundle\Entity\Country")
* @Groups({"manage"})
*
* @var \Acme\SearchBundle\Entity\Country
*/
private $country;
/**
* @Type("string")
* @Groups({"manage"})
*
* @var string
*/
private $title;
/**
* Set description
*
* @param string $description
* @return Ad
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description; …Run Code Online (Sandbox Code Playgroud) 问题:
序列化Doctrine enitities集合时,虽然项目为空,但集合仍然有2个项目.
背景:
我有一些实体相互B延伸延伸A和C延伸B.在实体中,Test我有一个包含该类型对象的数组B.$test将在序列化时具有预期值(具有两个项目的集合).
$test包含一个变量(数组)数组中的collection一个项是类型B和一个类型C.
$sTest虽然项目是空的,但会获得两个项目的集合.这是$sTest串行化后字符串的外观$test "{"collection":[[],[]]}"
测试脚本:
$test = new Test();
$b = new B();
$b->setToken('asdf');
$b->setName('asdf');
$c = new C();
$c->setToken('asdf');
$c->setName('asdf');
$c->setDescription('asdf');
$test->addCollection($b);
$test->addCollection($c);
//Serialize
$serializer = $this->container->get('serializer');
$sTest = $serializer->serialize($test, 'json');
//Deserialize
$deserializer = $this->container->get('serializer');
$dTest = $deserializer->deserialize($sTest, 'Acme\DemoBundle\Entity\Test', 'json');
$em = $this->getDoctrine()->getManager();
$em->merge($dTest);
$em->flush();
Run Code Online (Sandbox Code Playgroud)
A:
<?php
namespace …Run Code Online (Sandbox Code Playgroud) 我有两个实体,我想用JMSSerializerBundle序列化.该Music实体的映射文件用exclusion_policy: NONE.
该Music实体拥有实体的领域User从FOSUserBundle.该User实体有一个映射文件,exclusion_policy: ALL其中设置了几个字段expose: true,因此它们将被序列化.
问题是,该User字段完全序列化.如果我更改User实体的映射文件并不重要.
这是它的样子:
#My/Bundle/Resources/config/serializer/Entity.Music.yml
xxx\xxx\Entity\Music:
exclusion_policy: NONE
#My/Bundle/Resources/config/serializer/Entity.User.yml
xxx\xxx\Entity\User:
exclusion_policy: ALL
properties:
id:
expose: true
username:
expose: true
username_canonical:
exclude: true
email:
exclude: true
email_canonical:
exclude: true
enabled:
exclude: true
salt:
exclude: true
password:
exclude: true
last_login:
exclude: true
confirmation_token:
exclude: true
password_requested_at:
exclude: true
groups:
exclude: true
locked:
exclude: true
expired:
exclude: true
expires_at:
exclude: …Run Code Online (Sandbox Code Playgroud) serialization symfony symfony-2.1 jmsserializerbundle symfony-2.0
我正在使用JMS Serializer.我发现当我使用大数据时性能非常糟糕.我将以下对象结构显示为数组:
$jsonData = array(
'message' => 'this is a nice message',
'data' => array(
0 => array(
'firstname' => 'achim',
'lastname' => 'menzel'
)
)
);
Run Code Online (Sandbox Code Playgroud)
这是我序列化数据的方式:
$serializer = $this->get('serializer');
$encodedJson = $serializer->serialize($jsonData, 'json');
$response = new Response($encodedJson);
$response->headers->set('Content-Type', 'application/json');
Run Code Online (Sandbox Code Playgroud)
数据可以是1直到n对象的列表.当我在数据中有超过500个对象时,性能非常慢(超过5秒).当我json_encode()直接使用时,花费的时间不超过1秒.
如何改进JMS Serializer的使用?我不认为jms序列化程序无法处理大数据.
这是将用于序列化的主类:
class JsonData {
public $success = false;
public $message = '';
public $data;
public $responseCode = 200;
public $contentType = 'application/json';
}
Run Code Online (Sandbox Code Playgroud)
目前这个对象在里面$data:
class GuestDTO {
private …Run Code Online (Sandbox Code Playgroud) 我在反序列化学说集合的序列化集合时遇到问题.找不到关于那个和任何主题的文档,我是JMSSerializer的新手.当我尝试反序列化:
$collection = $serializer->deserialize($jsonData,'Doctrine\Common\Collections\ArrayCollection','json');
Run Code Online (Sandbox Code Playgroud)
$ collection是空的
当我设置为null而不是类名时,我在结果上有关联数组.是否有一种优雅的方式来反序列化json?
编辑:抱歉.这是序列化的集合:
[{"id":88,"name":"Poland","created_at":"2012-09-28T11:59:06+0000"},{"id":90,"name":"Great Britain","created_at":"2012-09-28T11:59:06+0000"}]
Run Code Online (Sandbox Code Playgroud) 我有一个json对象,我收到了一个get API调用.我打这个电话来接收一个对象列表.这是一个帖子列表...所以我有一个Post Objects数组.
这里输出:
{
"total":2,
"data":[
{
"id":2,
"user":{
"id":1,
"username":"sandro.tchikovani"
},
"description":"cool",
"nb_comments":0,
"nb_likes":0,
"date_creation":"2014-04-13T20:07:34-0700"
},
{
"id":1,
"user":{
"id":1,
"username":"sandro.tchikovani",
},
"description":"Premier pooooste #lol",
"nb_comments":0,
"nb_likes":0,
"date_creation":"2014-04-13T15:15:35-0700"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想反序列化数据部分......问题是Symfony中的Serializer给了我一个错误......
我有的错误:
Class array<Moodress\Bundle\PosteBundle\Entity\Poste> does not exist
Run Code Online (Sandbox Code Playgroud)
我如何反序列化:
$lastPosts = $serializer->deserialize($data['data'], 'array<Moodress\Bundle\PosteBundle\Entity\Poste>', 'json');
Run Code Online (Sandbox Code Playgroud)
如何反序列化数据数组...要有一个Postes数组.我想提出我的看法.twig一个数组Poste ...我在反序列化时做了精确的类型......所以我找不到问题所在...
谢谢.
symfony ×9
php ×6
doctrine-orm ×2
symfony-2.1 ×2
hateoas ×1
hypermedia ×1
json ×1
symfony-2.0 ×1