makehumancommunity / community-plugins-socket

Socket server for makehuman
7 stars 11 forks source link

Special characters like '\' are not escaped in JsonCall, resulting in invalid JSON #7

Closed saharNooby closed 3 years ago

saharNooby commented 3 years ago

JsonCall.serialize produces invalid JSON. Specifically, reverse slashes in strings \ are not escaped.

Reproducing:

This prevents working with MakeHuman socket using libraries like GSON.

I don't know whether the fix will break Blender plugin compatibility, so won't do a pull request.

Ugly workaround for clients until fix (or if fix is not possible):

// s is MakeHuman response
s = s.replace("\\", "\\\\").replace("\\\\\"", "\\\"");
joepal1976 commented 3 years ago

Yes, very true. We should probably switch to using forward slashes on windows too as that works perfectly fine in python.

Aranuvir commented 3 years ago

Sounds more like a mhapi problem. I've tested default json.dumps (on Linux) and IMHO it should do the escaping correctly, even on a Windows machine.

Quote from mhapi:

Why is the encoding routine even necessary? Why not simply use the json.dumps function already available in the imported json library?

Largely, this is because we want to format the floats with eight decimals no matter where in the hierarchy they appear, including converting strings containing numbers into real numbers.

Not sure if there isn't a better solution for that. I'd recommend to use the JSONEncoder class. See the official docs (https://docs.python.org/3/library/json.html): Extending JSONEncoder.

Aranuvir commented 3 years ago

I've provided a new version of version of JsonCall using the standard json library. You need to update mhapi. It's in the Aranuvir branch. Hopefully it does the string encoding/escaping correctly, now. I did not test it on Windows, yet! Perhaps someone could do this for me. Thanks.

Aranuvir commented 3 years ago

@OP: We have updated mhapi, does this fix your issue? Can we close?

saharNooby commented 3 years ago

I'll test it today

saharNooby commented 3 years ago

Works good with new JsonCall.py, reverse slashes are escaped. Thanks!