Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions jansson-impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ namespace json {
}

template <typename _Base>
int ValueBase<_Base>::as_integer() const {
json_int_t ValueBase<_Base>::as_integer() const {
return json_integer_value(_Base::as_json());
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand All @@ -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<char>(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;
Expand Down
6 changes: 3 additions & 3 deletions jansson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand Down
21 changes: 12 additions & 9 deletions test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <iostream>
#include <iomanip>
#include <malloc.h>
#include <set>

#include "jansson.hpp"

Expand Down Expand Up @@ -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<std::string> 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");
Expand Down Expand Up @@ -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;
Expand All @@ -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");
Expand Down