Merge pull request #1647 from joakim-hove/json

Add build and write functionality to JsonObject
This commit is contained in:
Joakim Hove
2020-08-10 15:40:56 +02:00
committed by GitHub
3 changed files with 117 additions and 0 deletions

View File

@@ -30,12 +30,26 @@ namespace Json {
class JsonObject {
public:
JsonObject();
explicit JsonObject(const Opm::filesystem::path& jsonFile );
explicit JsonObject(const std::string& inline_json);
explicit JsonObject(const char * inline_json);
explicit JsonObject(cJSON * root);
~JsonObject();
void add(double value);
void add(int value);
void add(const std::string& value);
JsonObject add_array();
JsonObject add_object();
void add_item(const std::string& key, double value);
void add_item(const std::string& key, int value);
void add_item(const std::string& key, const std::string& value);
JsonObject add_array(const std::string& key);
JsonObject add_object(const std::string& key);
std::string dump() const;
bool has_item(const std::string& key) const;
JsonObject get_array_item( size_t index ) const;
JsonObject get_item(const std::string& key) const;

View File

@@ -47,6 +47,78 @@ namespace Json {
}
JsonObject::JsonObject() {
this->owner = true;
this->root = cJSON_CreateObject();
}
void JsonObject::add_item(const std::string& key, const std::string& value) {
cJSON_AddStringToObject(this->root, key.c_str(), value.c_str());
}
void JsonObject::add_item(const std::string& key, int value) {
cJSON_AddNumberToObject(this->root, key.c_str(), value);
}
void JsonObject::add_item(const std::string& key, double value) {
cJSON_AddNumberToObject(this->root, key.c_str(), value);
}
JsonObject JsonObject::add_array(const std::string& key) {
return JsonObject( cJSON_AddArrayToObject(this->root, key.c_str()) );
}
JsonObject JsonObject::add_object(const std::string& key) {
return JsonObject( cJSON_AddObjectToObject(this->root, key.c_str()) );
}
void JsonObject::add(const std::string& value) {
if (this->is_array())
cJSON_AddItemToArray(this->root, cJSON_CreateString( value.c_str() ));
else
throw std::invalid_argument("Item is not an array");
}
void JsonObject::add(int value) {
if (this->is_array())
cJSON_AddItemToArray(this->root, cJSON_CreateNumber( value ));
else
throw std::invalid_argument("Item is not an array");
}
void JsonObject::add(double value) {
if (this->is_array())
cJSON_AddItemToArray(this->root, cJSON_CreateNumber( value ));
else
throw std::invalid_argument("Item is not an array");
}
JsonObject JsonObject::add_array() {
if (this->is_array()) {
cJSON_AddItemToArray(this->root, cJSON_CreateArray());
return JsonObject( this->get_array_item( this->size() - 1));
}
else
throw std::invalid_argument("Item is not an array");
}
JsonObject JsonObject::add_object() {
if (this->is_array()) {
cJSON_AddItemToArray(this->root, cJSON_CreateObject( ));
return JsonObject( this->get_array_item( this->size() - 1));
} else
throw std::invalid_argument("Item is not an array");
}
std::string JsonObject::dump() const {
char * c_str = cJSON_Print( this->root );
std::string str(c_str);
free( c_str );
return str;
}
JsonObject::JsonObject(const Opm::filesystem::path& jsonFile ) {
std::ifstream stream(jsonFile.string().c_str());

View File

@@ -280,4 +280,35 @@ BOOST_AUTO_TEST_CASE(to_string_ok) {
}
BOOST_AUTO_TEST_CASE(create) {
Json::JsonObject json;
json.add_item("name", "Awesome 4D");
json.add_item("size", 100);
json.add_item("pi", 3.14159265);
{
auto list = json.add_array("array");
list.add("String");
list.add(100);
list.add(2.7172);
}
{
auto dict = json.add_object("object");
dict.add_item("key", "String");
dict.add_item("int", 100);
dict.add_item("double", 2.7172);
}
std::string s = json.dump();
Json::JsonObject json2(s);
BOOST_CHECK_EQUAL(json2.get_string("name"), "Awesome 4D");
BOOST_CHECK_EQUAL(json2.get_int("size"), 100);
auto array = json2.get_item("array");
BOOST_CHECK( array.is_array() );
BOOST_CHECK_EQUAL( array.size(), 3 );
BOOST_CHECK_EQUAL( array.get_array_item(2).as_double(), 2.7172 );
auto dict = json2.get_item("object");
BOOST_CHECK( dict.is_object() );
BOOST_CHECK_EQUAL( dict.get_string("key"), "String");
}