json.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /*
  3. * Copyright 2010-2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. /*%******************************************************************************************%*/
  17. // CLASS
  18. /**
  19. * Handles the conversion of data from JSON to other formats.
  20. *
  21. * @version 2011.03.25
  22. * @license See the included NOTICE.md file for more information.
  23. * @copyright See the included NOTICE.md file for more information.
  24. * @link http://aws.amazon.com/php/ PHP Developer Center
  25. */
  26. class CFJSON
  27. {
  28. /**
  29. * Converts a JSON string to a CFSimpleXML object.
  30. *
  31. * @param string|array $json (Required) Pass either a valid JSON-formatted string, or an associative array.
  32. * @param SimpleXMLElement $xml (Optional) An XML object to add nodes to. Must be an object that is an <code>instanceof</code> a <code>SimpleXMLElement</code> object. If an object is not passed, a new one will be generated using the classname defined for <code>$parser</code>.
  33. * @param string $parser (Optional) The name of the class to use to parse the XML. This class should extend <code>SimpleXMLElement</code>. Has a default value of <code>CFSimpleXML</code>.
  34. * @return CFSimpleXML An XML representation of the data.
  35. */
  36. public static function to_xml($json, SimpleXMLElement $xml = null, $parser = 'CFSimpleXML')
  37. {
  38. // If there isn't an XML object, create one
  39. if (!$xml)
  40. {
  41. $xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><rootElement/>', $parser);
  42. }
  43. // If we haven't parsed the JSON, do it
  44. if (!is_array($json))
  45. {
  46. $json = json_decode($json, true);
  47. if (function_exists('json_last_error'))
  48. {
  49. // Did we encounter an error?
  50. switch (json_last_error())
  51. {
  52. case JSON_ERROR_DEPTH:
  53. throw new JSON_Exception('Maximum stack depth exceeded.');
  54. case JSON_ERROR_CTRL_CHAR:
  55. throw new JSON_Exception('Unexpected control character found.');
  56. case JSON_ERROR_SYNTAX:
  57. throw new JSON_Exception('Syntax error; Malformed JSON.');
  58. case JSON_ERROR_STATE_MISMATCH:
  59. throw new JSON_Exception('Invalid or malformed JSON.');
  60. }
  61. }
  62. else
  63. {
  64. throw new JSON_Exception('Unknown JSON error. Be sure to validate your JSON and read the notes on http://php.net/json_decode.');
  65. }
  66. }
  67. // Hand off for the recursive work
  68. self::process_json($json, $xml, $parser);
  69. return $xml;
  70. }
  71. /**
  72. * Converts a JSON string to a CFSimpleXML object.
  73. *
  74. * @param string|array $json (Required) Pass either a valid JSON-formatted string, or an associative array.
  75. * @param SimpleXMLElement $xml (Optional) An XML object to add nodes to. Must be an object that is an <code>instanceof</code> a <code>SimpleXMLElement</code> object. If an object is not passed, a new one will be generated using the classname defined for <code>$parser</code>.
  76. * @param string $parser (Optional) The name of the class to use to parse the XML. This class should extend <code>SimpleXMLElement</code>. Has a default value of <code>CFSimpleXML</code>.
  77. * @return CFSimpleXML An XML representation of the data.
  78. */
  79. protected static function process_json($json, SimpleXMLElement $xml = null, $parser = 'CFSimpleXML')
  80. {
  81. foreach ($json as $k => $v)
  82. {
  83. if (is_array($v))
  84. {
  85. $node = $xml->addChild($k);
  86. self::process_json($v, $node, $parser);
  87. }
  88. else
  89. {
  90. $xml->addChild($k, $v);
  91. }
  92. }
  93. }
  94. }
  95. /**
  96. * Default JSON Exception.
  97. */
  98. class JSON_Exception extends Exception {}