Add debug_bad_encoding function for cases of broken json encoding

Trying to debug #27
Code taken from #15