From 6d24711208e60ad4ef85c2d055bd49af120e8f77 Mon Sep 17 00:00:00 2001 From: Antony Woods Date: Thu, 6 Nov 2014 14:10:38 +0000 Subject: Added boolean types to json rvalue (.b()) including tests --- include/json.h | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'include/json.h') diff --git a/include/json.h b/include/json.h index d79fe88..3fc0911 100644 --- a/include/json.h +++ b/include/json.h @@ -284,6 +284,15 @@ namespace crow return boost::lexical_cast(start_, end_-start_); } + bool b() const + { +#ifndef CROW_JSON_NO_ERROR_CHECK + if (t() != type::True && t() != type::False) + throw std::runtime_error("value is not boolean"); +#endif + return t() == type::True; + } + void unescape() const { if (*(start_-1)) -- cgit v1.2.3-54-g00ecf From 49cad23ac917ac905fea2b16c0a4d63652aca2d9 Mon Sep 17 00:00:00 2001 From: Jaeseung Ha Date: Sun, 9 Nov 2014 08:12:43 +0900 Subject: Fixes #32 by chaing types for operator overloading from int32_t to int, long --- amalgamate/crow_all.h | 61 ++++++++++++++++++++++++++++++++++++++++++++++----- include/json.h | 28 ++++++++++++++++++----- 2 files changed, 77 insertions(+), 12 deletions(-) (limited to 'include/json.h') diff --git a/amalgamate/crow_all.h b/amalgamate/crow_all.h index 2294438..38a7e4f 100644 --- a/amalgamate/crow_all.h +++ b/amalgamate/crow_all.h @@ -569,6 +569,39 @@ namespace crow } + query_string(const query_string& qs) + : url_(qs.url_) + { + for(auto p:qs.key_value_pairs_) + { + key_value_pairs_.push_back((char*)(p-qs.url_.c_str()+url_.c_str())); + } + } + + query_string& operator = (const query_string& qs) + { + url_ = qs.url_; + key_value_pairs_.clear(); + for(auto p:qs.key_value_pairs_) + { + key_value_pairs_.push_back((char*)(p-qs.url_.c_str()+url_.c_str())); + } + return *this; + } + + query_string& operator = (query_string&& qs) + { + key_value_pairs_ = std::move(qs.key_value_pairs_); + char* old_data = (char*)qs.url_.c_str(); + url_ = std::move(qs.url_); + for(auto& p:key_value_pairs_) + { + p += (char*)url_.c_str() - old_data; + } + return *this; + } + + query_string(std::string url) : url_(std::move(url)) { @@ -1898,7 +1931,23 @@ namespace crow return *this; } - wvalue& operator = (uint16_t value) + wvalue& operator = (unsigned short value) + { + reset(); + t_ = type::Number; + d = (double)value; + return *this; + } + + wvalue& operator = (short value) + { + reset(); + t_ = type::Number; + d = (double)value; + return *this; + } + + wvalue& operator = (long long value) { reset(); t_ = type::Number; @@ -1906,7 +1955,7 @@ namespace crow return *this; } - wvalue& operator = (int16_t value) + wvalue& operator = (long value) { reset(); t_ = type::Number; @@ -1914,7 +1963,7 @@ namespace crow return *this; } - wvalue& operator = (uint32_t value) + wvalue& operator = (int value) { reset(); t_ = type::Number; @@ -1922,7 +1971,7 @@ namespace crow return *this; } - wvalue& operator = (int32_t value) + wvalue& operator = (unsigned long long value) { reset(); t_ = type::Number; @@ -1930,7 +1979,7 @@ namespace crow return *this; } - wvalue& operator = (uint64_t value) + wvalue& operator = (unsigned long value) { reset(); t_ = type::Number; @@ -1938,7 +1987,7 @@ namespace crow return *this; } - wvalue& operator = (int64_t value) + wvalue& operator = (unsigned int value) { reset(); t_ = type::Number; diff --git a/include/json.h b/include/json.h index d79fe88..91d4fe7 100644 --- a/include/json.h +++ b/include/json.h @@ -1134,7 +1134,7 @@ namespace crow return *this; } - wvalue& operator = (uint16_t value) + wvalue& operator = (unsigned short value) { reset(); t_ = type::Number; @@ -1142,7 +1142,7 @@ namespace crow return *this; } - wvalue& operator = (int16_t value) + wvalue& operator = (short value) { reset(); t_ = type::Number; @@ -1150,7 +1150,7 @@ namespace crow return *this; } - wvalue& operator = (uint32_t value) + wvalue& operator = (long long value) { reset(); t_ = type::Number; @@ -1158,7 +1158,7 @@ namespace crow return *this; } - wvalue& operator = (int32_t value) + wvalue& operator = (long value) { reset(); t_ = type::Number; @@ -1166,7 +1166,7 @@ namespace crow return *this; } - wvalue& operator = (uint64_t value) + wvalue& operator = (int value) { reset(); t_ = type::Number; @@ -1174,7 +1174,23 @@ namespace crow return *this; } - wvalue& operator = (int64_t value) + wvalue& operator = (unsigned long long value) + { + reset(); + t_ = type::Number; + d = (double)value; + return *this; + } + + wvalue& operator = (unsigned long value) + { + reset(); + t_ = type::Number; + d = (double)value; + return *this; + } + + wvalue& operator = (unsigned int value) { reset(); t_ = type::Number; -- cgit v1.2.3-54-g00ecf From f52778f04ddd0899374ce24bc36964181f67cbaa Mon Sep 17 00:00:00 2001 From: ipknHama Date: Sun, 9 Nov 2014 08:47:45 +0900 Subject: Fixes a bug that crow::json failed to parse double value starting with 0 --- include/json.h | 4 ++-- tests/unittest.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) (limited to 'include/json.h') diff --git a/include/json.h b/include/json.h index 91d4fe7..4fe03a7 100644 --- a/include/json.h +++ b/include/json.h @@ -824,9 +824,9 @@ namespace crow return {};*/ break; case '.': - state = (NumberParsingState)"\7\7\7\4\7\7\7"[state]; + state = (NumberParsingState)"\7\7\4\4\7\7\7"[state]; /* - if (state == NumberParsingState::Digits) + if (state == NumberParsingState::Digits || state == NumberParsingState::ZeroFirst) { state = NumberParsingState::DigitsAfterPoints; } diff --git a/tests/unittest.cpp b/tests/unittest.cpp index 4dec727..0e23ffd 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -455,6 +455,32 @@ TEST(json_read) } +TEST(json_read_real) +{ + vector v{"0.036303908355795146", "0.18320417789757412", + "0.05319940476190476", "0.15224702380952382", "0", "0.3296201145552561", + "0.47921580188679247", "0.05873511904761905", "0.1577827380952381", + "0.4996841307277628", "0.6425412735849056", "0.052113095238095236", + "0.12830357142857143", "0.7871041105121294", "0.954220013477089", + "0.05869047619047619", "0.1625", "0.8144794474393531", + "0.9721613881401617", "0.1399404761904762", "0.24470238095238095", + "0.04527459568733154", "0.2096950808625337", "0.35267857142857145", + "0.42791666666666667", "0.855731974393531", "0.9352467991913747", + "0.3816220238095238", "0.4282886904761905", "0.39414167789757415", + "0.5316079851752021", "0.3809375", "0.4571279761904762", + "0.03522995283018868", "0.1915641846361186", "0.6164136904761904", + "0.7192708333333333", "0.05675117924528302", "0.21308541105121293", + "0.7045386904761904", "0.8016815476190476"}; + for(auto x:v) + { + CROW_LOG_DEBUG << x; + ASSERT_EQUAL(json::load(x).d(), boost::lexical_cast(x)); + } + + auto ret = json::load(R"---({"balloons":[{"mode":"ellipse","left":0.036303908355795146,"right":0.18320417789757412,"top":0.05319940476190476,"bottom":0.15224702380952382,"index":"0"},{"mode":"ellipse","left":0.3296201145552561,"right":0.47921580188679247,"top":0.05873511904761905,"bottom":0.1577827380952381,"index":"1"},{"mode":"ellipse","left":0.4996841307277628,"right":0.6425412735849056,"top":0.052113095238095236,"bottom":0.12830357142857143,"index":"2"},{"mode":"ellipse","left":0.7871041105121294,"right":0.954220013477089,"top":0.05869047619047619,"bottom":0.1625,"index":"3"},{"mode":"ellipse","left":0.8144794474393531,"right":0.9721613881401617,"top":0.1399404761904762,"bottom":0.24470238095238095,"index":"4"},{"mode":"ellipse","left":0.04527459568733154,"right":0.2096950808625337,"top":0.35267857142857145,"bottom":0.42791666666666667,"index":"5"},{"mode":"ellipse","left":0.855731974393531,"right":0.9352467991913747,"top":0.3816220238095238,"bottom":0.4282886904761905,"index":"6"},{"mode":"ellipse","left":0.39414167789757415,"right":0.5316079851752021,"top":0.3809375,"bottom":0.4571279761904762,"index":"7"},{"mode":"ellipse","left":0.03522995283018868,"right":0.1915641846361186,"top":0.6164136904761904,"bottom":0.7192708333333333,"index":"8"},{"mode":"ellipse","left":0.05675117924528302,"right":0.21308541105121293,"top":0.7045386904761904,"bottom":0.8016815476190476,"index":"9"}]})---"); + ASSERT_TRUE(ret); +} + TEST(json_read_unescaping) { { -- cgit v1.2.3-54-g00ecf