Contactez-nous
Kitpages
17 rue de la Frise
38000 Grenoble
tel : 04 58 00 33 81
JSON en ISO-8859-1 (ou latin1)
Introduction
PHP 5.2 introduit les fonctions json_encode et json_decode pour traiter les conversions de variables PHP en JSON et inversement. Seulement ces fonctions posent quelques problèmes :
- Elles ne fonctionnent qu'en UTF-8. Comme pour l'instant la majorité des gens sont en l'ISO-8859-1, c'est pas hyper pratique
- Elles marchent très bien pour les tableaux, mais pour une chaîne de caractères simple, la fonction json_decode ne fonctionne plus.
Une alternative serait d'utiliser le Zend Framework, mais la classe Zend_Json s'appuie sur les fonctions PHP 5.2 natives (à partir du ZF 0.6.0) et donc a les mêmes limitations.
La classe ci-dessous utilise également les fonctions json_encode et json_decode, mais les encapsule pour ne pas avoir les mêmes limitations. Elle ne fonctionne cependant qu'avec PHP5.2.
Ci-dessous un exemple d'utilisation :
<?php
$tab = array("foo"=>12, "bar");
$json = Kitpages_Json::encode($tab);
$result = Kitpages_Json::decode($json);
?>
Code
Ci-joint le code de la classe.
Fonctionne à partir de PHP 5.2
<?php
/*
* Created on 12 déc. 06
*
* @author Philippe Le Van (Kitpages SARL)
* @copyright 2005-2006
*/
class Kitpages_Json {
////
// singleton management
////
static private $_instance = null;
private function __construct() {
}
static public function getInstance() {
if (!self::$_instance instanceof self) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Decodes the given $encodedValue string which is
* encoded in the JSON format
*
* @param string $encodedValue Encoded in JSON format
* @param boolean
* @return mixed
*/
static public function decode($encodedValue,
$objectDecodeType = true)
{
$char = substr($encodedValue,0,1);
$isValue = false;
if ($char != "{") {
$isValue = true;
$encodedValue = '{"toto":'.$encodedValue.'}';
}
$tab = json_decode($encodedValue, $objectDecodeType);
if ($isValue) {
$tab = $tab["toto"];
}
if (is_array($tab)) {
self::getInstance()->utf8_decode_array($tab);
}
else {
$tab = utf8_decode($tab);
}
return $tab;
}
/**
* Encode the mixed $valueToEncode into the JSON format
*
* @param mixed $valueToEncode
* @return string JSON encoded object
*/
static public function encode($valueToEncode)
{
if (!is_array($valueToEncode)) {
$valueToEncode = utf8_encode($valueToEncode);
}
else {
self::getInstance()->utf8_encode_array($valueToEncode);
}
// return encoded string
return json_encode($valueToEncode);
}
private function utf8_encode_array(&$tab) {
array_walk ($tab, array($this,'_utf8_encode_array'));
}
private function utf8_decode_array(&$tab) {
array_walk ($tab, array($this,'_utf8_decode_array'));
}
private function _utf8_encode_array (&$array, $key) {
if(is_array($array)) {
array_walk ($array, array($this,'_utf8_encode_array'));
} else {
$array = utf8_encode($array);
}
}
private function _utf8_decode_array (&$array, $key) {
if(is_array($array)) {
array_walk ($array, array($this,'_utf8_decode_array'));
} else {
$array = utf8_decode($array);
}
}
}
?>
Conclusion
A priori, une fonction équivalente devrait être disponible dans les prochaines version du Zend Framework, mais en attendant, il faut bien faire nos conversions...

Commentaires
Note : on ne peut plus ajouter de commentaire sur ce site