Is it possible to serialize an array at the root of an object using JMS Serializer?

Imagine that I have a simple object structured like the one below:

Object (SomeClass) { $someOtherData (array) [ ... ] $data (array) [ "key": "value", "key": "value", "key": "value", "key": "value" ] } 

If I were to serialize this object using JMS Serializer in JSON, I would get the result with the same structure, but with $ data on the root element, for example:

 { "someOtherData": { ... }, "data": { "key": "value", "key": "value", "key": "value", "key": "value" } } 

I need the contents of the $ data variable to be at the root of the serialized result, i.e.

 { "someOtherData": { ... }, "key": "value", "key": "value", "key": "value", "key": "value" } 

Is it possible? If so, how?

+6
source share
2 answers

It turns out there is an annotation for this. This is @Inline annotation:

 use JMS\Serializer\Annotation\Inline; // ... /** * @var array * * @Inline */ protected $variables; 
+7
source

I think the best way is to use SerializationHandler. You can find some documentation here: http://jmsyst.com/libs/serializer/master/handlers .

0
source

Source: https://habr.com/ru/post/975061/


All Articles