in reply to JSON.pm returning "false" instead of false

serialize is never called. I added:

print(serialize(undef, { key => JSON::false } ), "\n"); print(serialize(undef, bless({ key => JSON::false })), "\n");
which prints
{"key":false} {"key":false}

Do you get something different?

Replies are listed 'Best First'.
Re^2: JSON.pm returning "false" instead of false
by josh803316 (Beadle) on Apr 12, 2010 at 22:13 UTC
    Well I'm using this as a serializer object inside a Mojo App......so I'm getting a database response and converting it into JSON. I have a value that is returned as $hash->{'leaf'} = JSON::false but it is returning as "leaf": "false". I'm pretty sure I'm calling the serialize function but I'll have to trace it through. This is using Mojo, which has a render_json function and I'm over-riding Mojo::JSON with a controller that does this:
    BEGIN { # install JSON and JSON::XS if you can! eval 'use JSON -convert_blessed_universally;'; eval ( $@ ? 'sub HAS_JSON(){ 0 }' : 'sub HAS_JSON(){ 1 }' ); }
    Which makes it all a bit complicated. I just noticed that the return wasn't what I expected. I'll double check that my serialize is getting called but I'm pretty sure it is.

      I'm pretty sure I'm calling the serialize function but I'll have to trace it through.

      I meant in the code you provided. In other words, you didn't demonstrate a problem since the code you posted didn't do anything at all. My guesses as to what your code might be proved to be a waste of time since I could not reproduce the problem you describe.

      Please provide minimal code that reproduces the problem. The code you posted works fine when I call it. I even tried replacing the use statement with the BEGIN block you more recently provided.

        It's a big app so I didn't want to bog anyone down with the details, although I should have provided a test case. Your answer actually brought me to the solution though. It turns out that the $self->render_json method from Mojo was not being over-ridden correctly, and was therefore never calling the "serialize" method just as you suggested. So, I thank you very much for giving me a hint and leading me down the correct path. Next time, I will provide a test case that demonstrates the issue a little more clearly.