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