7

Imagine I have a simple object, structured similarly to the one below:

Object (SomeClass) {
    $someOtherData (array) [
        ...
    ]

    $data (array) [
        "key": "value",
        "key": "value",
        "key": "value",
        "key": "value"
    ]
}

If I were to serialize this object with JMS Serializer to JSON, I'd get a result that has an identical structure, but with $data being on the root element, like so:

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

I need to have the content of the $data variable be on the root of the serialized result, i.e:

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

Is this possible? If so, how?

Seer
  • 5,226
  • 5
  • 33
  • 55

2 Answers2

9

Turns out there is an annotation for this. It's the @Inline annotation:

use JMS\Serializer\Annotation\Inline;

// ...

/**
 * @var array
 *
 * @Inline
 */
protected $variables;
Seer
  • 5,226
  • 5
  • 33
  • 55
0

I think the best method is using a SerializationHandler. Here you can find a bit of documentation: http://jmsyst.com/libs/serializer/master/handlers.

M. Foti
  • 3,156
  • 2
  • 16
  • 14
  • Hmmm, I have been trying that since I posted this question - only thing is, I keep getting 'null' as the result of the serialization, no matter what I return from the handler. Weird... – Seer Sep 10 '14 at 13:10
  • could you create a gist with object class file and SerializerHandler? – M. Foti Sep 10 '14 at 13:18
  • 2
    Nevermind! I found exactly what I was after. The @Inline annotation. – Seer Sep 10 '14 at 13:33