apidoc
[ class tree: apidoc ] [ index: apidoc ] [ all elements ]

Class: Services_JSON

Source Location: /Services_JSON-1.0.3/JSON.php

Class Overview


Converts to and from JSON format.


Methods


Inherited Variables

Inherited Methods


Class Details

[line 119]
Converts to and from JSON format.

Brief example of use:

  1.  // create a new instance of Services_JSON
  2.  $json = new Services_JSON();
  3.  
  4.  // convert a complexe value to JSON notation, and send it to the browser
  5.  $value = array('foo''bar'array(12'baz')array(3array(4)));
  6.  $output $json->encode($value);
  7.  
  8.  print($output);
  9.  // prints: ["foo","bar",[1,2,"baz"],[3,[4]]]
  10.  
  11.  // accept incoming POST data, assumed to be in JSON notation
  12.  $input = file_get_contents('php://input'1000000);
  13.  $value $json->decode($input);



[ Top ]


Method Detail

Services_JSON (Constructor)   [line 142]

Services_JSON Services_JSON( [int $use = 0])

constructs a new JSON instance

Parameters:

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 it'self, 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.


[ Top ]

decode   [line 582]

mixed decode( string $str)

decodes a JSON string into appropriate variable
  • Return: number, boolean, string, array, or object corresponding to given JSON input string. See argument 1 to Services_JSON() above for object-output behavior. Note that decode() always returns strings in ASCII or UTF-8 format!
  • Access: public

Parameters:

string   $str   —  JSON-formatted string

[ Top ]

encode   [line 253]

mixed encode( mixed $var)

encodes an arbitrary variable into JSON format (and sends JSON Header)
  • Return: JSON string representation of input var or an error if a problem occurs
  • Access: public

Parameters:

mixed   $var   —  any number, boolean, string, array, or object to be encoded. see argument 1 to Services_JSON() above for array-parsing behavior. if var is a strng, note that encode() always expects it to be in ASCII or UTF-8 format!

[ Top ]

encodeUnsafe   [line 269]

mixed encodeUnsafe( mixed $var)

encodes an arbitrary variable into JSON format without JSON Header - warning - may allow XSS!!!!)
  • Return: JSON string representation of input var or an error if a problem occurs
  • Access: public

Parameters:

mixed   $var   —  any number, boolean, string, array, or object to be encoded. see argument 1 to Services_JSON() above for array-parsing behavior. if var is a strng, note that encode() always expects it to be in ASCII or UTF-8 format!

[ Top ]

isError   [line 863]

void isError( $data, [ $code = null])

  • Todo: Ultimately, this should just call PEAR::isError()

Parameters:

   $data   — 
   $code   — 

[ Top ]

strlen8   [line 880]

integer strlen8( string $str)

Calculates length of string in bytes
  • Return: length

Parameters:

string   $str   — 

[ Top ]

substr8   [line 895]

integer substr8( string $string, integer $start, [integer $length = false])

Returns part of a string, interpreting $start and $length as number of bytes.
  • Return: length

Parameters:

string   $string   — 
integer   $start   —  start
integer   $length   —  length

[ Top ]

_encode   [line 290]

mixed _encode( mixed $var)

PRIVATE CODE that does the work of encodes an arbitrary variable into JSON format
  • Return: JSON string representation of input var or an error if a problem occurs
  • Access: public

Parameters:

mixed   $var   —  any number, boolean, string, array, or object to be encoded. see argument 1 to Services_JSON() above for array-parsing behavior. if var is a strng, note that encode() always expects it to be in ASCII or UTF-8 format!

[ Top ]


Documentation generated on Mon, 11 Mar 2019 15:42:23 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.