I’m working on building two programs, one in Python and one in Processing, to communicate over socket TCP / data stream.
75% is working perfectly:
- Encoding JSON and sending over socket from Processing
- Receiving message and decoding JSON in Python
- Encoding JSON and sending over socket from Python
What is giving me trouble is parseJSONObject() in Processing:
Python originally builds the following string:
Python
msgPayload = {}
msgPayload['dataReq'] = True
msgPayload['captureCount'] = commHandler.imageCaptureCount
msgPayload['flatHash'] = commHandler.hashFlat
msgPayload['tbBlue'] = commHandler.tbDialogue
msgPayload['tbGrey'] = commHandler.tbGrey
msgPayload['tbFight'] = commHandler.tbFight
jsonObj = jsonpickle.encode(msgPayload)
jsonDumpStr = jsonpickle.dumps(jsonObj)
print(jsonDumpStr)
"{\"dataReq\": true, \"captureCount\": 0, \"flatHash\": false, \"tbBlue\": false, \"tbGrey\": false, \"tbFight\": false}"
However, attempting parseJSONObject() on the received string failed because:
Processing
"{\"dataReq\": true, \"captureCount\": 0, \"flatHash\": false, \"tbBlue\": false, \"tbGrey\": false, \"tbFight\": false}"
java.lang.RuntimeException: A JSONObject text must begin with '{'
So, I trimmed the string in python to remove the quotes from the encoded bytes:
Python
jsonDumpStrTrimmed = jsonDumpStr[1:(len(jsonDumpStr)-1)]
jsonDumpBytes = jsonDumpStr.encode('UTF-8')
This still results in a failed pasreJSONObject() in Processing because:
Processing
if (myClient.available() > 0) {
raw = myClient.readString();
print(raw);
JSONObject json = parseJSONObject(raw);
{\"dataReq\": true, \"captureCount\": 0, \"flatHash\": false, \"tbBlue\": false, \"tbGrey\": false, \"tbFight\": false}
java.lang.RuntimeException: Missing value
I must be missing something reaaaally fundamental because I don’t see any missing values or foobar JSON encoding. Help?