wppaste
WordPress

Services_JSON

Source
wp-includes/class-json.php:122
Converts to and from JSON format.

Description

Brief example of use:

// create a new instance of Services_JSON
$json = new Services_JSON();

// convert a complex value to JSON notation, and send it to the browser $value = array('foo', 'bar', array(1, 2, 'baz'), array(3, array(4))); $output = $json->encode($value);

print($output); // prints: ["foo","bar",[1,2,"baz"],[3,[4]]]

// accept incoming POST data, assumed to be in JSON notation $input = file_get_contents('php://input', 1000000); $value = $json->decode($input);

Compatibility

WordPress
core
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0).

Properties · 4

$useintpublic
Object behavior flags.
$_mb_strlenpublic
$_mb_substrpublic
$_mb_convert_encodingpublic

Methods · 13

  • __construct()constructs a new JSON instance
  • Services_JSON()PHP4 constructor.
  • utf162utf8()convert a string from one UTF-16 char to one UTF-8 char
  • utf82utf16()convert a string from one UTF-8 char to one UTF-16 char
  • encode()encodes an arbitrary variable into JSON format (and sends JSON Header)
  • encodeUnsafe()encodes an arbitrary variable into JSON format without JSON Header - warning - may allow XSS!!!!)
  • _encode()PRIVATE CODE that does the work of encodes an arbitrary variable into JSON format
  • name_value()array-walking function for use in generating JSON-formatted name-value pairs
  • reduce_string()reduce a string by removing leading and trailing comments and whitespace
  • decode()decodes a JSON string into appropriate variable
  • isError()
  • strlen8()Calculates length of string in bytes
  • substr8()Returns part of a string, interpreting $start and $length as number of bytes.

Source code

class Services_JSON{   /**    * Object behavior flags.    *    * @var int    */    public $use;     // private - cache the mbstring lookup results..    var $_mb_strlen = false;    var $_mb_substr = false;    var $_mb_convert_encoding = false;    /**    * constructs a new JSON instance    *    * @deprecated 5.3.0 Use the PHP native JSON extension instead.    *    * @param    int     $use    object behavior flags; combine with boolean-OR    *    *                           possible values:    *                           - SERVICES_JSON_LOOSE_TYPE:  loose typing.    *                                   "{...}" syntax creates associative arrays    *                                   instead of objects in decode().    *                           - SERVICES_JSON_SUPPRESS_ERRORS:  error suppression.    *                                   Values which can't be encoded (e.g. resources)    *                                   appear as NULL instead of throwing errors.    *                                   By default, a deeply-nested resource will    *                                   bubble up with an error, so all return values    *                                   from encode() should be checked with isError()    *                           - SERVICES_JSON_USE_TO_JSON:  call toJSON when serializing objects    *                                   It serializes the return value from the toJSON call rather     *                                   than the object itself, toJSON can return associative arrays,     *                                   strings or numbers, if you return an object, make sure it does    *                                   not have a toJSON method, otherwise an error will occur.    */    function __construct( $use = 0 )    {        _deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );         $this->use = $use;        $this->_mb_strlen            = function_exists('mb_strlen');        $this->_mb_convert_encoding  = function_exists('mb_convert_encoding');        $this->_mb_substr            = function_exists('mb_substr');    }     /**     * PHP4 constructor.     *     * @deprecated 5.3.0 Use __construct() instead.     *     * @see Services_JSON::__construct()     */    public function Services_JSON( $use = 0 ) {        _deprecated_constructor( 'Services_JSON', '5.3.0', get_class( $this ) );        self::__construct( $use );    }    /**    * convert a string from one UTF-16 char to one UTF-8 char    *    * Normally should be handled by mb_convert_encoding, but    * provides a slower PHP-only method for installations    * that lack the multibye string extension.    *    * @deprecated 5.3.0 Use the PHP native JSON extension instead.    *    * @param    string  $utf16  UTF-16 character    * @return   string  UTF-8 character    * @access   private    */    function utf162utf8($utf16)    {        _deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );         // oh please oh please oh please oh please oh please        if($this->_mb_convert_encoding) {            return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');        }

Changelog

Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-json.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.