diff --git a/jansson-impl.hpp b/jansson-impl.hpp index 89242fa..0f64f0b 100644 --- a/jansson-impl.hpp +++ b/jansson-impl.hpp @@ -202,7 +202,7 @@ namespace json { } template - int ValueBase<_Base>::as_integer() const { + json_int_t ValueBase<_Base>::as_integer() const { return json_integer_value(_Base::as_json()); } @@ -308,7 +308,10 @@ namespace json { // assign value to proxied array element ElementProxy& ElementProxy::operator=(const Value& value) { - json_array_set(_array, _index, value.as_json()); + if (_index == json_array_size(_array)) + json_array_append(_array, value.as_json()); + else + json_array_set(_array, _index, value.as_json()); return *this; } @@ -436,13 +439,23 @@ namespace json { } // load a file as a JSON value - Value load_file(const char* path, json_error_t* error) { - return Value::take_ownership(json_load_file(path, error)); + Value load_file(const char* path, json_error_t* error, size_t flags) { +#if JANSSON_VERSION_HEX >= 0x020000 + json_t* root = json_load_file(path, flags, error); +#else + json_t* root = json_load_file(path, error); +#endif + return Value::take_ownership(root); } // load a string as a JSON value - Value load_string(const char* string, json_error_t* error) { - return Value::take_ownership(json_loads(string, error)); + Value load_string(const char* string, json_error_t* error, size_t flags) { +#if JANSSON_VERSION_HEX >= 0x020000 + json_t* root = json_loads(string, flags, error); +#else + json_t* root = json_loads(string, error); +#endif + return Value::take_ownership(root); } } // namespace json @@ -463,8 +476,14 @@ std::ostream& operator<<(std::ostream& os, const json::Value& value) { std::istream& operator>>(std::istream& is, json::Value& value) { // buffer the remaining bytes into a single string for Jansson std::stringstream tmp; - while (is) - tmp << static_cast(is.get()); + char c; + while (is) { + is.get(c); + if (is.good()) + tmp << c; + } + tmp << '\0'; + // parse the buffered string value = json::load_string(tmp.str().c_str()); return is; diff --git a/jansson.hpp b/jansson.hpp index d3d11b3..774b4fb 100644 --- a/jansson.hpp +++ b/jansson.hpp @@ -98,7 +98,7 @@ namespace json { // get value cast to specified type inline const char* as_cstring() const; inline std::string as_string() const; - inline int as_integer() const; + inline json_int_t as_integer() const; inline double as_real() const; inline double as_number() const; inline bool as_boolean() const; @@ -284,10 +284,10 @@ namespace json { inline Value null(); // load a file as a JSON value - inline Value load_file(const char* path, json_error_t* error = 0); + inline Value load_file(const char* path, json_error_t* error = 0, size_t flags = 0); // load a string as a JSON value - inline Value load_string(const char* string, json_error_t* error = 0); + inline Value load_string(const char* string, json_error_t* error = 0, size_t flags = 0); } // namespace json diff --git a/test.cpp b/test.cpp index d69c344..fe0f734 100644 --- a/test.cpp +++ b/test.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "jansson.hpp" @@ -40,14 +41,16 @@ int json_cpp_tests() { ASSERT_EQ(e4["foo"].as_boolean(), true, "property has incorrect value"); + // verify iterator results (note that they can be returned in any order) json::Iterator i(e1.get("web-app")); - ASSERT_EQ(i.key(), "taglib", "first iterator result has incorrect key"); - i.next(); - ASSERT_EQ(i.key(), "servlet", "first iterator result has incorrect key"); - i.next(); - ASSERT_EQ(i.key(), "servlet-mapping", "first iterator result has incorrect key"); - i.next(); + std::set iteratorResults; + for ( int ii = 0; ii < 3; ++ii ) { + ASSERT_FALSE(i.key().empty(), "iterator returned a null value"); + iteratorResults.insert(i.key()); + i.next(); + } ASSERT_FALSE(i.valid(), "iterator has more values than expected"); + ASSERT_EQ(iteratorResults.size(), 3, "iterator did not return enough values"); json::Value e5(json::Value(12.34)); ASSERT_TRUE(e5.is_number(), "e5 is not a number after assignment"); @@ -123,10 +126,10 @@ int json_cpp_tests() { json::Value e12(json::object()); e12.set_key("foo", json::Value("test")); e12.set_key("bar", json::Value(3)); - char* out_cstr = e12.save_string(0); + char* out_cstr = e12.save_string(JSON_COMPACT); std::string out(out_cstr); free(out_cstr); - ASSERT_EQ(out, "{\"bar\": 3,\"foo\": \"test\"}\n", "object did not serialize as expected"); + ASSERT_EQ(out, "{\"bar\":3,\"foo\":\"test\"}", "object did not serialize as expected"); std::istringstream instr(out); instr >> e12; @@ -137,7 +140,7 @@ int json_cpp_tests() { std::ostringstream outstr; outstr << e12; - ASSERT_EQ(instr.str(), "{\"bar\": 3,\"foo\": \"test\"}\n", "object did not serialize as expected"); + ASSERT_EQ(instr.str(), "{\"bar\":3,\"foo\":\"test\"}", "object did not serialize as expected"); const json::Value e13(e12); ASSERT_EQ(e13["bar"].as_integer(), 3, "e13.bar has incorrect value after copy");