dongwenhui8900 2019-03-30 13:02 采纳率: 0%
浏览 33

覆盖JMS Serializer全局camelCase

I wish to default to outputting camel case names, and this answer provides an excellent solution.

$serializer= \JMS\Serializer\SerializerBuilder::create()
    ->setDebug(true)
    ->addMetadataDir(APP_ROOT.'/config/serializer')
    ->setPropertyNamingStrategy(new \JMS\Serializer\Naming\IdenticalPropertyNamingStrategy())
    ->build();

I also wish to occasionally override the serialized name, however, when doing it the traditional way using serialized-name, the serializer does not follow the instructions, and outputs idPublic instead of the desired id. If I remove the global override to using identical property names, id is outputted as desired, however, obviously all the other properties are snake-case (i.e. other_property) and need to be individually configured which is not desirable.

<?xml version="1.0" encoding="UTF-8" ?>
<serializer>
    <class name="Fully\Qualfied\ClassName" exclusion-policy="ALL">
        <property name="idPublic" serialized-name="id" expose="true"/>
        <property name="name" expose="true"/>
        <property name="otherProperty" expose="true"/>
    </class>
</serializer>

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="Fully\Qualfied\ClassName" table="systems">
    <id name="id" type="integer">
      <generator strategy="IDENTITY"/>
    </id>
    <field name="idPublic" type="integer" column="id_public"/>
    <field name="name" type="string"/>
    <field name="otherProperty" type="string" length="45" column="other_property"/>
  </entity>
</doctrine-mapping>

As an alternate approach, I even tried to eliminating the normal property and using a virtual property instead, but it too resulted in the name idPublic.

<?xml version="1.0" encoding="UTF-8" ?>
<serializer>
    <class name="Fully\Qualfied\ClassName" exclusion-policy="ALL">
        <virtual-property method="getIdPublic" serialized-name="id" expose="true"/>
        <property name="name" expose="true"/>
        <property name="otherProperty" expose="true"/>
    </class>
</serializer>

How can one override JMS Serializer's global configuration to use identical names?

  • 写回答

1条回答 默认 最新

  • doutangxi2144 2019-03-30 14:04
    关注

    Don't know whether this is the "right" way to do this, but it works.

    class SerializerNamingStrategy implements \JMS\Serializer\Naming\PropertyNamingStrategyInterface
    {
        public function translateName(\JMS\Serializer\Metadata\PropertyMetadata $property)
        {
            return $property->serializedName??$property->name;
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构