From d1b484614afbaa0a9a04e5339920e30668bb819c Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 2 May 2018 21:34:43 +0100 Subject: Pointer free Remove all the value pointer stuff... Just not needed. --- libjsonpp/jsonFlexLexer.cpp | 17 ++++++---------- libjsonpp/jsonFlexLexer.h | 4 +--- libjsonpp/jsonpp.h | 6 ++---- libjsonpp/parse.cpp | 5 +++-- libjsonpp/serialize.cpp | 4 ++-- libjsonpp/testParse.cpp | 28 +++++++++++++-------------- 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 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)).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(acceptValues.top()(Object())); acceptValues.push([object,this](const auto & value) { - return object->insert_or_assign(name, std::make_shared(value)).first->second.get(); + return &object->insert_or_assign(name, value).first->second; }); } @@ -42,7 +37,7 @@ namespace json { { auto array = std::get_if(acceptValues.top()(Array())); acceptValues.push([array](const auto & value) { - return array->emplace_back(std::make_shared(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 values; typedef std::function AcceptValue; std::stack 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 -#include #include #include #include @@ -20,9 +19,8 @@ namespace json { typedef bool Boolean; class Null { }; class Value; - typedef std::shared_ptr ValuePtr; - typedef std::map Object; - typedef std::list Array; + typedef std::map Object; + typedef std::list Array; typedef std::variant 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::parseValue(val)); BOOST_REQUIRE_EQUAL(7, arr.size()); auto itr = arr.begin(); - BOOST_REQUIRE_EQUAL(1, std::get(**itr++)); - BOOST_REQUIRE_EQUAL(2, std::get(**itr++)); - BOOST_REQUIRE_EQUAL(3, std::get(**itr++)); - std::get(**itr++); - std::get(**itr++); - BOOST_REQUIRE_EQUAL(9, std::get(**itr++)); - BOOST_REQUIRE_EQUAL(10, std::get(**itr++)); + BOOST_REQUIRE_EQUAL(1, std::get(*itr++)); + BOOST_REQUIRE_EQUAL(2, std::get(*itr++)); + BOOST_REQUIRE_EQUAL(3, std::get(*itr++)); + std::get(*itr++); + std::get(*itr++); + BOOST_REQUIRE_EQUAL(9, std::get(*itr++)); + BOOST_REQUIRE_EQUAL(10, std::get(*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(value); BOOST_REQUIRE_EQUAL(2, obj.size()); - BOOST_REQUIRE(std::get(*obj["v1"]).empty()); - BOOST_REQUIRE_EQUAL(100, std::get(*obj["v2"])); + BOOST_REQUIRE(std::get(obj["v1"]).empty()); + BOOST_REQUIRE_EQUAL(100, std::get(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(value); BOOST_REQUIRE_EQUAL(2, obj.size()); - BOOST_REQUIRE_EQUAL(1, std::get(*obj["a"])); - BOOST_REQUIRE_EQUAL(2, std::get(*obj["b"])); + BOOST_REQUIRE_EQUAL(1, std::get(obj["a"])); + BOOST_REQUIRE_EQUAL(2, std::get(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::parseValue(val)); BOOST_REQUIRE_EQUAL(3, obj.size()); - BOOST_REQUIRE_EQUAL("value1", std::get(*obj["key1"])); - BOOST_REQUIRE_EQUAL("value\"2\"", std::get(*obj["key2"])); - BOOST_REQUIRE_EQUAL(3, std::get(*obj["key3"])); + BOOST_REQUIRE_EQUAL("value1", std::get(obj["key1"])); + BOOST_REQUIRE_EQUAL("value\"2\"", std::get(obj["key2"])); + BOOST_REQUIRE_EQUAL(3, std::get(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 -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 + }), + } + }))); } -- cgit v1.2.3