Class JSONEncoder

Description

Serializes PHP variables using JSON

JSON stands for Javascript Object Notation. It's a lightweight data transfer format, widely used to interchange information between client and server side by the most common AJAX libraries.

Examples:

  1.  // prints true
  2.  print JSONEncoder::encode(TRUE);
  3.  // prints [1,2,3]
  4.  print JSONEncoder::encode(array(123));
  5.  // prints {a: 1, b: "foo", c: true}
  6.  print JSONEncoder::encode(array('a'=>1'b'=>'foo''c'=>TRUE));

Located in /core/util/json/JSONEncoder.class.php (line 52)

PHP2Go
   |
   --JSONEncoder
Variable Summary
array $objRef
Method Summary
static string encode (mixed $value, [bool $throwErrors = TRUE])
static object jsFunction (string $body, [array $inputArgs = array()])
static object jsIdentifier (string $name)
string encodeValue (mixed $value)
string _encodeArray ( &$arr, array $arr)
string _encodeObject ( &$obj, object $obj)
string _encodeString ( &$str, string $str)
bool _wasVisited (object &$obj)
Variables
array $objRef = array() (line 67)

Holds already visited objects, to avoid cyclic references

  • access: private
bool $throwErrors = TRUE (line 59)

Whether errors should be thrown or not

Methods
static method encode (line 86)

Shortcut method to encode a variable

  • return: Generated JSON string
static string encode (mixed $value, [bool $throwErrors = TRUE])
  • mixed $value: Input value
  • bool $throwErrors: Abort execution upon errors or not
static method jsFunction (line 113)

Prepares a Javascript function body to be encoded

static object jsFunction (string $body, [array $inputArgs = array()])
  • string $body: Function body
  • array $inputArgs: Function input arguments
static method jsIdentifier (line 99)

Prepares a Javascript identifier (variable, constant or function) to be encoded

static object jsIdentifier (string $name)
  • string $name: Variable name
Constructor JSONEncoder (line 74)

Class constructor

JSONEncoder JSONEncoder ()
encodeValue (line 134)

Encodes a given value using JSON syntax

Values that can't be encoded will produce a NULL result or throw an error.

  • return: Generated JSON string
string encodeValue (mixed $value)
  • mixed $value: Input value
_encodeArray (line 195)

Encodes a PHP array

Hash arrays are mapped to anonymous objects in JSON. A PHP array is serialized as a JSON array only when all keys are numeric and no gaps are found.

  • return: Serialized array
  • access: private
string _encodeArray ( &$arr, array $arr)
  • array $arr: PHP array
  • &$arr
_encodeObject (line 169)

Encodes a PHP object

  • return: Serialized object
  • access: private
string _encodeObject ( &$obj, object $obj)
  • object $obj: PHP object
  • &$obj
_encodeString (line 221)

Encodes a string

  • access: private
string _encodeString ( &$str, string $str)
  • string $str: PHP string
  • &$str
_wasVisited (line 252)

Checks if an object was already visited

  • access: private
bool _wasVisited (object &$obj)
  • object &$obj: PHP object

Inherited Methods

Inherited From PHP2Go

PHP2Go::PHP2Go()
PHP2Go::equals()
PHP2Go::generateUniqueId()
PHP2Go::getClassName()
PHP2Go::getConfigVal()
PHP2Go::getLangVal()
PHP2Go::getObjectName()
PHP2Go::getParentName()
PHP2Go::hasDestructor()
PHP2Go::hashCode()
PHP2Go::isA()
PHP2Go::isSubclassOf()
PHP2Go::logError()
PHP2Go::raiseError()
PHP2Go::registerDestructor()
PHP2Go::registerShutdownFunc()
PHP2Go::retrieve()
PHP2Go::store()
PHP2Go::__toString()

Documentation generated on Sun, 07 Oct 2007 19:26:50 -0300 by phpDocumentor 1.3.2