Working on making this lib more usable by usage-testing.

This commit is contained in:
2024-11-04 16:40:57 -05:00
parent ddc5d3fee7
commit 42fd86bdf1
6 changed files with 108 additions and 3 deletions

View File

@@ -25,10 +25,30 @@ namespace JJX
std::optional<std::map<std::string, value>> object;
value_type type;
};
struct string_value
{
std::string string;
};
struct
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);
template <typename T>
value array_of(const std::vector<T>& input);
}
namespace xml {}
}

View File

@@ -4,14 +4,12 @@
namespace JJX {
class ParsedValue;
class ParsedObject;
class ParsedString;
class ParsedNumber;
class ParsedArray;
class ParsedBool;
class ParsedNULL;
class ParsedFile;
}

View File

@@ -23,11 +23,34 @@ void ParseJsonFile(const std::string& file_path) {
std::cerr << "Error while parsing json: " << parse_error << std::endl;
std::cout << json::deparse(text) << std::endl;
}
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)});
}
/// Creates Vector3 type out of json array object.
Vector3 json_to_vector3(const json::value& obj)
{
Vector3 value;
value.x = obj.array->at(0).number.value_or(0);
value.y = obj.array->at(1).number.value_or(0);
value.z = obj.array->at(2).number.value_or(0);
return value;
}
int main(int argc, char* argv[]) {
ParseJsonFile("../samples/widgets.json");
ParseJsonFile("../samples/vector3.json");
std::cout << json::deparse(vector3_to_json({2,4.5f, 31}));
}

27
samples/menu.json Normal file
View File

@@ -0,0 +1,27 @@
{"menu": {
"header": "SVG Viewer",
"items": [
{"id": "Open"},
{"id": "OpenNew", "label": "Open New"},
null,
{"id": "ZoomIn", "label": "Zoom In"},
{"id": "ZoomOut", "label": "Zoom Out"},
{"id": "OriginalView", "label": "Original View"},
null,
{"id": "Quality"},
{"id": "Pause"},
{"id": "Mute"},
null,
{"id": "Find", "label": "Find..."},
{"id": "FindAgain", "label": "Find Again"},
{"id": "Copy"},
{"id": "CopyAgain", "label": "Copy Again"},
{"id": "CopySVG", "label": "Copy SVG"},
{"id": "ViewSVG", "label": "View SVG"},
{"id": "ViewSource", "label": "View Source"},
{"id": "SaveAs", "label": "Save As"},
null,
{"id": "Help"},
{"id": "About", "label": "About Adobe CVG Viewer..."}
]
}}

3
samples/vector3.json Normal file
View File

@@ -0,0 +1,3 @@
{
"position": [0, 0, 0]
}

View File

@@ -399,4 +399,38 @@ namespace JJX::json {
}
}
}
value string(const std::string &text) {
return {
.string = text,
.type = value_type::string
};
}
value number(double input) {
return {
.number = input,
.type = value_type::number
};
}
value boolean(bool input) {
return {
.boolean = input,
.type = value_type::boolean
};
}
value array(const std::vector<value>& input)
{
value arr;
arr.type = value_type::array;
for (auto& elem: input)
{
arr.array->push_back(elem);
}
return arr;
}
}