{"action":"created","issue":{"url":"https://api.github.com/repos/nlohmann/json/issues/367","repository_url":"https://api.github.com/repos/nlohmann/json","labels_url":"https://api.github.com/repos/nlohmann/json/issues/367/labels{/name}","comments_url":"https://api.github.com/repos/nlohmann/json/issues/367/comments","events_url":"https://api.github.com/repos/nlohmann/json/issues/367/events","html_url":"https://github.com/nlohmann/json/issues/367","id":191383970,"number":367,"title":"Behavior of operator>> should more closely resemble that of built-in overloads.","user":{"login":"TurpentineDistillery","id":23583218,"avatar_url":"https://avatars.githubusercontent.com/u/23583218?v=3","gravatar_id":"","url":"https://api.github.com/users/TurpentineDistillery","html_url":"https://github.com/TurpentineDistillery","followers_url":"https://api.github.com/users/TurpentineDistillery/followers","following_url":"https://api.github.com/users/TurpentineDistillery/following{/other_user}","gists_url":"https://api.github.com/users/TurpentineDistillery/gists{/gist_id}","starred_url":"https://api.github.com/users/TurpentineDistillery/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TurpentineDistillery/subscriptions","organizations_url":"https://api.github.com/users/TurpentineDistillery/orgs","repos_url":"https://api.github.com/users/TurpentineDistillery/repos","events_url":"https://api.github.com/users/TurpentineDistillery/events{/privacy}","received_events_url":"https://api.github.com/users/TurpentineDistillery/received_events","type":"User","site_admin":false},"labels":[{"id":232252782,"url":"https://api.github.com/repos/nlohmann/json/labels/confirmed","name":"confirmed","color":"e11d21","default":false},{"id":304694104,"url":"https://api.github.com/repos/nlohmann/json/labels/state:%20waiting%20/%20please%20discuss","name":"state: waiting / please discuss","color":"fbca04","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-11-23T21:30:35Z","updated_at":"2016-11-24T21:07:19Z","closed_at":null,"body":"Basically:\r\n* eat up leading blanks or newlines\r\n* leave the stream where the parsing ended, allowing for more data downstream, which may or may not be json. \r\n\r\n```c++\r\n#include \"json.hpp\"\r\n    \r\ntemplate<typename T>\r\nstatic void test(const std::string& s)\r\n{\r\n    T val{999};\r\n    std::stringstream sstr(s);\r\n    //sstr.exceptions(std::ifstream::failbit | std::ifstream::badbit);\r\n\r\n    try {\r\n        size_t i = 0;\r\n        while(sstr >> val) {\r\n            std::cerr << \"    result[\" << i++ << \"]=\" << val \r\n                      << \"; streampos=\" << sstr.tellg() << std::endl;\r\n        }   \r\n\r\n        std::cerr << \"    final values: result=\" << val \r\n                  << \" i=\" << i\r\n                  << \" streampos=\" << sstr.tellg()\r\n                  << \" streamflags=\" << sstr.flags() \r\n                  << std::endl;\r\n    } catch(std::exception& e) {\r\n        std::cerr << \"    std::exception:\" << e.what() << std::endl;\r\n    }   \r\n\r\n    std::cerr << std::endl;\r\n}\r\n\r\nint main()\r\n{\r\n    using json = nlohmann::json;\r\n    size_t i = 0;\r\n\r\n    for(std::string s : { \r\n        \"\", \r\n        \"   \",\r\n        \"111\",\r\n        \"222 \\t\\n\",\r\n        \"\\n\\t 333\", \r\n        \" 111 \\n222\\n \\n  333\" })\r\n    {   \r\n        std::cerr << \"\\nTest-case[\" << i++ << \"]: '\" << s << \"'\" << std::endl;\r\n    \r\n        std::cerr << \"With int:\" << std::endl;\r\n        test<int>(s);\r\n\r\n        std::cerr << \"With json:\" << std::endl;\r\n        test<json>(s);\r\n\r\n        std::cerr << \"----\" << std::endl;\r\n    }   \r\n\r\n    return 0;\r\n}\r\n```\r\n\r\n```\r\nTest-case[0]: ''\r\nWith int:\r\n    final values: result=999 i=0 streampos=-1 streamflags=4098\r\n\r\nWith json:\r\n    std::exception:parse error - unexpected end of input\r\n\r\n----\r\n\r\nTest-case[1]: '   '\r\nWith int:\r\n    final values: result=999 i=0 streampos=-1 streamflags=4098\r\n\r\nWith json:\r\n    std::exception:parse error - unexpected end of input\r\n\r\n----\r\n\r\nTest-case[2]: '111'\r\nWith int:\r\n    result[0]=111; streampos=-1\r\n    final values: result=111 i=1 streampos=-1 streamflags=4098\r\n\r\nWith json:\r\n    result[0]=111; streampos=-1\r\n    std::exception:basic_string::append\r\n\r\n----\r\n\r\nTest-case[3]: '222 \t\r\n'\r\nWith int:\r\n    result[0]=222; streampos=3\r\n    final values: result=222 i=1 streampos=-1 streamflags=4098\r\n\r\nWith json:\r\n    final values: result=222 i=0 streampos=-1 streamflags=4098\r\n\r\n----\r\n\r\nTest-case[4]: '\r\n\t 333'\r\nWith int:\r\n    result[0]=333; streampos=-1\r\n    final values: result=333 i=1 streampos=-1 streamflags=4098\r\n\r\nWith json:\r\n    result[0]=333; streampos=-1\r\n    std::exception:basic_string::append\r\n\r\n----\r\n\r\nTest-case[5]: ' 111 \r\n222\r\n \r\n  333'\r\nWith int:\r\n    result[0]=111; streampos=4\r\n    result[1]=222; streampos=9\r\n    result[2]=333; streampos=-1\r\n    final values: result=333 i=3 streampos=-1 streamflags=4098\r\n\r\nWith json:\r\n    std::exception:parse error - unexpected number literal; expected end of input\r\n\r\n----\r\n```"},"comment":{"url":"https://api.github.com/repos/nlohmann/json/issues/comments/262844685","html_url":"https://github.com/nlohmann/json/issues/367#issuecomment-262844685","issue_url":"https://api.github.com/repos/nlohmann/json/issues/367","id":262844685,"user":{"login":"TurpentineDistillery","id":23583218,"avatar_url":"https://avatars.githubusercontent.com/u/23583218?v=3","gravatar_id":"","url":"https://api.github.com/users/TurpentineDistillery","html_url":"https://github.com/TurpentineDistillery","followers_url":"https://api.github.com/users/TurpentineDistillery/followers","following_url":"https://api.github.com/users/TurpentineDistillery/following{/other_user}","gists_url":"https://api.github.com/users/TurpentineDistillery/gists{/gist_id}","starred_url":"https://api.github.com/users/TurpentineDistillery/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TurpentineDistillery/subscriptions","organizations_url":"https://api.github.com/users/TurpentineDistillery/orgs","repos_url":"https://api.github.com/users/TurpentineDistillery/repos","events_url":"https://api.github.com/users/TurpentineDistillery/events{/privacy}","received_events_url":"https://api.github.com/users/TurpentineDistillery/received_events","type":"User","site_admin":false},"created_at":"2016-11-24T21:07:19Z","updated_at":"2016-11-24T21:07:19Z","body":"> An empty input is not a valid JSON document.\r\n\r\nAn empty input is not a valid integer either, yet cin>>i will not throw - it will consume the blanks and leave i unchanged.\r\n\r\nThere are two classes of use-cases:\r\n* Parse a singular json document from a string (e.g. using parse(...) method or a free function). Here the library should verify that the input is not empty and that there's nothing past the end.\r\n* Parse next json document from a stream (operator>>). Here the code cannot assume that the stream must contain a singleton json document and nothing else. Maybe there's some binary data that follows - it's not our business to know.\r\n\r\nThink of a what kind of code a new user of the library would write, assuming they have not read any documentation other some examples, when they try to read a bunch of json values from stdin in a loop. Now make the API work that way, following the \"design principle of least astonishment\" : )\r\n\r\n\r\n"}}
{"actor":{"display_login":"TurpentineDistillery"}}