0

I need to create different object views depending on context.

For example, I need to serialize object for MySQL and ElasticSearch. For ElasticSearch I need to place word "def" instead of empty string. I also want the objects to be deserialized properly, replacing "def" to empty string when deserizlizing data from ElasticSearch. But for MySQL I don't want to do this - empty string stays empty string, no additional logic needed. How can I do this using JmsSerializer?

Here is an example:

<?php

use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;

/**
 *
 * @ExclusionPolicy("all")
 */
class MyClass
{
    /**
     * @Expose
     */
    private $property = '';
}

I need to serialize MyClass::$property as an empty string for MySQL, as "def" for ElasticSearch. Deserialize "def" as empty string from ElasticSearch.

UPD: I've managed this by using serialization subscriber but this is really workaround because it cannot detect serialization context

Serhii Smirnov
  • 1,338
  • 1
  • 16
  • 24

0 Answers0