Browse Source

Add json object parsing and helpers

master
Riyyi 3 years ago
parent
commit
105912a62a
  1. 66
      inferno/src/inferno/util/json.h

66
inferno/src/inferno/util/json.h

@ -111,7 +111,7 @@ namespace Inferno {
return {};
}
// Check if property is array
// Check if property is array []
ASSERT(getValue(it).is_array(), "Json property is not an array '{}'", property);
// Fill array with values
@ -126,6 +126,45 @@ namespace Inferno {
}
}
// Return vector if it has any items, uninitialized optional otherwise
return values.size() > 0 ? std::optional { values } : std::nullopt;
}
template<typename T>
static std::optional<std::map<std::string, T>> parseObjectProperty(const json& json, const char* property, bool required, json::value_t check)
{
return parseObjectProperty<T>(json, property, required, std::vector { check });
}
template<typename T>
static std::optional<std::map<std::string, T>> parseObjectProperty(const json& json, const char* property, bool required, std::vector<json::value_t> checks)
{
bool exists;
json_const_iterator it;
// Has property
exists = hasProperty(json, it, property);
ASSERT(!required || (required && exists), "Json could not find required property '{}'", property);
if (!exists) {
return {};
}
// Check if property is an object {}
ASSERT(getValue(it).is_object(), "Json property is not an array '{}'", property);
std::map<std::string, T> values;
for (auto& [key, value] : getValue(it).items()) {
auto typedValue = getPropertyValue<T>(value, checks);
if (typedValue) {
values.emplace(std::move(key), typedValue.value());
}
else {
warnln("Json array property '{}' has type inconsistency", property);
}
}
// Return map if it has any items, uninitialized optional otherwise
return values.size() > 0 ? std::optional { values } : std::nullopt;
}
@ -169,8 +208,33 @@ namespace Inferno {
return parseArrayProperty<uint32_t>(json, property, required, json::value_t::number_unsigned);
}
static std::optional<std::map<std::string, double>> parseDoubleObjectProperty(const json& json, const char* property, bool required)
{
return parseObjectProperty<double>(json, property, required, std::vector { json::value_t::number_integer, json::value_t::number_unsigned, json::value_t::number_float });
}
static std::optional<std::map<std::string, int32_t>> parseIntegerObjectProperty(const json& json, const char* property, bool required)
{
return parseObjectProperty<int32_t>(json, property, required, json::value_t::number_integer);
}
static std::optional<std::map<std::string, uint32_t>> parseUnsignedObjectProperty(const json& json, const char* property, bool required)
{
return parseObjectProperty<uint32_t>(json, property, required, json::value_t::number_unsigned);
}
};
} // namespace Inferno
#endif // JSON_UTIL_H
// JSON syntax:
// [ ] = std::map, array (ordered)
// { } = std::vector, object (unordered)
// "": = std::string, property (key -> value)
// true/false = bool, condition
// -3 = int64_t, number
// 5 = uint64_t, number
// 4.567 = double, number
// "thing" = std::string, text

Loading…
Cancel
Save