Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

boolean values not serialized when value is null. #356

Closed
schingeldi opened this issue Jan 10, 2014 · 3 comments
Closed

boolean values not serialized when value is null. #356

schingeldi opened this issue Jan 10, 2014 · 3 comments

Comments

@schingeldi
Copy link

I have serialized entity with a boolean attribute. This attribute is not shown when the value is null, its only shown if it is 0. As I use it with FosRestBundle and a form, it is set to null automatically if I change it to "false".

Is there any possibility to show null values as false?

@schmittjoh
Copy link
Owner

You'll need to write you own handler if you want to automatically make such
a conversion.

On Fri, Jan 10, 2014 at 11:13 AM, schingeldi [email protected]:

I have serialized entity with a boolean attribute. This attribute is not
shown when the value is null, its only shown if it is 0. As I use it with
FosRestBundle and a form, it is set to null automatically if I change it to
"false".

Is there any possibility to show null values as false?


Reply to this email directly or view it on GitHubhttps://github.com//issues/356
.

@stof
Copy link
Contributor

stof commented Jan 10, 2014

and if you want to show null values as null, you can enable the serialization of null values int he SerializationContext

@schingeldi
Copy link
Author

Thanks, but where exactly to I configure this. Now I followed the documentation and did:

namespace Acme\AdBundle\Handler;

use JMS\Serializer\Context;
use JMS\Serializer\JsonSerializationVisitor;


class SerializationHandler {


public function serializeBooleanToJson( JsonSerializationVisitor $visitor, $bool, array $type, Context $context ){

    $context->setSerializeNull(true);

    if( $bool ) {
        return 'YES';
    }
    return 'NO';

}

}

And in services.xml:

<service id="acme.serialization_handler" class="Acme\AdBundle\Handler\SerializationHandler" public="false">
            <tag name="jms_serializer.handler" type="boolean" direction="serialization" format="json"
             method="serializeBooleanToJson" />
</service>

But it does not seem to call thehandler at all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants