On Fri, Sep 25, 2009 at 5:16 PM, Scott Haneda <talkli...@newgeo.com> wrote:
>
> I can not see it
> being that huge a performance hit to massage that string a bit once you get
> ahold of it.

Precisely. If you really want to turn the numbers into strings in PHP,
here are 2 workaround examples. Please note, I use PHP personally, so
I am not apathetic to this issue. There are programmatic ways of
dealing with it, and that's half the fun of programming, right?

Example 1 (cursor specific example);
<?
/* normally use curl, but using file_get_contents for simplicity of example */
$json = 
file_get_contents("http://twitter.com/followers/ids/barackobama.json?cursor=-1";);

echo $json;

$pattern = "/\"next_cursor\":([0-9]+),/";
$replace = "\"next_cursor\":\"$1\",";

$new_json = preg_replace($pattern, $replace, $json);

$pattern = "/\"previous_cursor\":([0-9]+),/";
$replace = "\"previous_cursor\":\"$1\",";

/* note $new_json in third parameter */
$new_json = preg_replace($pattern, $replace, $new_json);

echo $new_json;


var_dump(json_decode($new_json));

?>


Example 2 (generalized json "naked" numbers example):
<?

/* generic helper function to turn all number types in json into strings */
function stringify_json_numbers($json)
{
  $pattern = "/([:[]?)([-]?[0-9]+)([,}\]])/";
  $replace = "$1\"$2\"$3";

  $new_json = preg_replace($pattern, $replace, $json);

  return $new_json;

}

/* normally use curl, but using file_get_contents for simplicity of example */
$json = 
file_get_contents("http://twitter.com/followers/ids/barackobama.json?cursor=-1";);

echo $json;

$new_json = stringify_json_numbers($json);


echo $new_json;

echo "<pre>";
var_dump(json_decode($new_json));
echo "</pre>";
?>

Can't we all just get along?

-Chad

Reply via email to