I think you should consider reprogramming your own preprocessing function, which goes through each character of the JSON string, looking for characters that are not part of your legal set, and either removes or replaces them with a space. Then pass the newly restored string forward to RapidJSON.
It is probably better to detect when you had problems with the message in the first place (and therefore, JSON may be incomplete and / or incorrect) and throw away and repeat the entire session, and not "correct" the data as you want here, which solves you a short-term problem (program failure), but can easily generate data inconsistencies and other more subtle and difficult to diagnose problems.
Also, if you see mostly bad data at the end of the line, like this, I think you should carefully check that your problem is actually related to comm - the case you give here is more like a string buffer that was not correctly completed and has extra garbage (uninitialized memory) after the end of the line - perhaps you expected C ++ to clear (set to zero) the allocated buffer?
source share