PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/pdf/code/trunk/administrator/components/com_artofpdf/helpers/html/json.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 86 lines | 57 code | 8 blank | 21 comment | 9 complexity | 50ee1b0ace6ac6f046af896a76b3f8f7 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: json.php 271 2010-09-09 08:35:10Z eddieajau $
  4. * @copyright Copyright (C) 2009 New Life in IT Pty Ltd. All rights reserved.
  5. * @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
  6. * @link http://www.theartofjoomla.com
  7. * @author John Glazebrook <open-flash-chart@teethgrinder.co.uk>
  8. */
  9. // no direct access
  10. defined('_JEXEC') or die;
  11. /**
  12. * JSON formatter
  13. *
  14. * @package NewLifeInIT
  15. * @subpackage com_artofpdf
  16. */
  17. class JHtmlJSON
  18. {
  19. /**
  20. * @param mixed $json A JSON string or object.
  21. *
  22. * @return string
  23. * @since 1.0
  24. */
  25. function format($json)
  26. {
  27. // Flatten the string or object.
  28. $json = json_encode(json_decode($json));
  29. $tab = ' ';
  30. $result = '';
  31. $indent = 0;
  32. $instr = false;
  33. $len = strlen($json);
  34. for ($c = 0; $c < $len; $c++)
  35. {
  36. $char = $json[$c];
  37. switch ($char)
  38. {
  39. case '{':
  40. case '[':
  41. if (!$instr) {
  42. $result .= $char."\n".str_repeat($tab, $indent+1);
  43. $indent++;
  44. }
  45. else {
  46. $result .= $char;
  47. }
  48. break;
  49. case '}':
  50. case ']':
  51. if (!$instr) {
  52. $indent--;
  53. $result .= "\n".str_repeat($tab, $indent).$char;
  54. }
  55. else {
  56. $result .= $char;
  57. }
  58. break;
  59. case ',':
  60. $result .= (!$instr ? ",\n".str_repeat($tab, $indent) : $char);
  61. break;
  62. case ':':
  63. $result .= (!$instr ? ": " : $char);
  64. break;
  65. case '"':
  66. if ($c > 0 && $json[$c-1] != '\\') {
  67. $instr = !$instr;
  68. }
  69. default:
  70. $result .= $char;
  71. break;
  72. }
  73. }
  74. return $result;
  75. }
  76. }