From 2afbd33472a2b64ccf57767e90427bba6b469be2 Mon Sep 17 00:00:00 2001
From: Niels Lohmann <mail@nlohmann.me>
Date: Wed, 10 May 2017 12:06:24 +0200
Subject: [PATCH] :hammer: working on #367

Test cases succeed as expected, but the example in #367 is not fully
realized yet.
---
 src/json.hpp                 |  4 +--
 test/src/unit-testsuites.cpp | 54 ++++++++++++++++++++++++++++++++++--
 2 files changed, 54 insertions(+), 4 deletions(-)

diff --git a/src/json.hpp b/src/json.hpp
index 14e687d9..62b4cfb7 100644
--- a/src/json.hpp
+++ b/src/json.hpp
@@ -7618,7 +7618,7 @@ class basic_json
     JSON_DEPRECATED
     friend std::istream& operator<<(basic_json& j, std::istream& i)
     {
-        j = parser(input_adapter::create(i)).parse(true);
+        j = parser(input_adapter::create(i)).parse(false);
         return i;
     }
 
@@ -7650,7 +7650,7 @@ class basic_json
     */
     friend std::istream& operator>>(std::istream& i, basic_json& j)
     {
-        j = parser(input_adapter::create(i)).parse(true);
+        j = parser(input_adapter::create(i)).parse(false);
         return i;
     }
 
diff --git a/test/src/unit-testsuites.cpp b/test/src/unit-testsuites.cpp
index 798bc5c7..d3dbaf4b 100644
--- a/test/src/unit-testsuites.cpp
+++ b/test/src/unit-testsuites.cpp
@@ -75,11 +75,29 @@ TEST_CASE("compliance tests from json.org")
                     "test/data/json_tests/fail32.json",
                     "test/data/json_tests/fail33.json"
                 })
+        {
+            CAPTURE(filename);
+            std::ifstream f(filename);
+            CHECK_THROWS_AS(json::parse(f), json::parse_error);
+        }
+    }
+
+    SECTION("no failures with trailing literals (relaxed)")
+    {
+        // these tests fail above, because the parser does not end on EOF;
+        // they succeed when the operator>> is used, because it does not
+        // have this constraint
+        for (auto filename :
+                {
+                    "test/data/json_tests/fail7.json",
+                    "test/data/json_tests/fail8.json",
+                    "test/data/json_tests/fail10.json",
+                })
         {
             CAPTURE(filename);
             std::ifstream f(filename);
             json j;
-            CHECK_THROWS_AS(f >> j, json::parse_error);
+            CHECK_NOTHROW(f >> j);
         }
     }
 
@@ -751,11 +769,43 @@ TEST_CASE("nst's JSONTestSuite")
                         "test/data/nst_json_testsuite/test_parsing/n_structure_whitespace_formfeed.json"
                     }
                 )
+            {
+                CAPTURE(filename);
+                std::ifstream f(filename);
+                CHECK_THROWS_AS(json::parse(f), json::parse_error);
+            }
+        }
+
+        SECTION("n -> y (relaxed)")
+        {
+            // these tests fail above, because the parser does not end on EOF;
+            // they succeed when the operator>> is used, because it does not
+            // have this constraint
+            for (auto filename :
+                    {
+                        "test/data/nst_json_testsuite/test_parsing/n_array_comma_after_close.json",
+                        "test/data/nst_json_testsuite/test_parsing/n_array_extra_close.json",
+                        "test/data/nst_json_testsuite/test_parsing/n_object_trailing_comment.json",
+                        "test/data/nst_json_testsuite/test_parsing/n_object_trailing_comment_open.json",
+                        "test/data/nst_json_testsuite/test_parsing/n_object_trailing_comment_slash_open.json",
+                        "test/data/nst_json_testsuite/test_parsing/n_object_trailing_comment_slash_open_incomplete.json",
+                        "test/data/nst_json_testsuite/test_parsing/n_object_with_trailing_garbage.json",
+                        "test/data/nst_json_testsuite/test_parsing/n_string_with_trailing_garbage.json",
+                        "test/data/nst_json_testsuite/test_parsing/n_structure_array_trailing_garbage.json",
+                        "test/data/nst_json_testsuite/test_parsing/n_structure_array_with_extra_array_close.json",
+                        "test/data/nst_json_testsuite/test_parsing/n_structure_close_unopened_array.json",
+                        "test/data/nst_json_testsuite/test_parsing/n_structure_double_array.json",
+                        "test/data/nst_json_testsuite/test_parsing/n_structure_number_with_trailing_garbage.json",
+                        "test/data/nst_json_testsuite/test_parsing/n_structure_object_followed_by_closing_object.json",
+                        "test/data/nst_json_testsuite/test_parsing/n_structure_object_with_trailing_garbage.json",
+                        "test/data/nst_json_testsuite/test_parsing/n_structure_trailing_#.json"
+                    }
+                )
             {
                 CAPTURE(filename);
                 std::ifstream f(filename);
                 json j;
-                CHECK_THROWS_AS(f >> j, json::parse_error);
+                CHECK_NOTHROW(f >> j);
             }
         }