Text File Parsing Warcrime mini update

This commit is contained in:
2024-11-04 19:51:40 -06:00
parent 5d078c16ff
commit e183f130a4
3 changed files with 37 additions and 31 deletions

View File

@@ -31,18 +31,15 @@ namespace JJX::json {
explicit operator bool() const { return boolean.value(); }
explicit operator std::vector<value>() const { return array.value(); }
explicit operator std::map<std::string, value>() const { return object.value(); }
};
struct string_val : value
{
void operator=(const std::string& me)
{
string = me;
}
operator std::string()
{
return string.value();
}
void operator=(const std::string& me);
operator std::string();
};
struct number_val : value {};
struct boolean_val : value {};
@@ -52,6 +49,9 @@ namespace JJX::json {
void add(const std::string& key, const std::string& val);
value& operator[] (const std::string& key);
template <typename T>
T get(const std::string& key);
};
struct array_val : value
{
@@ -66,9 +66,9 @@ namespace JJX::json {
string_val string(const std::string& text);
value number(double input);
number_val number(double input);
value boolean(bool input);
boolean_val boolean(bool input);
array_val array(std::vector<value> input);
array_val array();

View File

@@ -114,15 +114,15 @@ product_info pinfo_fromjson(json::value root)
this_product.name = better_root["name"];
this_product.price = double(better_root["price"]);
json::array_val subobj = static_cast<json::array_val>(better_root["reviews"]);
json::object_val subobj = static_cast<json::object_val>(better_root["reviews"]);
for (auto& review_data : subobj)
for (auto& review_data_pre : subobj.array.value())
{
json::object_val review_data = static_cast<json::object_val>(review_data_pre);
product_info::review going_in;
going_in.rating = review_data.object.value().at("rating").number.value();
going_in.user = review_data.object.value().at("user").string.value();
going_in.review_text = review_data.object.value().at("review_text").string.value();
going_in.rating = double(review_data["rating"]);
going_in.user = review_data["user"];
going_in.review_text = review_data["review_text"];
this_product.reviews.push_back(going_in);
}
@@ -149,6 +149,7 @@ json::value pinfo_tojson(product_info input)
}
// Some progress has been made on small scale.
int main(int argc, char* argv[]) {
//parse_json_file("../samples/product_info.json");
auto file_contents = read_file("../samples/product_info.json");

View File

@@ -203,6 +203,16 @@ namespace JJX::json {
object = in;
}
void string_val::operator=(const std::string& me)
{
string = me;
}
string_val::operator std::string()
{
return string.value();
}
void object_val::add(const std::string& key, value val)
{
object.value().emplace(key, val);
@@ -400,10 +410,6 @@ namespace JJX::json {
}
}
std::tuple<json::value, std::string> parse(std::string source) {
auto [tokens, error] = json::lex(source);
if (error.size())
@@ -460,22 +466,21 @@ namespace JJX::json {
string_val out;
out.type = value_type::string;
out.string = text;
return out;
}
value number(double input) {
return {
.number = input,
.type = value_type::number
};
number_val number(double input) {
number_val out;
out.type = value_type::number;
out.number = input;
return out;
}
value boolean(bool input) {
return {
.boolean = input,
.type = value_type::boolean
};
boolean_val boolean(bool input) {
boolean_val out;
out.type = value_type::boolean;
out.number = input;
return out;
}
array_val array(std::vector<value> input)