/** * Encode the mixed $valueToEncode into the JSON format * * Encodes using ext/json's json_encode() if available. * * NOTE: Object should not contain cycles; the JSON format * does not allow object reference. * * NOTE: Only public variables will be encoded * * @param mixed $valueToEncode * @param boolean $cycleCheck Optional; whether or not to check for object recursion; off by default * @return string JSON encoded object */ public static function encode($valueToEncode, $cycleCheck = false) { if (function_exists('json_encode') && self::$useBuiltinEncoderDecoder !== true) { return json_encode($valueToEncode); } require_once 'Sopha/Json/Encoder.php'; return Sopha_Json_Encoder::encode($valueToEncode, $cycleCheck); }
public function testEncodeObject() { $actual = new Sopha_JsonTest_Object(); $encoded = Sopha_Json_Encoder::encode($actual); $decoded = Sopha_Json_Decoder::decode($encoded, Sopha_Json::TYPE_OBJECT); $this->assertTrue(isset($decoded->__className)); $this->assertEquals('Sopha_JsonTest_Object', $decoded->__className); $this->assertTrue(isset($decoded->foo)); $this->assertEquals('bar', $decoded->foo); $this->assertTrue(isset($decoded->bar)); $this->assertEquals('baz', $decoded->bar); $this->assertFalse(isset($decoded->_foo)); }