TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-18-2008, 05:00 PM   #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Unhappy Json_encode is crazy

To cut a long story short, I'm wanting to check the return value of the json_encode function - therefore if its false I can throw an exception. What do I find? The lovely json_encode returns false as a string if it fails!

php Code:
$aJson = json_encode($szContent);

if(!$aJson)
{
    throw new Exception('Badly formatted JSON returned');
}

That won't work because it's a string, and I can't typecast it using (bool) because the string will be seen as a true value. Here's the result from var_dump when json_encode fails:

Quote:
string(5) "false"
Does anybody have any idea? I don't want to just do it like the following as that would be very silly indeed.

php Code:
if($aJson == 'false')
{
   
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-18-2008, 05:05 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

What's the $szContent that you're failing to encode? I might be having an attack of the dumbass (lack of caffeine, honestly) but the only time json_encode returns "false" is with a boolean false. I think.
Salathe is offline  
Reply With Quote
Old 01-18-2008, 05:15 PM   #3 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

What is $szContent?
Orc is offline  
Reply With Quote
Old 01-18-2008, 05:22 PM   #4 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

So check it against the string 'false'...

Code:
if( $content === 'false' )
{
    // do ya thang :D
}
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 01-18-2008, 05:34 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Yes, $szContent would be false as I've omitted the exception above it to test what happens. So am I okay then? If $szContent is anything but false - which it won't be because it would execute the exception above it, then it WILL return false as a boolean?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-18-2008, 05:50 PM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

json_encode will never return a boolean. If you previously check that the $szContent cannot be FALSE then you might consider it ok to check to see if json_encode returns string "false".

However, if you pass in a non-encodable value (e.g. a resource) the return string won't be "false" but will instead be (string) "null" with an E_WARNING error ([json] (json_encode_r) type is unsupported, encoded as null.).

Depending on your specific scenario, simply checking for string "false" might suffice.
Salathe is offline  
Reply With Quote
Old 01-18-2008, 06:00 PM   #7 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Is there a valid reason for it returning a string and not a boolean?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-18-2008, 06:04 PM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Yep, the reasoning is:
Return as binary string, since the result is 99% likely to be just echo'ed out and we want to avoid overhead of double conversion.
From PHP source for json_encode function
Salathe is offline  
Reply With Quote
Old 01-18-2008, 06:18 PM   #9 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

What's wrong with NULL?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-18-2008, 06:47 PM   #10 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Well, json_encode will only return "null" in two cases:
  1. If the argument passed in was NULL.
  2. If the argument passed is not JSON encodable.

Therefore, a quick check for errors might be something along the lines of the following :
PHP Code:

    
// Suppress E_WARNING error if any. 
    
$szJson = @json_encode($szContent);

    
// If the encoded string is "null" but we didn't pass in NULL
    // then throw the exception.
    
if ($szJson === 'null' AND $szContent !== NULL)
    {
        throw new 
Exception(sprintf(
            
'Unable to encode "%s" into JSON format.',
            
gettype($szContent)
        ));
    } 
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Wildhoney (01-18-2008)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:57 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design