Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writer does not flush when writing standalone values #684

Closed
ludocode opened this issue Jul 16, 2016 · 1 comment
Closed

Writer does not flush when writing standalone values #684

ludocode opened this issue Jul 16, 2016 · 1 comment

Comments

@ludocode
Copy link
Contributor

Writer flushes its output stream when it ends the top-level object or array (i.e. in EndObject() or EndArray() when the stack is empty.) However, as of the new JSON spec, there is no longer a requirement that a "JSON text" start with an object or array. A standalone value such as 5 or "HI" is a valid JSON text:

https://tools.ietf.org/html/rfc7158

o Changed the definition of "JSON text" so that it can be any JSON
value, removing the constraint that it be an object or array.

When a Writer is used with a FileWriteStream to write a standalone value, it does not flush, so the value is discarded instead of being written to the file.

This can be partly fixed by adding a destructor to FileWriteStream so that it always flushes when destroyed:

diff --git a/include/rapidjson/filewritestream.h b/include/rapidjson/filewritestream.h
index 6378dd6..d90f0a5 100644
--- a/include/rapidjson/filewritestream.h
+++ b/include/rapidjson/filewritestream.h
@@ -37,6 +37,10 @@ public:
         RAPIDJSON_ASSERT(fp_ != 0);
     }

+    ~FileWriteStream() {
+        Flush();
+    }
+
     void Put(char c) { 
         if (current_ >= bufferEnd_)
             Flush();

However this would only fix FileWriteStream. If a Writer is connected to a socket for example, it won't flush in between writing standalone values. If you want to preserve the "automatic flushing" in between objects written to a stream, you may need to add a flush check to all value write functions, but this could adversely affect performance.

Alternatively, if you're comfortable breaking a bit of backwards compatibility for people using RapidJSON for RPC, you could remove all automatic flushing from the writer and use only destructors on the underlying streams to flush. It would then be up to users to flush manually in between writing objects if they want them flushed to a socket for example.

@ludocode
Copy link
Contributor Author

Thanks 👍

dand-oss pushed a commit to dand-oss/rapidjson that referenced this issue Jul 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant