added save_file and save_string methods

This commit is contained in:
Sean Middleditch 2010-01-12 04:20:17 -08:00
parent 36085ab49a
commit 7ef3202f83
2 changed files with 19 additions and 0 deletions

View File

@ -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()); }

View File

@ -1,5 +1,6 @@
#include <iostream>
#include <iomanip>
#include <malloc.h>
#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;
}