added save_file and save_string methods
This commit is contained in:
parent
36085ab49a
commit
7ef3202f83
10
janssonxx.h
10
janssonxx.h
@ -49,6 +49,16 @@ public:
|
|||||||
return Value().take_ownership(json_loads(string, error));
|
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
|
// construct Value from input
|
||||||
static Value from(const char* value) { return Value().take_ownership(json_string(value)); }
|
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()); }
|
static Value from(const std::string& value) { return from(value.c_str()); }
|
||||||
|
9
test.cc
9
test.cc
@ -1,5 +1,6 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
#include <malloc.h>
|
||||||
|
|
||||||
#include "janssonxx.h"
|
#include "janssonxx.h"
|
||||||
|
|
||||||
@ -108,5 +109,13 @@ int main() {
|
|||||||
e3.clear();
|
e3.clear();
|
||||||
ASSERT_EQ(e3.size(), 0, "e3 has incorrect number of properties after 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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user