Created
November 19, 2015 09:19
-
-
Save wudi/48fb40ede64dfda2a537 to your computer and use it in GitHub Desktop.
Test Json Bug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* lvcha. | |
* | |
* Author: wudi <[email protected]> | |
* Date: 2015/11/19 | |
*/ | |
class Person implements JsonSerializable | |
{ | |
protected $name = 'eagle'; | |
protected $detail = ' | |
详细内容232432 | |
<div class="detail-mini-row"><img src="/upload/39/8e/56318e318bf39.jpg"><div class="detail-mini-edit fn-clear" style="display: none;"><div class="detail-mini-edit-left"><button type="button" class="btn btn-up btn-primary">上移</button><button type="button" class="btn btn-down btn-primary">下移</button></div><div class="detail-mini-edit-right"><button type="button" class="btn btn-del btn-primary"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button></div></div></div><div class="detail-mini-row"><div class="detail-mini-txt">测试文字手打点撒 | |
撒旦撒旦</div><div class="detail-mini-edit fn-clear" style="display: none;"><div class="detail-mini-edit-left"><button type="button" class="btn btn-up btn-primary">上移</button><button type="button" class="btn btn-down btn-primary">下移</button></div><div class="detail-mini-edit-right"><button type="button" class="btn btn-del btn-primary"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button></div></div></div>'; | |
public function jsonSerialize() | |
{ | |
return $this->toArray(); | |
} | |
public function toArray() | |
{ | |
/** | |
* The attribute detail is invalid json string. | |
* Trigger json error, But no fatal error just return NULL. | |
*/ | |
$this->detail = json_decode($this->detail, true); | |
/** | |
* valid array return, only detail is NULL. | |
*/ | |
$arr = [ | |
'name' => $this->name, | |
'detail' => $this->detail, | |
]; | |
echo "\nAfter json_decode() return valid array: \n"; | |
var_dump($arr); | |
return $arr; | |
} | |
} | |
$person = new Person(); | |
/** | |
* Why return false ????? | |
*/ | |
$json = json_encode($person); | |
echo PHP_EOL . 'json_encode() return:' . PHP_EOL; | |
var_dump($json); | |
echo PHP_EOL . json_last_error_msg() . ':' . json_last_error() . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment