Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,17 @@ Json::getString()
}
}

const std::string&
Json::getString() const
{
switch (type_) {
case String:
return string_value;
default:
ON_LOGIC_ERROR("JSON value is not a string.");
}
}

std::vector<Json>&
Json::getArray()
{
Expand All @@ -532,6 +543,17 @@ Json::getArray()
}
}

const std::vector<Json>&
Json::getArray() const
{
switch (type_) {
case Array:
return array_value;
default:
ON_LOGIC_ERROR("JSON value is not an array.");
}
}

std::map<std::string, Json>&
Json::getObject()
{
Expand All @@ -543,6 +565,17 @@ Json::getObject()
}
}

const std::map<std::string, Json>&
Json::getObject() const
{
switch (type_) {
case Object:
return object_value;
default:
ON_LOGIC_ERROR("JSON value is not an object.");
}
}

void
Json::setArray()
{
Expand Down Expand Up @@ -580,6 +613,17 @@ Json::operator[](size_t index)
return array_value[index];
}

const Json&
Json::operator[](size_t index) const
{
if (!isArray())
ON_LOGIC_ERROR("JSON value is not an array.");
if (index >= array_value.size()) {
ON_LOGIC_ERROR("JSON index not in array.");
}
return array_value[index];
}

Json&
Json::operator[](const std::string& key)
{
Expand All @@ -588,6 +632,17 @@ Json::operator[](const std::string& key)
return object_value[key];
}

const Json&
Json::operator[](const std::string& key) const
{
if (!isObject())
ON_LOGIC_ERROR("JSON value is not an object.");
auto it = object_value.find(key);
if (it == object_value.end())
ON_LOGIC_ERROR("JSON object does not contain key.");
return it->second;
}

std::string
Json::toString() const
{
Expand Down
5 changes: 5 additions & 0 deletions json.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,11 @@ class Json
double getDouble() const;
double getNumber() const;
long long getLong() const;
const std::string& getString() const;
std::string& getString();
const std::vector<Json>& getArray() const;
std::vector<Json>& getArray();
const std::map<std::string, Json>& getObject() const;
std::map<std::string, Json>& getObject();

bool contains(const std::string&) const;
Expand All @@ -206,7 +209,9 @@ class Json
Json& operator=(Json&&);

Json& operator[](size_t);
const Json& operator[](size_t) const;
Json& operator[](const std::string&);
const Json& operator[](const std::string&) const;

operator std::string() const
{
Expand Down
Loading