diff --git a/src/JSON.cc b/src/JSON.cc
index e7186560..ed2e4949 100644
--- a/src/JSON.cc
+++ b/src/JSON.cc
@@ -22,6 +22,7 @@
 ////////////////////
 
 std::mutex JSON::_token;
+bool JSON::Parser::firstCall = true;
 
 
 ///////////////////////////////////
@@ -1776,9 +1777,17 @@ Initialize the JSON parser given an input stream \p _is.
 */
 JSON::Parser::Parser(std::istream& _is)
 {
-    // from http://www.manticmoo.com/articles/jeff/programming/c++/making-io-streams-efficient-in-c++.php
-    //  Don't sync C++ and C I/O
-    std::ios_base::sync_with_stdio(false);
+    // On first call, switch off syncing between C++ and C I/O. This call must
+    // be done before first I/O operation as the behavior may be undefined
+    // otherwise.
+    if (firstCall)
+    {
+        firstCall = false;
+        //  Don't sync C++ and C I/O
+        // from http://www.manticmoo.com/articles/jeff/programming/c++/making-io-streams-efficient-in-c++.php
+        std::ios_base::sync_with_stdio(false);
+    }
+
     while (_is)
     {
         std::string input_line;
diff --git a/src/JSON.h b/src/JSON.h
index 5d512fc4..14ca857c 100644
--- a/src/JSON.h
+++ b/src/JSON.h
@@ -432,6 +432,10 @@ class JSON
         char _current {};
         /// the position inside the input buffer
         size_t _pos = 0;
+
+      private:
+        /// variable to guard std::ios_base::sync_with_stdio
+        static bool firstCall;
     };
 };