Why can't I read a JSON file with a different program while my sketch is still open?

edited August 2015 in How To...

I'm writing data to a JSON file in Processing with the saveJSONObject command. I would like to access that JSON file with another program (MAX/MSP) while my sketch is still open. The problem is, MAX is unable to read from the file while my sketch is running. Only after I close the sketch is MAX able to import data from my file.

Is Processing keeping that file open somehow while the sketch is running? Is there any way I can get around this problem?

Answers

  • edited August 2015 Answer ✓

    It is called file locking.
    I looked at the source, and there might be a bug, as saveJSONObject doesn't close the writer. Perhaps I miss something.
    Possible workaround (untested): replace the call

    saveJSONObject(json, filename, options);
    

    with

    PrintWriter writer = PApplet.createWriter(saveFile(filename));
    json.write(writer, options);
    writer.close();
    

    which does the same operations, but exposes the writer so that we can explicitly close it.

  • Thanks, your workaround works perfectly.

  • And it has been fixed, it was quick... To be seen in next beta, I suppose.

  • Great! Thanks for going to the trouble.

Sign In or Register to comment.