Make sure required headers are included in JSON.hpp
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
|
||||
namespace JJX::json {
|
||||
enum class token_type { string, number, syntax, boolean, null };
|
||||
enum class value_type { string, number, object, array, boolean, null};
|
||||
@@ -10,6 +16,12 @@ namespace JJX::json {
|
||||
std::shared_ptr<std::string> full_source;
|
||||
};
|
||||
|
||||
struct string_val;
|
||||
struct number_val;
|
||||
struct boolean_val;
|
||||
struct object_val;
|
||||
struct array_val;
|
||||
|
||||
struct value
|
||||
{
|
||||
std::optional<std::string> string;
|
||||
@@ -33,6 +45,14 @@ namespace JJX::json {
|
||||
explicit operator std::map<std::string, value>() const { return object.value(); }
|
||||
|
||||
|
||||
[[nodiscard]] object_val as_object() const;
|
||||
[[nodiscard]] array_val as_array() const;
|
||||
[[nodiscard]] string_val as_string() const;
|
||||
[[nodiscard]] number_val as_number() const;
|
||||
[[nodiscard]] boolean_val as_boolean() const;
|
||||
|
||||
|
||||
void convert_descendants();
|
||||
};
|
||||
|
||||
struct string_val : value
|
||||
@@ -49,9 +69,6 @@ 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
|
||||
{
|
||||
|
42
main.cpp
42
main.cpp
@@ -105,7 +105,7 @@ product_info pinfo_fromjson(json::value root)
|
||||
|
||||
product_info this_product;
|
||||
|
||||
json::object_val better_root = static_cast<json::object_val>(root);
|
||||
auto better_root = root.as_object();
|
||||
|
||||
this_product.category = better_root["category"];
|
||||
this_product.description = better_root["description"];
|
||||
@@ -114,11 +114,12 @@ 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"]);
|
||||
auto subobj = better_root["reviews"].as_array();
|
||||
|
||||
|
||||
for (auto& review_data_pre : subobj.array.value())
|
||||
{
|
||||
json::object_val review_data = static_cast<json::object_val>(review_data_pre);
|
||||
auto review_data = static_cast<json::object_val>(review_data_pre);
|
||||
product_info::review going_in;
|
||||
going_in.rating = double(review_data["rating"]);
|
||||
going_in.user = review_data["user"];
|
||||
@@ -138,28 +139,45 @@ json::value pinfo_tojson(product_info input)
|
||||
//root.add("category", input.category);
|
||||
root.add("description", input.description);
|
||||
root["name"] = input.name;
|
||||
root["reviews"] = json::array();
|
||||
for (auto& rev : input.reviews) {
|
||||
root["reviews"].array.value().push_back(rev);
|
||||
}
|
||||
|
||||
|
||||
auto root_review_list = json::array();
|
||||
for (auto& rev : input.reviews) {
|
||||
auto review = json::object();
|
||||
|
||||
review["rating"] = double(rev.rating);
|
||||
review["review_text"] = rev.review_text;
|
||||
review["user"] = rev.user;
|
||||
|
||||
root_review_list.add(rev);
|
||||
}
|
||||
root["reviews"] = root_review_list;
|
||||
root["taters"] = 5.0;
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
|
||||
// Some progress has been made on small scale.
|
||||
int main(int argc, char* argv[]) {
|
||||
//parse_json_file("../samples/product_info.json");
|
||||
void test_product_info()
|
||||
{
|
||||
parse_json_file("../samples/product_info.json");
|
||||
auto file_contents = read_file("../samples/product_info.json");
|
||||
|
||||
auto [text, err] = json::parse(file_contents);
|
||||
|
||||
product_info test = pinfo_fromjson(text);
|
||||
|
||||
|
||||
auto result = pinfo_tojson(test);
|
||||
|
||||
result.convert_descendants();
|
||||
std::cout << json::deparse(result);
|
||||
}
|
||||
|
||||
void test_tile_data() {
|
||||
parse_json_file("../samples/tiles.json");
|
||||
}
|
||||
|
||||
|
||||
// Some progress has been made on small scale.
|
||||
int main(int argc, char* argv[]) {
|
||||
test_tile_data();
|
||||
}
|
||||
|
55
samples/tiles.json
Normal file
55
samples/tiles.json
Normal file
@@ -0,0 +1,55 @@
|
||||
[
|
||||
{
|
||||
"mnemonic-id" : "void",
|
||||
"display-name" : "VOID",
|
||||
"solid": true,
|
||||
"does-random-ticc": false,
|
||||
"does-forced-ticc": false,
|
||||
"color": "#FFFFFF",
|
||||
"pallet": [],
|
||||
"hardcoded-id": 65536
|
||||
},
|
||||
{
|
||||
"mnemonic-id" : "air",
|
||||
"display-name" : "AIR",
|
||||
"solid": true,
|
||||
"does-random-ticc": false,
|
||||
"does-forced-ticc": false,
|
||||
"color": "#FFFFFF",
|
||||
"pallet": [],
|
||||
"hardcoded-id": 0
|
||||
},
|
||||
{
|
||||
"mnemonic-id" : "stone",
|
||||
"display-name" : "",
|
||||
"solid": true,
|
||||
"does-random-ticc": false,
|
||||
"does-forced-ticc": false,
|
||||
"color": "#FFFFFF",
|
||||
"pallet": []
|
||||
},
|
||||
{
|
||||
"mnemonic-id" : "oak-wood-plank",
|
||||
"display-name" : "",
|
||||
"solid": true,
|
||||
"does-random-ticc": false,
|
||||
"does-forced-ticc": false,
|
||||
"color": "#FFFFFF",
|
||||
"pallet": [],
|
||||
"random-ticc-func": "zzyyzz",
|
||||
"forced-ticc-func": "zzyyzz",
|
||||
"drops" : null
|
||||
},
|
||||
{
|
||||
"mnemonic-id" : "gravel",
|
||||
"display-name" : "",
|
||||
"solid": true,
|
||||
"does-random-ticc": false,
|
||||
"does-forced-ticc": false,
|
||||
"color": "#FFFFFF",
|
||||
"pallet": [],
|
||||
"random-ticc-func": "zzyyzz",
|
||||
"forced-ticc-func": "zzyyzz",
|
||||
"drops" : null
|
||||
}
|
||||
]
|
76
src/JSON.cpp
76
src/JSON.cpp
@@ -203,6 +203,80 @@ namespace JJX::json {
|
||||
object = in;
|
||||
}
|
||||
|
||||
object_val value::as_object() const {
|
||||
return static_cast<object_val>(*this);
|
||||
}
|
||||
|
||||
array_val value::as_array() const {
|
||||
return static_cast<array_val>(*this);
|
||||
}
|
||||
|
||||
string_val value::as_string() const {
|
||||
return static_cast<string_val>(*this);
|
||||
}
|
||||
|
||||
number_val value::as_number() const {
|
||||
return static_cast<number_val>(*this);
|
||||
}
|
||||
|
||||
boolean_val value::as_boolean() const {
|
||||
return static_cast<boolean_val>(*this);
|
||||
}
|
||||
|
||||
void value::convert_descendants() {
|
||||
if (type == value_type::object)
|
||||
{
|
||||
for (auto& [k, v] : object.value())
|
||||
{
|
||||
if (v.type == value_type::object)
|
||||
{
|
||||
auto converted = v.as_object();
|
||||
converted.convert_descendants();
|
||||
object.value().emplace(k, converted);
|
||||
}
|
||||
|
||||
if (v.type == value_type::array)
|
||||
{
|
||||
auto converted = v.as_array();
|
||||
converted.convert_descendants();
|
||||
object.value().emplace(k, converted);
|
||||
}
|
||||
|
||||
|
||||
if (v.type == value_type::string)
|
||||
object.value().emplace(k, v.as_string());
|
||||
|
||||
if (v.type == value_type::number)
|
||||
object.value().emplace(k, v.as_number());
|
||||
|
||||
if (v.type == value_type::boolean)
|
||||
object.value().emplace(k, v.as_boolean());
|
||||
}
|
||||
}
|
||||
|
||||
if (type == value_type::array)
|
||||
{
|
||||
for (int i = 0; i < array.value().size(); ++i)
|
||||
{
|
||||
auto v = array.value()[i];
|
||||
if (v.type == value_type::object) {
|
||||
auto converted = v.as_object();
|
||||
converted.convert_descendants();
|
||||
array.value()[i] = converted;
|
||||
} else if (v.type == value_type::array) {
|
||||
auto converted = v.as_array();
|
||||
converted.convert_descendants();
|
||||
array.value()[i] = converted;
|
||||
} else if (v.type == value_type::string)
|
||||
array.value()[i] = v.as_string();
|
||||
else if (v.type == value_type::number)
|
||||
array.value()[i] = v.as_number();
|
||||
else if (v.type == value_type::boolean)
|
||||
array.value()[i] = v.as_boolean();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void string_val::operator=(const std::string& me)
|
||||
{
|
||||
string = me;
|
||||
@@ -351,7 +425,7 @@ namespace JJX::json {
|
||||
}
|
||||
}
|
||||
auto [key, new_index, error] = parse(tokens, index);
|
||||
if (error.size())
|
||||
if (!error.empty())
|
||||
{
|
||||
return {{}, index, error};
|
||||
}
|
||||
|
Reference in New Issue
Block a user