JOOMLA中国
  • Joomla中国首页
  • 社区
  • 教程
  • 应用市场
  • B计划
Joomla! Framework TM
  • Namespace
  • Class
  • Tree
  • Deprecated

Namespaces

  • Composer
    • Autoload
  • Joomla
    • Application
      • Cli
        • Output
          • Processor
      • Web
    • Data
    • DI
      • Exception
    • Event
    • Filter
    • Input
    • Ldap
    • Registry
      • Format
    • Session
      • Storage
    • String
    • Uri
    • Utilities
  • None
  • PasswordCompat
    • binary
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Yaml
        • Exception
    • Polyfill
      • Util

Classes

  • Ini
  • Json
  • Php
  • Xml
  • Yaml
 1 <?php
 2 /**
 3  * Part of the Joomla Framework Registry Package
 4  *
 5  * @copyright  Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
 6  * @license    GNU General Public License version 2 or later; see LICENSE
 7  */
 8 
 9 namespace Joomla\Registry\Format;
10 
11 use Joomla\Registry\AbstractRegistryFormat;
12 
13 /**
14  * JSON format handler for Registry.
15  *
16  * @since  1.0
17  */
18 class Json extends AbstractRegistryFormat
19 {
20     /**
21      * Converts an object into a JSON formatted string.
22      *
23      * @param   object  $object   Data source object.
24      * @param   array   $options  Options used by the formatter.
25      *
26      * @return  string  JSON formatted string.
27      *
28      * @since   1.0
29      */
30     public function objectToString($object, $options = array())
31     {
32         $bitmask = isset($options['bitmask']) ? $options['bitmask'] : 0;
33 
34         // The depth parameter is only present as of PHP 5.5
35         if (version_compare(PHP_VERSION, '5.5', '>='))
36         {
37             $depth = isset($options['depth']) ? $options['depth'] : 512;
38 
39             return json_encode($object, $bitmask, $depth);
40         }
41 
42         return json_encode($object, $bitmask);
43     }
44 
45     /**
46      * Parse a JSON formatted string and convert it into an object.
47      *
48      * If the string is not in JSON format, this method will attempt to parse it as INI format.
49      *
50      * @param   string  $data     JSON formatted string to convert.
51      * @param   array   $options  Options used by the formatter.
52      *
53      * @return  object   Data object.
54      *
55      * @since   1.0
56      * @throws  \RuntimeException
57      */
58     public function stringToObject($data, array $options = array('processSections' => false))
59     {
60         $data = trim($data);
61 
62         if ((substr($data, 0, 1) != '{') && (substr($data, -1, 1) != '}'))
63         {
64             return AbstractRegistryFormat::getInstance('Ini')->stringToObject($data, $options);
65         }
66 
67         $decoded = json_decode($data);
68 
69         // Check for an error decoding the data
70         if ($decoded === null)
71         {
72             throw new \RuntimeException(sprintf('Error decoding JSON data: %s', json_last_error_msg()));
73         }
74 
75         return $decoded;
76     }
77 }
78 
Joomla! Framework TM API documentation generated by ApiGen 2.8.0
Joomla!® and Joomla! Framework™ are trademarks of Open Source Matters, Inc. in the United States and other countries.