我正在使用JMSSerializerBundle,并注意到序列化的奇怪行为。
我正在使用$ jsonItems = $ serializer->序列化($ invoiceItems,' json'); $ invoiceitems如下:
array(2) {
["services"]=>
array(3) {
[1]=>
array(1) {
[0]=>
object(stdClass)#757 (26) {
["__CLASS__"]=>
string(43) "Evo\BackendBundle\Entity\InvoiceItemService"
["id"]=>
int(4)
}
}
[2]=>
array(0) {
}
[3]=>
array(1) {
[1]=>
object(stdClass)#846 (26) {
["__CLASS__"]=>
string(43) "Evo\BackendBundle\Entity\InvoiceItemService"
["id"]=>
int(7)
}
}
}
["marchandises"]=>
array(0) {
}
}
我得到以下JSON:
{"services"{
"1":[
{"realDiscount":0,"id":4,"createdAt":"2014-08-22","description":"test 2","type":1,"totalFlat":75,"discountFlat":0,"discountFlatType":"%","chargedHours":0,"chargedMinutes":0,"hourlyRate":0,"discountHourly":0,"discountHourlyType":"%","chargedQuantity":0,"unitPrice":0,"discountQuantity":0,"discountQuantityType":"%","total":75,"discount":0,"discountType":"%","position":1,"chargedTime":0}
],
"2":[],
"3":{
"1":{"realDiscount":0,"id":7,"createdAt":"2014-08-22","description":"test 22","type":3,"totalFlat":0,"discountFlat":0,"discountFlatType":"%","chargedHours":0,"chargedMinutes":0,"hourlyRate":0,"discountHourly":0,"discountHourlyType":"%","chargedQuantity":50,"unitPrice":1,"discountQuantity":0,"discountQuantityType":"%","total":50,"discount":0,"discountType":"%","position":1,"chargedTime":0}
}
},
"marchandises":[]}
为什么在我的JSON字符串中,服务[1]的格式与服务[3]不同? 如果我引用我的PHP数组,唯一的区别是服务[1]的第一个键是0,而服务[3]的第一个键是1.它就像JMS Serializer没有映射键时它等于0.最后,我想在JSON字符串中将键等于0保持。
我该怎么做?
答案 0 :(得分:3)
将此逻辑应用于整个项目:
只需将这些行添加到序列化程序配置(config.yml):
jms_serializer:
visitors:
json:
options: [JSON_FORCE_OBJECT]
来源:http://jmsyst.com/bundles/JMSSerializerBundle/master/configuration#extension-reference
每次通话应用此逻辑:
您必须创建EventSubscriber
<?php
namespace Acme\Api\Serializer;
use JMS\Serializer\EventDispatcher\Events;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\PreSerializeEvent;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use JMS\Serializer\JsonSerializationVisitor;
class JsonSerializationSubscriber implements EventSubscriberInterface
{
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
array('event' => Events::PRE_SERIALIZE, 'method' => 'onPreSerialization', 'format' => 'json'),
array('event' => Events::POST_SERIALIZE, 'method' => 'onPostSerialization', 'format' => 'json')
);
}
/**
* @param PreSerializeEvent $event
*/
public function onPreSerialization(PreSerializeEvent $event)
{
$context = $event->getContext();
if (!$context->attributes->contains('json_encode_options')) {
return;
}
$visitor = $event->getVisitor();
if ($visitor instanceof JsonSerializationVisitor) {
$options = $visitor->getOptions();
$context->setAttribute('__old_json_encode_options', $options);
$visitor->setOptions($options | $context->attributes->get('json_encode_options'));
}
}
/**
* @param ObjectEvent $event
*/
public function onPostSerialization(ObjectEvent $event)
{
$context = $event->getContext();
if (!$context->attributes->contains('__old_json_encode_options')) {
return;
}
$visitor = $event->getVisitor();
if ($visitor instanceof JsonSerializationVisitor) {
$visitor->setOptions($context->attributes->get('__old_json_encode_options'));
}
}
}
在配置文件中注册EventSubscriber
:
services:
acme.serializer.subscriber.json:
class: Acme\Api\Serializer\JsonSerializationSubscriber
tags:
- {name: jms_serializer.event_subscriber}
然后,只要你想使用它:
$context = new SerializationContext();
$context->setAttribute('json_encode_options', JSON_FORCE_OBJECT);
$this->get('serializer')->serialize($var, 'json', $context);