diff options
-rw-r--r-- | libjsonpp/Jamfile.jam | 14 | ||||
-rw-r--r-- | libjsonpp/serialize.cpp | 4 | ||||
-rw-r--r-- | libjsonpp/testSerialise.cpp | 114 |
3 files changed, 131 insertions, 1 deletions
diff --git a/libjsonpp/Jamfile.jam b/libjsonpp/Jamfile.jam index cfbb48e..d97edea 100644 --- a/libjsonpp/Jamfile.jam +++ b/libjsonpp/Jamfile.jam @@ -41,4 +41,18 @@ run testParse ; +run + testSerialise.cpp + pch + : : : + <define>ROOT=\"$(me)\" + <define>BOOST_TEST_DYN_LINK + <library>jsonpp + <library>boost_utf + <library>boost_system + <library>boost_filesystem + : + testSerialise + ; + package.install install : <install-source-root>. : : jsonpp : [ glob *.h ] ; diff --git a/libjsonpp/serialize.cpp b/libjsonpp/serialize.cpp index 7fde08e..b9456a7 100644 --- a/libjsonpp/serialize.cpp +++ b/libjsonpp/serialize.cpp @@ -1,6 +1,7 @@ #include <pch.hpp> #include "jsonpp.h" #include <glibmm/convert.h> +#include <ios> namespace json { class JsonSerialize : public boost::static_visitor<> { @@ -128,11 +129,12 @@ namespace json { } void serializeNumber(const Number & n, std::ostream & s, const std::string & ) { + s.unsetf(std::ios::fixed); s << n; } void serializeBoolean(const Boolean & b, std::ostream & s, const std::string & ) { - s << b; + s << (b ? "true" : "false"); } void serializeNull(const Null &, std::ostream & s, const std::string & ) { diff --git a/libjsonpp/testSerialise.cpp b/libjsonpp/testSerialise.cpp new file mode 100644 index 0000000..98e9b41 --- /dev/null +++ b/libjsonpp/testSerialise.cpp @@ -0,0 +1,114 @@ +#include <pch.hpp> + +#define BOOST_TEST_MODULE serialise +#include <boost/test/unit_test.hpp> + +#include "jsonpp.h" +#include <fstream> +#include <boost/filesystem/path.hpp> + +#define XSTR(s) STR(s) +#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)); +} + +static +std::string +writeString(const json::Value & v) +{ + std::stringstream ss; + json::serializeValue(v, ss, "utf-8"); + return ss.str(); +} + +BOOST_AUTO_TEST_CASE( serialise_true ) +{ + BOOST_REQUIRE_EQUAL("true", writeString(true)); +} + +BOOST_AUTO_TEST_CASE( serialise_false ) +{ + BOOST_REQUIRE_EQUAL("false", writeString(false)); +} + +BOOST_AUTO_TEST_CASE( serialise_0 ) +{ + BOOST_REQUIRE_EQUAL("0", writeString(0.0)); +} + +BOOST_AUTO_TEST_CASE( serialise_48 ) +{ + BOOST_REQUIRE_EQUAL("48", writeString(48.0)); +} + +BOOST_AUTO_TEST_CASE( serialise_neg48 ) +{ + BOOST_REQUIRE_EQUAL("-48", writeString(-48.0)); +} + +BOOST_AUTO_TEST_CASE( serialise_pi ) +{ + BOOST_REQUIRE_EQUAL("3.14159", writeString(3.14159)); +} + +BOOST_AUTO_TEST_CASE( serialise_emptyArray ) +{ + BOOST_REQUIRE_EQUAL("[]", writeString(json::Array())); +} + +BOOST_AUTO_TEST_CASE( serialise_emptyObject ) +{ + BOOST_REQUIRE_EQUAL("{}", writeString(json::Object())); +} + +BOOST_AUTO_TEST_CASE( serialise_null ) +{ + BOOST_REQUIRE_EQUAL("null", writeString(json::Null())); +} + +BOOST_AUTO_TEST_CASE( serialise_unicode ) +{ + BOOST_REQUIRE_EQUAL("\"A Űņĩćőđē string.\"", writeString(Glib::ustring("A Űņĩćőđē string."))); +} + +BOOST_AUTO_TEST_CASE( serialise_whitespace ) +{ + BOOST_REQUIRE_EQUAL("\"\\r\\n\\t\"", writeString(Glib::ustring("\r\n\t"))); +} + +BOOST_AUTO_TEST_CASE( serialise_quotes ) +{ + BOOST_REQUIRE_EQUAL("\"string with \\\" in\"", writeString(Glib::ustring("string with \" in"))); +} + +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")) + }) + )}, + }))); +} + |