Write json in an async way and caught errors, fixes #352.