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

Class: Services_JSON

Source Location: /communication/Services_JSON.class.php

Class Overview


Converts to and from JSON format.


Methods



Class Details

[line 116]
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 ]


Class Methods


constructor Services_JSON [line 134]

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()


[ Top ]

method decode [line 485]

mixed decode( string $str)

decodes a JSON string into appropriate variable



Tags:

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 ]

method encode [line 238]

mixed encode( mixed $var)

encodes an arbitrary variable into JSON format



Tags:

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 ]

method isError [line 767]

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



Tags:

todo:  Ultimately, this should just call PEAR::isError()


Parameters:

   $data  
   $code  

[ Top ]

method name_value [line 435]

string name_value( string $name, mixed $value)

array-walking function for use in generating JSON-formatted name-value pairs



Tags:

return:  JSON-formatted name-value pair, like '"name":value'
access:  private


Parameters:

string   $name   name of key to use
mixed   $value   reference to an array element to be encoded

[ Top ]

method reduce_string [line 454]

string reduce_string( $str $str)

reduce a string by removing leading and trailing comments and whitespace



Tags:

return:  string value stripped of comments and whitespace
access:  private


Parameters:

$str   $str   string string value to strip of comments and whitespace

[ Top ]

method utf82utf16 [line 194]

string utf82utf16( string $utf8)

convert a string from one UTF-8 char to one UTF-16 char

Normally should be handled by mb_convert_encoding, but provides a slower PHP-only method for installations that lack the multibye string extension.




Tags:

return:  UTF-16 character
access:  private


Parameters:

string   $utf8   UTF-8 character

[ Top ]

method utf162utf8 [line 150]

string utf162utf8( string $utf16)

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.




Tags:

return:  UTF-8 character
access:  private


Parameters:

string   $utf16   UTF-16 character

[ Top ]


Documentation generated on Thu, 05 Jun 2008 19:15:04 +0200 by phpDocumentor 1.4.1