Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
json.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Registry
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * JSON format handler for JRegistry.
14  *
15  * @package Joomla.Platform
16  * @subpackage Registry
17  * @since 11.1
18  */
20 {
21  /**
22  * Converts an object into a JSON formatted string.
23  *
24  * @param object $object Data source object.
25  * @param array $options Options used by the formatter.
26  *
27  * @return string JSON formatted string.
28  *
29  * @since 11.1
30  */
31  public function objectToString($object, $options = array())
32  {
33  return json_encode($object);
34  }
35 
36  /**
37  * Parse a JSON formatted string and convert it into an object.
38  *
39  * If the string is not in JSON format, this method will attempt to parse it as INI format.
40  *
41  * @param string $data JSON formatted string to convert.
42  * @param array $options Options used by the formatter.
43  *
44  * @return object Data object.
45  *
46  * @since 11.1
47  */
48  public function stringToObject($data, array $options = array('processSections' => false))
49  {
50  $data = trim($data);
51 
52  if ((substr($data, 0, 1) != '{') && (substr($data, -1, 1) != '}'))
53  {
54  $ini = JRegistryFormat::getInstance('INI');
55  $obj = $ini->stringToObject($data, $options);
56  }
57  else
58  {
59  $obj = json_decode($data);
60  }
61 
62  return $obj;
63  }
64 }