Answering myself 'cause I found a solution to this problem even if I am unsure about what caused it in the first place.
Anyway, I found out that if I do a base64 encoding of the binary data before sending it as json, the json object gets properly parsed by the PHP script, and doing a base64 decoding successfully recreates the original document created by the servlet.