From f79a81dad96a6f3a57c5d5b0bbb65aa0494975a6 Mon Sep 17 00:00:00 2001 From: Sean Middleditch Date: Tue, 12 Jan 2010 15:29:45 -0800 Subject: [PATCH] add (ugly) stream support --- janssonxx.h | 21 +++++++++++++++++++++ test.cc | 11 +++++++++++ 2 files changed, 32 insertions(+) diff --git a/janssonxx.h b/janssonxx.h index b60e8cf..49975bf 100644 --- a/janssonxx.h +++ b/janssonxx.h @@ -2,6 +2,10 @@ #define JANSSONXX_H 1 #include +#include +#include +#include +#include namespace jansson { @@ -238,4 +242,21 @@ private: } // namespace jansson +// stream JSON value out +std::ostream& operator<<(std::ostream& os, const jansson::Value& value) { + char* tmp = value.save_string(); + os << tmp; + free(tmp); + return os; +} + +// read JSON value +std::istream& operator>>(std::istream& is, jansson::Value& value) { + std::stringstream tmp; + while (is) + tmp << static_cast(is.get()); + value = jansson::Value::load_string(tmp.str().c_str()); + return is; +} + #endif diff --git a/test.cc b/test.cc index 66ce7bb..28a83c8 100644 --- a/test.cc +++ b/test.cc @@ -117,5 +117,16 @@ int main() { free(out_cstr); ASSERT_EQ(out, "{\"bar\": 3,\"foo\": \"test\"}\n", "object did not serialize as expected"); + std::istringstream instr(out); + instr >> e3; + ASSERT_TRUE(e3.is_object(), "e3 is not an object after stream read"); + ASSERT_EQ(e3.size(), 2, "e3 has wrong size after stream read"); + ASSERT_EQ(e3.get("bar").as_integer(), 3, "e3.bar has incorrect value after stream read"); + ASSERT_EQ(e3.get("foo").as_string(), "test", "ee3.test has incorrect value after stream read"); + + std::ostringstream outstr; + outstr << e3; + ASSERT_EQ(instr.str(), "{\"bar\": 3,\"foo\": \"test\"}\n", "object did not serialize as expected"); + return 0; } -- 2.1.4