mirror of
https://codeberg.org/vyn/rei-json.git
synced 2025-07-01 09:33:19 +00:00
Add 'remove' methods to Object and Array + unify 'set' methods
This commit is contained in:
parent
eb5191a722
commit
fd034eff71
8 changed files with 109 additions and 26 deletions
|
@ -32,8 +32,8 @@ int main() {
|
|||
array.push(false);
|
||||
array.pushNull();
|
||||
|
||||
objectJson.addObject("keyObject", std::move(obj));
|
||||
objectJson.addArray("keyArray", std::move(array));
|
||||
objectJson.set("keyObject", std::move(obj));
|
||||
objectJson.set("keyArray", std::move(array));
|
||||
|
||||
auto newJsonString = rei::json::toString(objectJson);
|
||||
std::println("{}\n", newJsonString);
|
||||
|
|
|
@ -19,12 +19,14 @@ namespace rei::json {
|
|||
JsonArray& push(const std::string& value);
|
||||
JsonArray& push(const std::string&& value);
|
||||
JsonArray& push(const char * value);
|
||||
JsonArray& pushObject(const JsonObject& object);
|
||||
JsonArray& pushObject(const JsonObject&& object);
|
||||
JsonArray& pushArray(const JsonArray& array);
|
||||
JsonArray& pushArray(const JsonArray&& array);
|
||||
JsonArray& push(const JsonObject& object);
|
||||
JsonArray& push(const JsonObject&& object);
|
||||
JsonArray& push(const JsonArray& array);
|
||||
JsonArray& push(const JsonArray&& array);
|
||||
JsonArray& pushNull();
|
||||
|
||||
FieldValue& operator[](unsigned index);
|
||||
|
||||
int getNumber(int index);
|
||||
bool getBool(int index);
|
||||
std::string getString(int index);
|
||||
|
@ -32,6 +34,7 @@ namespace rei::json {
|
|||
JsonArray& getArray(int index);
|
||||
bool hasNull(int index);
|
||||
|
||||
void remove(int index);
|
||||
void forEach(std::function<void(FieldValue&, int)>);
|
||||
size_t count() const;
|
||||
|
||||
|
|
|
@ -21,11 +21,13 @@ namespace rei::json {
|
|||
JsonObject& set(const std::string& key, const std::string& value);
|
||||
JsonObject& set(const std::string& key, const std::string&& value);
|
||||
JsonObject& set(const std::string& key, const char * value);
|
||||
JsonObject& set(const std::string& key, const JsonArray& array);
|
||||
JsonObject& set(const std::string& key, JsonArray&& array);
|
||||
JsonObject& set(const std::string& key, const JsonObject& object);
|
||||
JsonObject& set(const std::string& key, JsonObject&& object);
|
||||
JsonObject& setNull(const std::string& key);
|
||||
JsonObject& addArray(const std::string& key, const JsonArray& array);
|
||||
JsonObject& addArray(const std::string& key, JsonArray&& array);
|
||||
JsonObject& addObject(const std::string& key, const JsonObject& object);
|
||||
JsonObject& addObject(const std::string& key, JsonObject&& object);
|
||||
|
||||
FieldValue& operator[](const std::string& key);
|
||||
|
||||
int getNumber(const std::string& key);
|
||||
bool getBool(const std::string& key);
|
||||
|
@ -34,6 +36,9 @@ namespace rei::json {
|
|||
JsonArray& getArray(const std::string& key);
|
||||
bool hasNull(const std::string& key);
|
||||
|
||||
|
||||
void remove(const std::string& key);
|
||||
|
||||
void forEach(std::function<void(FieldValue&, const std::string& key)>);
|
||||
size_t count() const;
|
||||
|
||||
|
|
|
@ -10,6 +10,14 @@ namespace rei::json {
|
|||
elements.reserve(1000);
|
||||
}
|
||||
|
||||
|
||||
FieldValue& JsonArray::operator[](unsigned index) {
|
||||
if (index >= elements.size()) {
|
||||
throw std::logic_error(std::string("Trying to access non existing index: ") + std::to_string(index));
|
||||
}
|
||||
return elements[index];
|
||||
}
|
||||
|
||||
JsonArray& JsonArray::push(int value) {
|
||||
elements.push_back(value);
|
||||
return *this;
|
||||
|
@ -40,22 +48,22 @@ namespace rei::json {
|
|||
return *this;
|
||||
}
|
||||
|
||||
JsonArray& JsonArray::pushObject(const JsonObject& object) {
|
||||
JsonArray& JsonArray::push(const JsonObject& object) {
|
||||
elements.push_back(object);
|
||||
return *this;
|
||||
};
|
||||
|
||||
JsonArray& JsonArray::pushObject(const JsonObject&& object) {
|
||||
JsonArray& JsonArray::push(const JsonObject&& object) {
|
||||
elements.push_back(std::move(object));
|
||||
return *this;
|
||||
};
|
||||
|
||||
JsonArray& JsonArray::pushArray(const JsonArray& array) {
|
||||
JsonArray& JsonArray::push(const JsonArray& array) {
|
||||
elements.push_back(array);
|
||||
return *this;
|
||||
};
|
||||
|
||||
JsonArray& JsonArray::pushArray(const JsonArray&& array) {
|
||||
JsonArray& JsonArray::push(const JsonArray&& array) {
|
||||
elements.push_back(std::move(array));
|
||||
return *this;
|
||||
};
|
||||
|
@ -112,4 +120,8 @@ namespace rei::json {
|
|||
return elements.size();
|
||||
}
|
||||
|
||||
void JsonArray::remove(int index) {
|
||||
elements.erase(elements.begin() + index);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include "rei-json/Object.h"
|
||||
#include "rei-json/json.h"
|
||||
#include "rei-json/Field.h"
|
||||
#include "rei-json/Array.h"
|
||||
|
@ -11,6 +12,14 @@ namespace rei::json {
|
|||
|
||||
};
|
||||
|
||||
|
||||
FieldValue& JsonObject::operator[](const std::string& key) {
|
||||
if (!contains(key)) {
|
||||
throw std::logic_error(std::string("Trying to access non existing key: ") + key);
|
||||
}
|
||||
return elements.at(key);
|
||||
}
|
||||
|
||||
bool JsonObject::contains(const std::string& key) const {
|
||||
return elements.contains(key);
|
||||
}
|
||||
|
@ -87,22 +96,22 @@ namespace rei::json {
|
|||
return elements.at(key).isNull();
|
||||
}
|
||||
|
||||
JsonObject& JsonObject::addArray(const std::string& key, const JsonArray& array) {
|
||||
JsonObject& JsonObject::set(const std::string& key, const JsonArray& array) {
|
||||
elements.insert_or_assign(key, array);
|
||||
return *this;
|
||||
}
|
||||
|
||||
JsonObject& JsonObject::addArray(const std::string& key, JsonArray&& array) {
|
||||
JsonObject& JsonObject::set(const std::string& key, JsonArray&& array) {
|
||||
elements.insert_or_assign(key, std::move(array));
|
||||
return *this;
|
||||
}
|
||||
|
||||
JsonObject& JsonObject::addObject(const std::string& key, const JsonObject& object) {
|
||||
JsonObject& JsonObject::set(const std::string& key, const JsonObject& object) {
|
||||
elements.insert_or_assign(key, object);
|
||||
return *this;
|
||||
}
|
||||
|
||||
JsonObject& JsonObject::addObject(const std::string& key, JsonObject&& object) {
|
||||
JsonObject& JsonObject::set(const std::string& key, JsonObject&& object) {
|
||||
elements.insert_or_assign(key, std::move(object));
|
||||
return *this;
|
||||
}
|
||||
|
@ -116,4 +125,8 @@ namespace rei::json {
|
|||
size_t JsonObject::count() const {
|
||||
return elements.size();
|
||||
}
|
||||
|
||||
void JsonObject::remove(const std::string& key) {
|
||||
elements.erase(key);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -87,11 +87,11 @@ namespace rei::json {
|
|||
} else if (str[*i] == '{'){
|
||||
JsonObject newObject{};
|
||||
parseObject(str, i, &newObject);
|
||||
object->addObject(key, std::move(newObject));
|
||||
object->set(key, std::move(newObject));
|
||||
} else if (str[*i] == '['){
|
||||
JsonArray newArray{};
|
||||
parseArray(str, i, &newArray);
|
||||
object->addArray(key, std::move(newArray));
|
||||
object->set(key, std::move(newArray));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -127,11 +127,11 @@ namespace rei::json {
|
|||
} else if (str[*i] == '{'){
|
||||
JsonObject newObject{};
|
||||
parseObject(str, i, &newObject);
|
||||
array->pushObject(std::move(newObject));
|
||||
array->push(std::move(newObject));
|
||||
} else if (str[*i] == '['){
|
||||
JsonArray newArray{};
|
||||
parseArray(str, i, &newArray);
|
||||
array->pushArray(std::move(newArray));
|
||||
array->push(std::move(newArray));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,23 +9,31 @@ TEST_CASE("Stringify json") {
|
|||
.set("keyNegativeNumber", -13)
|
||||
.set("keyBooleanTrue", true)
|
||||
.set("keyBooleanFalse", false)
|
||||
.set("keyElementDeleted", true) // it will be deleted after
|
||||
.set("keyString", "YEP")
|
||||
.set("keyEmptyString", "")
|
||||
.setNull("keyNull");
|
||||
|
||||
|
||||
rei::json::JsonObject obj{};
|
||||
obj.set("keyNumberOnObject", 42);
|
||||
|
||||
rei::json::JsonArray array{};
|
||||
array.push(42);
|
||||
array.push("elemString");
|
||||
array.push("elementDeleted"); // it will be deleted after
|
||||
array.push("");
|
||||
array.push(true);
|
||||
array.push(false);
|
||||
array.pushNull();
|
||||
|
||||
objectJson.addObject("keyObject", std::move(obj));
|
||||
objectJson.addArray("keyArray", std::move(array));
|
||||
|
||||
objectJson.remove("keyElementDeleted");
|
||||
array.remove(2);
|
||||
|
||||
objectJson.set("keyObject", std::move(obj));
|
||||
objectJson.set("keyArray", std::move(array));
|
||||
|
||||
|
||||
std::string expectedJson = R"({
|
||||
"keyArray": [
|
||||
|
|
|
@ -26,26 +26,68 @@ TEST_CASE("Basic json") {
|
|||
array.push(false);
|
||||
array.pushNull();
|
||||
|
||||
objectJson.addObject("keyObject", std::move(obj));
|
||||
objectJson.addArray("keyArray", std::move(array));
|
||||
objectJson.set("keyObject", std::move(obj));
|
||||
objectJson.set("keyArray", std::move(array));
|
||||
|
||||
REQUIRE(objectJson.getNumber("keyPositiveNumber") == 12);
|
||||
REQUIRE(objectJson["keyPositiveNumber"].asNumber() == 12);
|
||||
|
||||
REQUIRE(objectJson.getNumber("keyNegativeNumber") == -13);
|
||||
REQUIRE(objectJson["keyNegativeNumber"].asNumber() == -13);
|
||||
|
||||
REQUIRE(objectJson.getBool("keyBooleanTrue") == true);
|
||||
REQUIRE(objectJson["keyBooleanTrue"].asBool() == true);
|
||||
|
||||
REQUIRE(objectJson.getBool("keyBooleanFalse") == false);
|
||||
REQUIRE(objectJson["keyBooleanFalse"].asBool() == false);
|
||||
|
||||
REQUIRE(objectJson.getString("keyString") == "YEP");
|
||||
REQUIRE(objectJson["keyString"].asString() == "YEP");
|
||||
|
||||
REQUIRE(objectJson.getString("keyEmptyString") == "");
|
||||
REQUIRE(objectJson["keyEmptyString"].asString() == "");
|
||||
|
||||
REQUIRE(objectJson.getObject("keyObject").getNumber("keyNumberOnObject") == 42);
|
||||
REQUIRE(objectJson["keyObject"].asObject()["keyNumberOnObject"].asNumber() == 42);
|
||||
|
||||
REQUIRE(objectJson.hasNull("keyNull") == true);
|
||||
REQUIRE(objectJson["keyNull"].isNull() == true);
|
||||
|
||||
REQUIRE(objectJson.hasNull("keyPositiveNumber") == false);
|
||||
REQUIRE(objectJson["keyPositiveNumber"].isNull() == false);
|
||||
|
||||
REQUIRE_THROWS(objectJson.getNumber("nonExistantKey"));
|
||||
REQUIRE_THROWS(objectJson["nonExistantKey"]);
|
||||
|
||||
auto& arrayJson = objectJson.getArray("keyArray");
|
||||
|
||||
|
||||
REQUIRE(arrayJson.getNumber(0) == 42);
|
||||
REQUIRE(arrayJson[0].asNumber() == 42);
|
||||
|
||||
REQUIRE(arrayJson.getString(1) == "elemString");
|
||||
REQUIRE(arrayJson[1].asString() == "elemString");
|
||||
|
||||
REQUIRE(arrayJson.getString(2) == "");
|
||||
REQUIRE(arrayJson[2].asString() == "");
|
||||
|
||||
REQUIRE(arrayJson.getBool(3) == true);
|
||||
REQUIRE(arrayJson[3].asBool() == true);
|
||||
|
||||
REQUIRE(arrayJson.getBool(4) == false);
|
||||
REQUIRE(arrayJson[4].asBool() == false);
|
||||
|
||||
REQUIRE(arrayJson.hasNull(5));
|
||||
REQUIRE(arrayJson[5].isNull() == true);
|
||||
|
||||
REQUIRE_THROWS(arrayJson.getNumber(99));
|
||||
REQUIRE_THROWS(arrayJson[99].asNumber());
|
||||
|
||||
objectJson.remove("keyString");
|
||||
REQUIRE_THROWS(objectJson.getString("keyString") == "YEP");
|
||||
|
||||
arrayJson.remove(0);
|
||||
REQUIRE(arrayJson[0].isNumber() == false);
|
||||
REQUIRE(arrayJson[0].isString() == true);
|
||||
REQUIRE(arrayJson[0].asString() == "elemString");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue