Sam Anthony
Sam Anthony

Reputation: 1749

Symfony Serializer: Denormalize (Deserialize) awkward array data

Using symfony/serializer version 4. The data I am getting back JSON from an API that looks like this

{
  "name": "Steves Book Shop",
  "book_1": "Lord of the Rings",
  "book_2": "The Hobbit",
  "book_[n]": "there can be any number of books"
}

I want to deserialize into the following models

class BookShop
{
    protected $name;

    /** @var  Book[] */
    protected $books;

    public function getBooks(): array
    {
        return $this->books;
    }

    public function setBooks(array $books)
    {
        $this->books = $books;
    }

    public function addBook(Book $book)
    {
        $this->books[] = $book;
    }
    // ... other code removed to save space   
}

class Book
{
    protected $title;
    // ... other code removed to save space 
}

When using "cleaner" JSON below, everything works as expected and I get a BookShop with and array of Books returned.

{
  "name": "Steves Book Shop",
  "books": [
      { "title": "Lord of the Rings" },
      { "title": "The Hobbit" }   
    ]
}

What would be a clean way to denormalize the original JSON which instead has the annoying book_1, book_2 etc.

I have been experimenting with a custom denormalizer (DenormalizerInterface) and my solution is looking much more difficult than you would expect.

Upvotes: 1

Views: 2372

Answers (2)

Sam Anthony
Sam Anthony

Reputation: 1749

Here is the solution I ended up with, I am not completely convinced this is the best way, but its working for the moment. Any feedback welcome:

class StrangeDataDenormalizer extends Symfony\Component\Serializer\Normalizer\ObjectNormalizer
{
    protected function isStrangeDataInterface(string $type): bool
    {
        try {
            $reflection = new \ReflectionClass($type);

            return $reflection->implementsInterface(StrangeDataInterface::class);
        } catch (\ReflectionException $e) { // $type is not always a valid class name, might have extract junk like `[]`
            return false;
        }
    }

    public function denormalize($data, $class, $format = null, array $context = array())
    {
        $normalizedData = $this->prepareForDenormalization($data);

        $normalizedData = $class::prepareStrangeData($normalizedData);

        return parent::denormalize($normalizedData, $class, $format, $context);
    }

    public function supportsDenormalization($data, $type, $format = null)
    {
        return $this->isStrangeDataInterface($type);
    }
}

interface StrangeDataInterface
{
    public static function prepareStrangeData($data): array;
}

class BookShop implements StrangeDataInterface
{
    public static function prepareStrangeData($data): array
    {
        $preparedData = [];

        foreach ($data as $key => $value) {
            if (preg_match('~^book_[0-9]+$~', $key)) {
                $preparedData['books'][] = ['title' => $value];
            } else {
                $preparedData[$key] = $value;
            }
        }

        return $preparedData;
    }

    // .... other code hidden
}

function makeSerializer(): Symfony\Component\Serializer\Serializer
{
    $extractor = new ReflectionExtractor();

    $nameConverter = new CamelCaseToSnakeCaseNameConverter();

    $arrayDenormalizer = new ArrayDenormalizer(); // seems to help respect the 'adder' typehints in the model. eg `addEmployee(Employee $employee)`

    $strangeDataDenormalizer = new StrangeDataDenormalizer(
        null,
        $nameConverter,
        null,
        $extractor
    );

    $objectNormalizer = new ObjectNormalizer(
        null,
        $nameConverter,
        null,
        $extractor
    );

    $encoder = new JsonEncoder();

    $serializer = new Symfony\Component\Serializer\Serializer(
        [
            $strangeDataDenormalizer,
            $objectNormalizer,
            $arrayDenormalizer,
        ],
        [$encoder]
    );

    return $serializer;
}

Upvotes: 1

tarlepp
tarlepp

Reputation: 358

You should use ArrayCollection for those books - assuming that those are just another entity on your application

Upvotes: 0

Related Questions