JMSSerializerBundle icon indicating copy to clipboard operation
JMSSerializerBundle copied to clipboard

Customized attributes in JSON

Open log0 opened this issue 14 years ago • 4 comments

Dear all, I'm new to JMS, got a question that I need some help on.

Suppose I have a model that has fields A and B. So serializing that gives only id, A and B, what if I want to expose as a JSON a field C (which is NOT attribute) and is a function of A and B?

e.g. {"A":2, "B":4, "C":16} , where C is A^B.

Something like that.

Thanks!!!

log0 avatar Mar 13 '12 17:03 log0

This is not possible atm.

You need to create either a) a property for C or b) write a custom handler for your object.

schmittjoh avatar Mar 13 '12 17:03 schmittjoh

Hi @schmittjoh

Thanks for swift response. I didn't clearly understand so I want to repeat to see if I'm right on this: a) create a property that will be updated on a @PreSerialize event? b) what's the custom handler you mean? Sorry, could you elaborate a bit?

Thanks!

log0 avatar Mar 13 '12 17:03 log0

@schmittjoh couldn't it be possible if you have a getter for C in the class ?

stof avatar Mar 13 '12 19:03 stof

You would still need the property to declare an @Accessor annotation on it.

On Tue, Mar 13, 2012 at 1:03 PM, Christophe Coevoet < [email protected]

wrote:

@schmittjoh couldn't it be possible if you have a getter for C in the class ?


Reply to this email directly or view it on GitHub:

https://github.com/schmittjoh/JMSSerializerBundle/issues/91#issuecomment-4483019

schmittjoh avatar Mar 13 '12 19:03 schmittjoh