Friday 5 July 2013

JSON: just to confirm my expectations aren't off

G'day:
I'm half-way across the Irish Sea at the moment (oh, now that I look out the window, I've actually just making landfall over Ireland), and sitting in a very cramped seat trying to write code on my netbook without elbowing the bloke next to me too much. So this will be short and to the point. And hopefully the last chapter in all this JSON nonsense.


Just in case I was being rough on ColdFusion in having these crazy expectations that it could tell the difference between a string and a numeric value when it comes to serialising data into a type-aware format, I checked further. As you know I've already checked Railo, and it has none of these issues. But on a whim I have also checked PHP and Ruby - similar sorts of languages to CFML in a way - to see how they handle it.

As you know I know bugger-all about either PHP or Ruby, as I'm a distinct n00b at bother. However I googled-up how to convert stuff to JSON in both languages. Here's my test code in each language:

# hash2json.rb
intVariable = 123
strVariable = "123"

hash = {
     :intVariable     => intVariable,
     :strVariable     => strVariable,
     :intValue     => 123,
     :strValue     => "123",
}

require "json"

str = JSON.generate(hash)

puts str

This works fine, and outputs this:
{
"intVariable":123,
"strVariable":"123",
"intValue":123,"strValue":"123"
}

And the PHP:

<?php
     // array2json.php
     $intVariable     = 123;
     $strVariable          = "123";

     $arr = array(
          "intVariable"     => $intVariable,
          "strVariable"     => $strVariable,
          "intValue"          => 123,
          "strValue"          => "123"
     );
   
     $json = json_encode($arr);
   
     echo $json;
?>


This is no problem for PHP either:
{
"intVariable":123,
"strVariable":"123",
"intValue":123,
"strValue":"123"
}

So, ColdFusion... you need to pick up your game a bit. It's really really really not acceptable to not be able to do this sort of thing properly. Especially now that JSON support has been in ColdFusion since CF8, and when was that released? 2008? (am offline, so cannot check). It's not great.

Anyway... landing soon. Need to switch off.

--
Adam