diff --git a/janssonxx.h b/janssonxx.h index 9085652..46e53e8 100644 --- a/janssonxx.h +++ b/janssonxx.h @@ -49,6 +49,16 @@ public: return Value().take_ownership(json_loads(string, error)); } + // write the value to a file + int save_file(const char* path, int flags = JSON_INDENT(2)) const { + return json_dump_file(_value, path, flags); + } + + // write the value to a string (caller must deallocate with free()!) + char* save_string(int flags = JSON_INDENT(2)) const { + return json_dumps(_value, flags); + } + // construct Value from input static Value from(const char* value) { return Value().take_ownership(json_string(value)); } static Value from(const std::string& value) { return from(value.c_str()); } diff --git a/test.cc b/test.cc index 531db58..daacbea 100644 --- a/test.cc +++ b/test.cc @@ -1,5 +1,6 @@ #include #include +#include #include "janssonxx.h" @@ -108,5 +109,13 @@ int main() { e3.clear(); ASSERT_EQ(e3.size(), 0, "e3 has incorrect number of properties after clear"); + e3 = jansson::Value::object(); + e3.set("foo", jansson::Value::from("test")); + e3.set("bar", jansson::Value::from(3)); + char* out_cstr = e3.save_string(0); + string out(out_cstr); + free(out_cstr); + ASSERT_EQ(out, "{\"bar\": 3,\"foo\": \"test\"}\n", "object did not serialize as expected"); + return 0; }