Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make json_encode fail

Tags:

json

php

testing

Trying to test a method that is doing a json_encode and throwing an exception if the encode fails. However, whatever I throw at json_encode it successfully encodes. Any idea what would be something simple to have it fail?

like image 969
Cristian Avatar asked Dec 24 '17 17:12

Cristian


Video Answer


2 Answers

I think the smallest values to fail json_encode would be:

json_encode(NAN);
json_encode(INF);

Other candidate:

$a = array(&$a);
json_encode($a);
like image 141
Salman A Avatar answered Oct 19 '22 09:10

Salman A


$text = "\xB1\x31";

$json  = json_encode($text);

For checking errors you can use json_last_error().

like image 22
V. Panait Avatar answered Oct 19 '22 08:10

V. Panait