Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 * Copyright 2010-2012 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
 
18
/*%******************************************************************************************%*/
19
// CLASS
20
 
21
/**
22
 * Handles the conversion of data from JSON to other formats.
23
 *
24
 * @version 2012.01.27
25
 * @license See the included NOTICE.md file for more information.
26
 * @copyright See the included NOTICE.md file for more information.
27
 * @link http://aws.amazon.com/php/ PHP Developer Center
28
 */
29
class CFJSON
30
{
31
	/**
32
	 * Converts a JSON string to a CFSimpleXML object.
33
	 *
34
	 * @param string|array $json (Required) Pass either a valid JSON-formatted string, or an associative array.
35
	 * @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>.
36
	 * @return CFSimpleXML An XML representation of the data.
37
	 */
38
	public static function to_xml($json, $parser = 'CFSimpleXML')
39
	{
40
		// If we haven't parsed the JSON, do it
41
		if (!is_array($json))
42
		{
43
			// Handle the case of JSON-encoded NULL value
44
			if ($json === 'null')
45
			{
46
				$json = null;
47
			}
48
			else
49
			{
50
				$json = json_decode($json, true);
51
 
52
				if (function_exists('json_last_error'))
53
				{
54
					// Did we encounter an error?
55
					switch (json_last_error())
56
					{
57
						case JSON_ERROR_DEPTH:
58
							throw new JSON_Exception('Maximum stack depth exceeded.');
59
 
60
						case JSON_ERROR_CTRL_CHAR:
61
							throw new JSON_Exception('Unexpected control character found.');
62
 
63
						case JSON_ERROR_SYNTAX:
64
							throw new JSON_Exception('Syntax error; Malformed JSON.');
65
 
66
						case JSON_ERROR_STATE_MISMATCH:
67
							throw new JSON_Exception('Invalid or malformed JSON.');
68
					}
69
				}
70
				// json_last_error() not available?
71
				elseif ($json === null)
72
				{
73
					throw new JSON_Exception('Unknown JSON error. Be sure to validate your JSON and read the notes on http://php.net/json_decode.');
74
				}
75
			}
76
		}
77
 
78
		// Hand off for the recursive work
79
		$string = Array2DOM::arrayToXMLString($json, 'rootElement', true);
80
 
81
		return simplexml_load_string($string, $parser, LIBXML_NOCDATA);
82
	}
83
}
84
 
85
 
86
/**
87
 * Default JSON Exception.
88
 */
89
class JSON_Exception extends Exception {}