Commit b4571360 authored by Niels's avatar Niels

more on #290

parent eef80590
...@@ -511,7 +511,7 @@ To compile and run the tests, you need to execute ...@@ -511,7 +511,7 @@ To compile and run the tests, you need to execute
$ make check $ make check
=============================================================================== ===============================================================================
All tests passed (8905099 assertions in 32 test cases) All tests passed (8905119 assertions in 32 test cases)
``` ```
For more information, have a look at the file [.travis.yml](https://github.com/nlohmann/json/blob/master/.travis.yml). For more information, have a look at the file [.travis.yml](https://github.com/nlohmann/json/blob/master/.travis.yml).
#include <json.hpp>
using json = nlohmann::json;
int main()
{
// a JSON text
char text[] = R"(
{
"Image": {
"Width": 800,
"Height": 600,
"Title": "View from 15th Floor",
"Thumbnail": {
"Url": "http://www.example.com/image/481989943",
"Height": 125,
"Width": 100
},
"Animated" : false,
"IDs": [116, 943, 234, 38793]
}
}
)";
// parse and serialize JSON
json j_complete = json::parse(text);
std::cout << std::setw(4) << j_complete << "\n\n";
}
<a target="_blank" href="http://melpon.org/wandbox/permlink/LvZQq5fybVH1nh9L"><b>online</b></a>
\ No newline at end of file
{
"Image": {
"Animated": false,
"Height": 600,
"IDs": [
116,
943,
234,
38793
],
"Thumbnail": {
"Height": 125,
"Url": "http://www.example.com/image/481989943",
"Width": 100
},
"Title": "View from 15th Floor",
"Width": 800
}
}
#include <json.hpp>
using json = nlohmann::json;
int main()
{
// a JSON text given as std::vector
std::vector<uint8_t> text = {'[', '1', ',', '2', ',', '3', ']', '\0'};
// parse and serialize JSON
json j_complete = json::parse(text);
std::cout << std::setw(4) << j_complete << "\n\n";
}
<a target="_blank" href="http://melpon.org/wandbox/permlink/F8VaVFyys87qQRt5"><b>online</b></a>
\ No newline at end of file
#include <json.hpp>
using json = nlohmann::json;
int main()
{
// a JSON text given as std::vector
std::vector<uint8_t> text = {'[', '1', ',', '2', ',', '3', ']', '\0'};
// parse and serialize JSON
json j_complete = json::parse(text.begin(), text.end());
std::cout << std::setw(4) << j_complete << "\n\n";
}
<a target="_blank" href="http://melpon.org/wandbox/permlink/ojh4Eeol4G9RgeRV"><b>online</b></a>
\ No newline at end of file
...@@ -5,7 +5,7 @@ using json = nlohmann::json; ...@@ -5,7 +5,7 @@ using json = nlohmann::json;
int main() int main()
{ {
// a JSON text // a JSON text
std::string text = R"( auto text = R"(
{ {
"Image": { "Image": {
"Width": 800, "Width": 800,
...@@ -44,4 +44,4 @@ int main() ...@@ -44,4 +44,4 @@ int main()
// parse (with callback) and serialize JSON // parse (with callback) and serialize JSON
json j_filtered = json::parse(text, cb); json j_filtered = json::parse(text, cb);
std::cout << std::setw(4) << j_filtered << '\n'; std::cout << std::setw(4) << j_filtered << '\n';
} }
\ No newline at end of file
<a target="_blank" href="http://melpon.org/wandbox/permlink/SrKpkE9ivmvd2OUy"><b>online</b></a> <a target="_blank" href="http://melpon.org/wandbox/permlink/n888UNQlMFduURhE"><b>online</b></a>
\ No newline at end of file \ No newline at end of file
This diff is collapsed.
This diff is collapsed.
...@@ -477,7 +477,7 @@ TEST_CASE("parser class") ...@@ -477,7 +477,7 @@ TEST_CASE("parser class")
case ('r'): case ('r'):
case ('t'): case ('t'):
{ {
CHECK_NOTHROW(json::parser(s).parse()); CHECK_NOTHROW(json::parser(s.c_str()).parse());
break; break;
} }
...@@ -490,8 +490,8 @@ TEST_CASE("parser class") ...@@ -490,8 +490,8 @@ TEST_CASE("parser class")
// any other combination of backslash and character is invalid // any other combination of backslash and character is invalid
default: default:
{ {
CHECK_THROWS_AS(json::parser(s).parse(), std::invalid_argument); CHECK_THROWS_AS(json::parser(s.c_str()).parse(), std::invalid_argument);
CHECK_THROWS_WITH(json::parser(s).parse(), "parse error - unexpected '\"'"); CHECK_THROWS_WITH(json::parser(s.c_str()).parse(), "parse error - unexpected '\"'");
break; break;
} }
} }
...@@ -549,22 +549,22 @@ TEST_CASE("parser class") ...@@ -549,22 +549,22 @@ TEST_CASE("parser class")
if (valid(c)) if (valid(c))
{ {
CHECK_NOTHROW(json::parser(s1).parse()); CHECK_NOTHROW(json::parser(s1.c_str()).parse());
CHECK_NOTHROW(json::parser(s2).parse()); CHECK_NOTHROW(json::parser(s2.c_str()).parse());
CHECK_NOTHROW(json::parser(s3).parse()); CHECK_NOTHROW(json::parser(s3.c_str()).parse());
CHECK_NOTHROW(json::parser(s4).parse()); CHECK_NOTHROW(json::parser(s4.c_str()).parse());
} }
else else
{ {
CHECK_THROWS_AS(json::parser(s1).parse(), std::invalid_argument); CHECK_THROWS_AS(json::parser(s1.c_str()).parse(), std::invalid_argument);
CHECK_THROWS_AS(json::parser(s2).parse(), std::invalid_argument); CHECK_THROWS_AS(json::parser(s2.c_str()).parse(), std::invalid_argument);
CHECK_THROWS_AS(json::parser(s3).parse(), std::invalid_argument); CHECK_THROWS_AS(json::parser(s3.c_str()).parse(), std::invalid_argument);
CHECK_THROWS_AS(json::parser(s4).parse(), std::invalid_argument); CHECK_THROWS_AS(json::parser(s4.c_str()).parse(), std::invalid_argument);
CHECK_THROWS_WITH(json::parser(s1).parse(), "parse error - unexpected '\"'"); CHECK_THROWS_WITH(json::parser(s1.c_str()).parse(), "parse error - unexpected '\"'");
CHECK_THROWS_WITH(json::parser(s2).parse(), "parse error - unexpected '\"'"); CHECK_THROWS_WITH(json::parser(s2.c_str()).parse(), "parse error - unexpected '\"'");
CHECK_THROWS_WITH(json::parser(s3).parse(), "parse error - unexpected '\"'"); CHECK_THROWS_WITH(json::parser(s3.c_str()).parse(), "parse error - unexpected '\"'");
CHECK_THROWS_WITH(json::parser(s4).parse(), "parse error - unexpected '\"'"); CHECK_THROWS_WITH(json::parser(s4.c_str()).parse(), "parse error - unexpected '\"'");
} }
} }
} }
......
...@@ -113,6 +113,12 @@ TEST_CASE("deserialization") ...@@ -113,6 +113,12 @@ TEST_CASE("deserialization")
std::initializer_list<uint8_t> v = {'t', 'r', 'u', 'e', '\0'}; std::initializer_list<uint8_t> v = {'t', 'r', 'u', 'e', '\0'};
CHECK(json::parse(v) == json(true)); CHECK(json::parse(v) == json(true));
} }
SECTION("empty container")
{
std::vector<uint8_t> v;
CHECK_THROWS_AS(json::parse(v), std::invalid_argument);
}
} }
SECTION("via iterator range") SECTION("via iterator range")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment