API Documentation

Json.php

Includes Classes 
category
Zend
copyright
Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
license
http://framework.zend.com/license/new-bsd New BSD License
package
Zend_Json
version
$Id: Json.php 20616 2010-01-25 19:56:04Z matthew $
Classes
Zend_Json

Description

Zend Framework

LICENSE

This source file is subject to the new BSD license that is bundled with this package in the file LICENSE.txt. It is also available through the world-wide-web at this URL: http://framework.zend.com/license/new-bsd If you did not receive a copy of the license and are unable to obtain it through the world-wide-web, please send an email to license@zend.com so we can send you a copy immediately.

Zend_Json

category
Zend
copyright
Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
license
http://framework.zend.com/license/new-bsd New BSD License
package
Zend_Json
uses
Zend_Json_Expr
Constants
TYPE_ARRAY
TYPE_OBJECT
Properties
$maxRecursionDepthAllowed
$useBuiltinEncoderDecoder
Methods
decode
encode
_recursiveJsonExprFinder
fromXml
_processXml
prettyPrint

Description

Class for encoding to and decoding from JSON.

Constants

TYPE_ARRAY

 TYPE_ARRAY = '1'

How objects should be encoded -- arrays or as StdClass. TYPE_ARRAY is 1 so that it is a boolean true value, allowing it to be used with ext/json's functions.

Details

value
1

TYPE_OBJECT

 TYPE_OBJECT = '0'

Details

value
0

Properties

$maxRecursionDepthAllowed

int $maxRecursionDepthAllowed = '25'

To check the allowed nesting depth of the XML tree during xml2json conversion.

Details

$maxRecursionDepthAllowed
int
visibility
public
default
25
final
false
static
true

$useBuiltinEncoderDecoder

bool $useBuiltinEncoderDecoder = 'false'

Details

$useBuiltinEncoderDecoder
bool
visibility
public
default
false
final
false
static
true

Methods

_processXml

_processXml( SimpleXMLElement $simpleXmlElementObject, boolean $ignoreXmlAttributes, int $recursionDepth = 0 ) : mixed

_processXml - Contains the logic for xml2json

The logic in this function is a recursive one.

The main caller of this function (i.e. fromXml) needs to provide only the first two parameters i.e. the SimpleXMLElement object and the flag for ignoring or not ignoring XML attributes. The third parameter will be used internally within this function during the recursive calls.

This function converts the SimpleXMLElement object into a PHP array by calling a recursive (protected static) function in this class. Once all the XML elements are stored in the PHP array, it is returned to the caller.

Throws a Zend_Json_Exception if the XML tree is deeper than the allowed limit.

Arguments
$simpleXmlElementObject
SimpleXMLElement
XML element to be converted
$ignoreXmlAttributes
boolean
Include or exclude XML attributes in the xml2json conversion process.
$recursionDepth
int
Current recursion depth of this function
Output
mixed
- On success, a PHP associative array of traversed XML elements
Details
visibility
protected
final
false
static
true
access
protected
static
throws

_recursiveJsonExprFinder

_recursiveJsonExprFinder(  $value,  $javascriptExpressions,  $currentKey = null ) : void

Check & Replace Zend_Json_Expr for tmp ids in the valueToEncode

Check if the value is a Zend_Json_Expr, and if replace its value with a magic key and save the javascript expression in an array.

NOTE this method is recursive.

NOTE: This method is used internally by the encode method.

Arguments
$value
$javascriptExpressions
$currentKey
Details
visibility
protected
final
false
static
true
see
encode

decode

decode( string $encodedValue, int $objectDecodeType = Zend_Json ) : mixed

Decodes the given $encodedValue string which is encoded in the JSON format

Uses ext/json's json_decode if available.

Arguments
$encodedValue
string
Encoded in JSON format
$objectDecodeType
int
Optional; flag indicating how to decode objects. See {@link Zend_Json_Decoder::decode()} for details.
Output
mixed
Details
visibility
public
final
false
static
true

encode

encode( mixed $valueToEncode, boolean $cycleCheck = false, array $options = array ) : string

Encode the mixed $valueToEncode into the JSON format

Encodes using ext/json's json_encode() if available.

NOTE: Object should not contain cycles; the JSON format does not allow object reference.

NOTE: Only public variables will be encoded

NOTE: Encoding native javascript expressions are possible using Zend_Json_Expr. You can enable this by setting $options['enableJsonExprFinder'] = true

Arguments
$valueToEncode
mixed
$cycleCheck
boolean
Optional; whether or not to check for object recursion; off by default
$options
array
Additional options used during encoding
Output
string
JSON encoded object
Details
visibility
public
final
false
static
true
see
Zend_Json_Expr

fromXml

fromXml( string $xmlStringContents, boolean $ignoreXmlAttributes = true ) : mixed

fromXml - Converts XML to JSON

Converts a XML formatted string into a JSON formatted string. The value returned will be a string in JSON format.

The caller of this function needs to provide only the first parameter, which is an XML formatted String. The second parameter is optional, which lets the user to select if the XML attributes in the input XML string should be included or ignored in xml2json conversion.

This function converts the XML formatted string into a PHP array by calling a recursive (protected static) function in this class. Then, it converts that PHP array into JSON by calling the "encode" static funcion.

Throws a Zend_Json_Exception if the input not a XML formatted string. NOTE: Encoding native javascript expressions via Zend_Json_Expr is not possible.

Arguments
$xmlStringContents
string
XML String to be converted
$ignoreXmlAttributes
boolean
Include or exclude XML attributes in the xml2json conversion process.
Output
mixed
- JSON formatted string on success
Details
visibility
public
final
false
static
true
access
public
static
throws

prettyPrint

prettyPrint( string $json, array $options = array ) : string

Pretty-print JSON string

Use 'indent' option to select indentation string - by default it's a tab

Arguments
$json
string
Original JSON string
$options
array
Encoding options
Output
string
Details
visibility
public
final
false
static
true
Documentation was generated by DocBlox.