library = JSON_NATIVE; } } /** * Converts incoming object to UTF-8 for json_encode * */ private function json_fix_charset($var) { if (is_array($var)) { $new = array(); foreach ($var as $k => $v) { $new[$this->json_fix_charset($k)] = $this->json_fix_charset($v); } $var = $new; } elseif (is_object($var)) { $vars = get_object_vars(get_class($var)); foreach ($vars as $m => $v) { $var->$m = $this->json_fix_charset($v); } } elseif (is_string($var)) { $var = utf8_encode($var); } return $var; } /** * savely encode object to JSON, * converting to UTF-8 */ public function save_encode($anObject){ $e = new mb_notice("converting to UTF-8 before using native JSON"); return $this->encode($this->json_fix_charset($anObject)); } /** * Encodes an object to JSON */ public function encode($anObject) { if ($this->library == JSON_PEAR) { $pear = new Services_JSON(); $e = new mb_notice("using PEAR JSON"); return $pear->encode($anObject); } $e = new mb_notice("using native JSON"); return json_encode($anObject); } /** * Decodes a JSON string */ public function decode($aString) { if ($this->library == JSON_PEAR) { $pear = new Services_JSON(); $e = new mb_notice("using PEAR JSON"); return $pear->decode($aString); } $e = new mb_notice("using native JSON"); return json_decode($aString); } } ?>