C++: Rename some functions to better match the C API

Value::save_file -> Value::dump_file
Value::save_string -> Value::dumps
load_string -> loads
This commit is contained in:
Petri Lehtinen 2010-02-04 21:02:35 +02:00
parent 49d40f020b
commit b8059a1880
3 changed files with 10 additions and 10 deletions

View File

@ -127,10 +127,10 @@ namespace json {
inline _Base& insert_at(unsigned int index, const Value& value); inline _Base& insert_at(unsigned int index, const Value& value);
// write the value to a file // write the value to a file
inline int save_file(const char* path, int flags = 0) const; inline int dump_file(const char* path, int flags = 0) const;
// write the value to a string (caller must deallocate with free()!) // write the value to a string (caller must deallocate with free()!)
inline char* save_string(int flags = 0) const; inline char* dumps(int flags = 0) const;
}; };
// represents any JSON value, private base // represents any JSON value, private base
@ -291,7 +291,7 @@ namespace json {
inline Value load_file(const char* path, json_error_t* error = 0); inline Value load_file(const char* path, json_error_t* error = 0);
// load a string as a JSON value // load a string as a JSON value
inline Value load_string(const char* string, json_error_t* error = 0); inline Value loads(const char* string, json_error_t* error = 0);
} // namespace json } // namespace json

View File

@ -270,13 +270,13 @@ namespace json {
// write the value to a file // write the value to a file
template <typename _Base> template <typename _Base>
int ValueBase<_Base>::save_file(const char* path, int flags) const { int ValueBase<_Base>::dump_file(const char* path, int flags) const {
return json_dump_file(_Base::as_json(), path, flags); return json_dump_file(_Base::as_json(), path, flags);
} }
// write the value to a string (caller must deallocate with free()!) // write the value to a string (caller must deallocate with free()!)
template <typename _Base> template <typename _Base>
char* ValueBase<_Base>::save_string(int flags) const { char* ValueBase<_Base>::dumps(int flags) const {
return json_dumps(_Base::as_json(), flags); return json_dumps(_Base::as_json(), flags);
} }
@ -440,7 +440,7 @@ namespace json {
} }
// load a string as a JSON value // load a string as a JSON value
Value load_string(const char* string, json_error_t* error) { Value loads(const char* string, json_error_t* error) {
return Value::take_ownership(json_loads(string, error)); return Value::take_ownership(json_loads(string, error));
} }
@ -449,7 +449,7 @@ namespace json {
// stream JSON value out // stream JSON value out
std::ostream& operator<<(std::ostream& os, const json::Value& value) { std::ostream& operator<<(std::ostream& os, const json::Value& value) {
// get the temporary serialize string // get the temporary serialize string
char* tmp = value.save_string(); char* tmp = value.dumps();
if (tmp != 0) { if (tmp != 0) {
// stream temp string out and release it // stream temp string out and release it
os << tmp; os << tmp;
@ -465,6 +465,6 @@ std::istream& operator>>(std::istream& is, json::Value& value) {
while (is) while (is)
tmp << static_cast<char>(is.get()); tmp << static_cast<char>(is.get());
// parse the buffered string // parse the buffered string
value = json::load_string(tmp.str().c_str()); value = json::loads(tmp.str().c_str());
return is; return is;
} }

View File

@ -25,7 +25,7 @@ int main() {
json::Value e1(json::load_file("suites/api/test_cpp.json")); json::Value e1(json::load_file("suites/api/test_cpp.json"));
json::Value e2(e1); json::Value e2(e1);
json::Value e3; json::Value e3;
json::Value e4(json::load_string("{\"foo\": true, \"bar\": \"test\"}")); json::Value e4(json::loads("{\"foo\": true, \"bar\": \"test\"}"));
ASSERT_TRUE(e1.is_object(), "e1 is not an object"); ASSERT_TRUE(e1.is_object(), "e1 is not an object");
ASSERT_TRUE(e2.is_object(), "e2 is not an object"); ASSERT_TRUE(e2.is_object(), "e2 is not an object");
@ -125,7 +125,7 @@ int main() {
json::Value e12(json::object()); json::Value e12(json::object());
e12.set_key("foo", json::Value("test")); e12.set_key("foo", json::Value("test"));
e12.set_key("bar", json::Value(3)); e12.set_key("bar", json::Value(3));
char* out_cstr = e12.save_string(0); char* out_cstr = e12.dumps(0);
std::string out(out_cstr); std::string out(out_cstr);
free(out_cstr); free(out_cstr);
ASSERT_EQ(out, "{\"bar\": 3, \"foo\": \"test\"}", "object did not serialize as expected"); ASSERT_EQ(out, "{\"bar\": 3, \"foo\": \"test\"}", "object did not serialize as expected");