Working on making this lib more usable by usage-testing.
This commit is contained in:
@@ -17,6 +17,7 @@ namespace JJX
|
||||
int location;
|
||||
std::shared_ptr<std::string> full_source;
|
||||
};
|
||||
|
||||
struct value {
|
||||
std::optional<std::string> string;
|
||||
std::optional<double> number;
|
||||
@@ -26,26 +27,28 @@ namespace JJX
|
||||
value_type type;
|
||||
};
|
||||
|
||||
struct string_value
|
||||
{
|
||||
std::string string;
|
||||
};
|
||||
struct
|
||||
struct string_val: value { std::string data; };
|
||||
|
||||
struct number_val : value { double data; };
|
||||
|
||||
struct boolean_val : value { bool data; };
|
||||
|
||||
struct object_val : value { std::map<std::string, value> data; };
|
||||
|
||||
struct array_val : value { std::vector<value> data; };
|
||||
|
||||
std::tuple<std::vector<json::token>, std::string> lex(std::string);
|
||||
std::tuple<json::value, int, std::string> parse(std::vector<json::token>, int index = 0);
|
||||
std::tuple<json::value, std::string> parse(std::string);
|
||||
std::string deparse(json::value, std::string whitespace = "");
|
||||
|
||||
|
||||
value string(const std::string& text);
|
||||
|
||||
value number(double input);
|
||||
|
||||
value boolean(bool input);
|
||||
|
||||
value array(const std::vector<value>& input);
|
||||
value array(std::vector<value> input);
|
||||
|
||||
template <typename T>
|
||||
value array_of(const std::vector<T>& input);
|
||||
|
19
main.cpp
19
main.cpp
@@ -4,7 +4,8 @@
|
||||
|
||||
using namespace JJX;
|
||||
|
||||
void ParseJsonFile(const std::string& file_path) {
|
||||
std::string read_file(const std::string& file_path)
|
||||
{
|
||||
std::ifstream file(file_path, std::ios::binary);
|
||||
if (!file)
|
||||
throw std::runtime_error("We couldn't find the file: " + file_path);
|
||||
@@ -18,10 +19,18 @@ void ParseJsonFile(const std::string& file_path) {
|
||||
file.read(&file_content[0], file_size);
|
||||
file.close();
|
||||
|
||||
return file_content;
|
||||
}
|
||||
|
||||
|
||||
void parse_json_file(const std::string& file_path) {
|
||||
auto file_content = read_file(file_path);
|
||||
|
||||
auto [text, parse_error] = json::parse(file_content);
|
||||
if (!parse_error.empty())
|
||||
std::cerr << "Error while parsing json: " << parse_error << std::endl;
|
||||
|
||||
|
||||
std::cout << json::deparse(text) << std::endl;
|
||||
}
|
||||
|
||||
@@ -30,10 +39,14 @@ struct Vector3
|
||||
float x, y, z;
|
||||
};
|
||||
|
||||
|
||||
/// Creates json array out of Vector3 type.
|
||||
json::value vector3_to_json(const Vector3& input)
|
||||
{
|
||||
return json::array({json::number(input.x), json::number(input.y), json::number(input.z)});
|
||||
auto x = json::number(input.x);
|
||||
auto y = json::number(input.y);
|
||||
auto z = json::number(input.z);
|
||||
return json::array({x, y, z});
|
||||
}
|
||||
|
||||
/// Creates Vector3 type out of json array object.
|
||||
@@ -49,7 +62,7 @@ Vector3 json_to_vector3(const json::value& obj)
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
ParseJsonFile("../samples/vector3.json");
|
||||
parse_json_file("../samples/product_info.json");
|
||||
|
||||
|
||||
std::cout << json::deparse(vector3_to_json({2,4.5f, 31}));
|
||||
|
16
samples/months.json
Normal file
16
samples/months.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"months": [
|
||||
"January",
|
||||
"February",
|
||||
"March",
|
||||
"April",
|
||||
"May",
|
||||
"June",
|
||||
"July",
|
||||
"August",
|
||||
"September",
|
||||
"October",
|
||||
"November",
|
||||
"December"
|
||||
]
|
||||
}
|
45
samples/product_info.json
Normal file
45
samples/product_info.json
Normal file
@@ -0,0 +1,45 @@
|
||||
{
|
||||
|
||||
"product_id": "SKU12345",
|
||||
|
||||
"name": "Example Product",
|
||||
|
||||
"description": "This is an example product.",
|
||||
|
||||
"category": "Electronics",
|
||||
|
||||
"price": 199.99,
|
||||
|
||||
"stock_quantity": 50,
|
||||
|
||||
"manufacturer": "TechCo",
|
||||
|
||||
"release_date": "2022-05-10",
|
||||
|
||||
"is_available": true,
|
||||
|
||||
"ratings": {
|
||||
|
||||
"average": 4.5,
|
||||
|
||||
"total": 100
|
||||
|
||||
},
|
||||
"reviews": [
|
||||
{
|
||||
"user": "user123",
|
||||
"rating": 5,
|
||||
"review_text": "Great product, highly recommended!"
|
||||
},
|
||||
{
|
||||
"user": "user456",
|
||||
"rating": 4,
|
||||
"review_text": "Good value for the price."
|
||||
},
|
||||
{
|
||||
"user": "user450",
|
||||
"rating": 4,
|
||||
"review_text": "Good value for the price."
|
||||
}
|
||||
]
|
||||
}
|
6
samples/test.xml
Normal file
6
samples/test.xml
Normal file
@@ -0,0 +1,6 @@
|
||||
<note>
|
||||
<to>Tove</to>
|
||||
<from>Jani</from>
|
||||
<heading>Reminder</heading>
|
||||
<body>Don't forget me this weekend!</body>
|
||||
</note>
|
@@ -421,10 +421,11 @@ namespace JJX::json {
|
||||
};
|
||||
}
|
||||
|
||||
value array(const std::vector<value>& input)
|
||||
value array(std::vector<value> input)
|
||||
{
|
||||
value arr;
|
||||
arr.type = value_type::array;
|
||||
arr.array = std::vector<value>();
|
||||
|
||||
for (auto& elem: input)
|
||||
{
|
||||
|
Reference in New Issue
Block a user