diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-05-02 21:34:43 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-05-02 21:34:43 +0100 |
commit | d1b484614afbaa0a9a04e5339920e30668bb819c (patch) | |
tree | f2009d68e3b70c764db0cc8f0e5ce5e4859be797 | |
parent | Remove pointless VALUE state... (diff) | |
download | libjsonpp-d1b484614afbaa0a9a04e5339920e30668bb819c.tar.bz2 libjsonpp-d1b484614afbaa0a9a04e5339920e30668bb819c.tar.xz libjsonpp-d1b484614afbaa0a9a04e5339920e30668bb819c.zip |
Pointer free
Remove all the value pointer stuff... Just not needed.
-rw-r--r-- | libjsonpp/jsonFlexLexer.cpp | 17 | ||||
-rw-r--r-- | libjsonpp/jsonFlexLexer.h | 4 | ||||
-rw-r--r-- | libjsonpp/jsonpp.h | 6 | ||||
-rw-r--r-- | libjsonpp/parse.cpp | 5 | ||||
-rw-r--r-- | libjsonpp/serialize.cpp | 4 | ||||
-rw-r--r-- | libjsonpp/testParse.cpp | 28 | ||||
-rw-r--r-- | libjsonpp/testSerialise.cpp | 47 |
7 files changed, 48 insertions, 63 deletions
diff --git a/libjsonpp/jsonFlexLexer.cpp b/libjsonpp/jsonFlexLexer.cpp index f8900b0..95fb955 100644 --- a/libjsonpp/jsonFlexLexer.cpp +++ b/libjsonpp/jsonFlexLexer.cpp @@ -3,22 +3,17 @@ #include <glibmm/convert.h> namespace json { - jsonFlexLexer::jsonFlexLexer(std::istream & in, const std::string & enc) : + jsonFlexLexer::jsonFlexLexer(std::istream & in, const std::string & enc, Value & v) : yyFlexLexer(&in, NULL), encoding(enc) { yy_push_state(0); - acceptValues.push([this](const auto & value) { - return values.emplace(std::make_shared<Value>(value)).get(); + acceptValues.push([&v](const auto & value) { + v = value; + return &v; }); } - ValuePtr - jsonFlexLexer::getValue() const - { - return values.top(); - } - std::string jsonFlexLexer::encodeBuf() const { @@ -33,7 +28,7 @@ namespace json { { auto object = std::get_if<Object>(acceptValues.top()(Object())); acceptValues.push([object,this](const auto & value) { - return object->insert_or_assign(name, std::make_shared<Value>(value)).first->second.get(); + return &object->insert_or_assign(name, value).first->second; }); } @@ -42,7 +37,7 @@ namespace json { { auto array = std::get_if<Array>(acceptValues.top()(Array())); acceptValues.push([array](const auto & value) { - return array->emplace_back(std::make_shared<Value>(value)).get(); + return &array->emplace_back(value); }); } diff --git a/libjsonpp/jsonFlexLexer.h b/libjsonpp/jsonFlexLexer.h index 6ea1336..c95636e 100644 --- a/libjsonpp/jsonFlexLexer.h +++ b/libjsonpp/jsonFlexLexer.h @@ -9,10 +9,9 @@ namespace json { class jsonFlexLexer : public yyFlexLexer { public: - jsonFlexLexer(std::istream &, const std::string & enc); + jsonFlexLexer(std::istream &, const std::string & enc, Value & v); int yylex(); - ValuePtr getValue() const; void LexerError(const char * msg) override; void BeginObject(); @@ -29,7 +28,6 @@ namespace json { std::string encodeBuf() const; std::string buf, name, encoding; - std::stack<ValuePtr> values; typedef std::function<Value *(const Value &)> AcceptValue; std::stack<AcceptValue> acceptValues; diff --git a/libjsonpp/jsonpp.h b/libjsonpp/jsonpp.h index 7e5a393..d56ca99 100644 --- a/libjsonpp/jsonpp.h +++ b/libjsonpp/jsonpp.h @@ -2,7 +2,6 @@ #define JSON_H #include <glibmm/ustring.h> -#include <memory> #include <variant> #include <map> #include <list> @@ -20,9 +19,8 @@ namespace json { typedef bool Boolean; class Null { }; class Value; - typedef std::shared_ptr<Value> ValuePtr; - typedef std::map<std::string, ValuePtr> Object; - typedef std::list<ValuePtr> Array; + typedef std::map<std::string, Value> Object; + typedef std::list<Value> Array; typedef std::variant<Null, String, Number, Object, Array, Boolean> VT; class Value : public VT { public: diff --git a/libjsonpp/parse.cpp b/libjsonpp/parse.cpp index 5dca4e8..3606258 100644 --- a/libjsonpp/parse.cpp +++ b/libjsonpp/parse.cpp @@ -13,9 +13,10 @@ namespace json { } Value parseValue(std::istream & s, const std::string & enc) { - jsonFlexLexer jfl(s, enc); + Value v; + jsonFlexLexer jfl(s, enc, v); while (jfl.yylex()) ; - return *jfl.getValue(); + return v; } Value parseValue(const Glib::ustring & s) { diff --git a/libjsonpp/serialize.cpp b/libjsonpp/serialize.cpp index e0d24eb..fe55fbd 100644 --- a/libjsonpp/serialize.cpp +++ b/libjsonpp/serialize.cpp @@ -93,7 +93,7 @@ namespace json { if (&v != &*a.begin()) { s << ','; } - (*this)(*v); + (*this)(v); } s << ']'; } @@ -105,7 +105,7 @@ namespace json { } (*this)(v.first); s << ':'; - (*this)(*v.second); + (*this)(v.second); } s << '}'; } diff --git a/libjsonpp/testParse.cpp b/libjsonpp/testParse.cpp index aa086e9..1fddab8 100644 --- a/libjsonpp/testParse.cpp +++ b/libjsonpp/testParse.cpp @@ -57,13 +57,13 @@ BOOST_AUTO_TEST_CASE( parse_array ) auto arr = std::get<json::Array>(json::parseValue(val)); BOOST_REQUIRE_EQUAL(7, arr.size()); auto itr = arr.begin(); - BOOST_REQUIRE_EQUAL(1, std::get<json::Number>(**itr++)); - BOOST_REQUIRE_EQUAL(2, std::get<json::Number>(**itr++)); - BOOST_REQUIRE_EQUAL(3, std::get<json::Number>(**itr++)); - std::get<json::Array>(**itr++); - std::get<json::Object>(**itr++); - BOOST_REQUIRE_EQUAL(9, std::get<json::Number>(**itr++)); - BOOST_REQUIRE_EQUAL(10, std::get<json::Number>(**itr++)); + BOOST_REQUIRE_EQUAL(1, std::get<json::Number>(*itr++)); + BOOST_REQUIRE_EQUAL(2, std::get<json::Number>(*itr++)); + BOOST_REQUIRE_EQUAL(3, std::get<json::Number>(*itr++)); + std::get<json::Array>(*itr++); + std::get<json::Object>(*itr++); + BOOST_REQUIRE_EQUAL(9, std::get<json::Number>(*itr++)); + BOOST_REQUIRE_EQUAL(10, std::get<json::Number>(*itr++)); } BOOST_AUTO_TEST_CASE( parse_array_of_strings ) @@ -91,8 +91,8 @@ BOOST_AUTO_TEST_CASE( parse_empty_arrary_in_object ) BOOST_REQUIRE_EQUAL(3, value.index()); auto obj = std::get<json::Object>(value); BOOST_REQUIRE_EQUAL(2, obj.size()); - BOOST_REQUIRE(std::get<json::Array>(*obj["v1"]).empty()); - BOOST_REQUIRE_EQUAL(100, std::get<json::Number>(*obj["v2"])); + BOOST_REQUIRE(std::get<json::Array>(obj["v1"]).empty()); + BOOST_REQUIRE_EQUAL(100, std::get<json::Number>(obj["v2"])); } BOOST_AUTO_TEST_CASE( parse_broken_array ) @@ -114,8 +114,8 @@ BOOST_AUTO_TEST_CASE( parse_object ) BOOST_REQUIRE_EQUAL(3, value.index()); auto obj = std::get<json::Object>(value); BOOST_REQUIRE_EQUAL(2, obj.size()); - BOOST_REQUIRE_EQUAL(1, std::get<json::Number>(*obj["a"])); - BOOST_REQUIRE_EQUAL(2, std::get<json::Number>(*obj["b"])); + BOOST_REQUIRE_EQUAL(1, std::get<json::Number>(obj["a"])); + BOOST_REQUIRE_EQUAL(2, std::get<json::Number>(obj["b"])); } BOOST_AUTO_TEST_CASE( parse_string_simple ) @@ -129,9 +129,9 @@ BOOST_AUTO_TEST_CASE( parse_object_withStringContainingQuote ) const Glib::ustring val(" { \"key1\": \"value1\", \"key2\": \"value\\\"2\\\"\", \"key3\": 3 } "); auto obj = std::get<json::Object>(json::parseValue(val)); BOOST_REQUIRE_EQUAL(3, obj.size()); - BOOST_REQUIRE_EQUAL("value1", std::get<json::String>(*obj["key1"])); - BOOST_REQUIRE_EQUAL("value\"2\"", std::get<json::String>(*obj["key2"])); - BOOST_REQUIRE_EQUAL(3, std::get<json::Number>(*obj["key3"])); + BOOST_REQUIRE_EQUAL("value1", std::get<json::String>(obj["key1"])); + BOOST_REQUIRE_EQUAL("value\"2\"", std::get<json::String>(obj["key2"])); + BOOST_REQUIRE_EQUAL(3, std::get<json::Number>(obj["key3"])); } BOOST_AUTO_TEST_CASE( parse_string_invalid_missingOpeningQuote ) diff --git a/libjsonpp/testSerialise.cpp b/libjsonpp/testSerialise.cpp index e1d7af2..66fc774 100644 --- a/libjsonpp/testSerialise.cpp +++ b/libjsonpp/testSerialise.cpp @@ -9,13 +9,7 @@ #define STR(s) #s const boost::filesystem::path root(XSTR(ROOT)); -template <typename T> -static -json::ValuePtr -jvp(const T & t) -{ - return json::ValuePtr(new json::Value(t)); -} +using namespace std::literals; static std::string @@ -103,25 +97,24 @@ BOOST_AUTO_TEST_CASE( serialise_quotes ) BOOST_AUTO_TEST_CASE( serialise_complexObject ) { - BOOST_REQUIRE_EQUAL("{\"a\":\"string\",\"b\":null,\"c\":64,\"d\":true,\"e\":{\"suba\":\"suba-val\",\"subb\":\"subb-val\"},\"f\":[true,false,-4.9,\"item\"]}", writeString(json::Object({ - {"a", jvp(Glib::ustring("string"))}, - {"b", jvp(json::Null())}, - {"c", jvp(64.0)}, - {"d", jvp(true)}, - {"e", jvp( - json::Object({ - {"suba", jvp(Glib::ustring("suba-val"))}, - {"subb", jvp(Glib::ustring("subb-val"))} - }) - )}, - {"f", jvp( - json::Array({ - jvp(true), - jvp(false), - jvp(-4.9), - jvp(Glib::ustring("item")) - }) - )}, - }))); + BOOST_REQUIRE_EQUAL("{\"a\":\"string\",\"b\":null,\"c\":64,\"d\":true,\"e\":{\"suba\":\"suba-val\",\"subb\":\"subb-val\"},\"f\":[true,false,-4.9,\"item\"]}", writeString( + json::Object({ + {"a", "string"s}, + {"b", json::Null()}, + {"c", 64.0}, + {"d", true}, + {"e", json::Object({ + {"suba", "suba-val"s}, + {"subb", "subb-val"s} + }) + }, + {"f", json::Array({ + true, + false, + -4.9, + "item"s + }), + } + }))); } |