public function testEncoder() { $this->assertEquals('1', CopixJSONEncoder::encode(1)); $this->assertEquals('-1', CopixJSONEncoder::encode(-1)); $this->assertEquals('true', CopixJSONEncoder::encode(true)); $this->assertEquals('false', CopixJSONEncoder::encode(false)); $this->assertEquals('null', CopixJSONEncoder::encode(null)); $this->assertEquals('1.0', CopixJSONEncoder::encode(1.0)); $this->assertEquals('-1.0', CopixJSONEncoder::encode(-1.0)); $this->assertEquals('"abc"', CopixJSONEncoder::encode('abc')); $this->assertEquals('"a\\nbc"', CopixJSONEncoder::encode("a\nbc")); $this->assertEquals('"a\\tbc"', CopixJSONEncoder::encode("a\tbc")); $this->assertEquals('"a\\rbc"', CopixJSONEncoder::encode("a\rbc")); $this->assertEquals('"a\\fbc"', CopixJSONEncoder::encode("a\fbc")); $this->assertEquals('"a\\bbc"', CopixJSONEncoder::encode("abc")); $this->assertEquals('"a\\"bc"', CopixJSONEncoder::encode("a\"bc")); $this->assertEquals('"a\\/bc"', CopixJSONEncoder::encode("a/bc")); $this->assertEquals('"a\\\\bc"', CopixJSONEncoder::encode('a\\bc')); $this->assertEquals('"aéc"', CopixJSONEncoder::encode('aéc')); $this->assertEquals('"a\\u00E9c"', CopixJSONEncoder::encode("aéc")); $this->assertEquals('[]', CopixJSONEncoder::encode(array())); $this->assertEquals('{}', CopixJSONEncoder::encode(new stdClass())); $this->assertEquals('[4,"a",true]', CopixJSONEncoder::encode(array(4, "a", true))); $this->assertEquals('{"b":4,"5":"X","c":true}', CopixJSONEncoder::encode(array("b" => 4, 5 => "X", "c" => true))); $o = new stdClass(); $o->b = 4; $o->c = "X"; $o->d = true; $this->assertEquals('{"b":4,"c":"X","d":true}', CopixJSONEncoder::encode($o)); $this->assertEquals('[4,[5,6],7,false]', CopixJSONEncoder::encode(array(4, array(5, 6), 7, false))); $o = new stdClass(); $o->o = new stdClass(); $o->a = array(5, 6); $this->assertEquals('{"o":{},"a":[5,6]}', CopixJSONEncoder::encode($o)); // Récursion infinie $o = new stdClass(); $o->o = $o; try { CopixJSONEncoder::encode($o); $this->fail("CopixJSONEncoderException expected"); } catch (CopixJSONEncoderException $e) { // OK } // Donnée impossible à encoder $f = fopen(__FILE__, "r"); try { CopixJSONEncoder::encode($f); fclose($f); $this->fail("CopixJSONEncoderException expected"); } catch (CopixJSONEncoderException $e) { fclose($f); // OK } }
function json_encode($pValue) { try { return CopixJSONEncoder::encode($pValue); } catch (CopixJSONEncoderException $e) { return false; } }
/** * Génère une instruction "return". * * @param mixed $pValue Valeur à retourner. */ public function return_($pValue) { $this->addStatement_('return ' . CopixJSONEncoder::encode($pValue)); }