Working Json Parsing system

main
anulax1225 ago%!(EXTRA string=11 months)
parent 4c92c8db77
commit 7fb4812d0b
  1. 5
      src/bakatools/file_system/directory.cpp
  2. 1
      src/bakatools/file_system/directory.h
  3. 48
      src/bakatools/json/json_node.cpp
  4. 6
      src/bakatools/json/json_node.h
  5. 40
      src/bakatools/json/json_parser.cpp
  6. 4
      src/bakatools/json/json_parser.h

@ -1,6 +1,11 @@
#include "directory.h"
namespace Bk {
bool Directory::create()
{
return std::filesystem::create_directory(ent.path());
}
std::string Directory::current()
{
return std::filesystem::current_path();

@ -15,6 +15,7 @@ namespace Bk {
virtual ~Directory() {}
bool create();
int remove() override;
void copy(std::string path, CopyOption opt = CopyOption::recursive) override;

@ -1,10 +1,12 @@
#include "json_node.h"
namespace Bk::Json
{
std::string Node::to_string(int indent) {
std::string Node::to_string(int indent)
{
std::string space_string = std::string(indent, ' ');
std::string output_string = "";
switch (type) {
switch (type)
{
case Type::STRING:
{
output_string += '"' + *values.s + '"';
@ -27,15 +29,13 @@ namespace Bk::Json
}
case Type::LIST:
{
output_string += "\n" + space_string + "[\n";;
for (int i = 0; i < (*values.list).size() - 1; i++)
output_string += space_string + "[\n";
for (int i = 0; i < (*values.list).size(); i++)
{
output_string += get_list()[i]->to_string(indent + 4);
if (i < (*values.list).size() - 2)
if (i < (*values.list).size() - 1)
{
output_string += ",\n";
} else {
break;
}
}
output_string += "\n" + space_string + "]";
@ -67,6 +67,13 @@ namespace Bk::Json
type = Type::OBJECT;
}
Object* Node::get_p_object()
{
if (type == Type::OBJECT)
return values.object;
throw std::logic_error("Improper return");
}
Object Node::get_object()
{
if (type == Type::OBJECT)
@ -80,6 +87,13 @@ namespace Bk::Json
type = Type::LIST;
}
List* Node::get_p_list()
{
if (type == Type::LIST)
return values.list;
throw std::logic_error("Improper return");
}
List Node::get_list()
{
if (type == Type::LIST)
@ -93,6 +107,13 @@ namespace Bk::Json
type = Type::STRING;
}
std::string* Node::get_p_string()
{
if (type == Type::STRING)
return values.s;
throw std::logic_error("Improper return");
}
std::string Node::get_string()
{
if (type == Type::STRING)
@ -126,6 +147,19 @@ namespace Bk::Json
throw std::logic_error("Improper return");
}
void Node::set_int(int value)
{
values.fValue = value;
type = Type::NUMBER;
}
int Node::get_int()
{
if (type == Type::NUMBER)
return (int)values.fValue;
throw std::logic_error("Improper return");
}
void Node::set_null()
{
type = Type::NULL_TYPE;

@ -22,12 +22,15 @@ namespace Bk::Json
NULL_TYPE
};
void set_object(Object* object);
Object* get_p_object();
Object get_object();
void set_list(List* list);
List* get_p_list();
List get_list();
void set_string(std::string* str);
std::string* get_p_string();
std::string get_string();
void set_bool(bool value);
@ -36,6 +39,9 @@ namespace Bk::Json
void set_float(float value);
float get_float();
void set_int(int value);
int get_int();
void set_null();
bool is_null();

@ -16,7 +16,7 @@ namespace Bk::Json
Parser::Parser(const char* str, int length)
{
Type::DataStream data;
data.payload = std::vector<char>(str, str + length);
data.push<char>(str, length);
data.reverse();
lexer = Lexer(data);
}
@ -41,36 +41,31 @@ namespace Bk::Json
case TokenType::CURLY_OPEN:
{
std::shared_ptr<Json::Node> parsed_object = parse_object();
if (!root) {
root = parsed_object;
}
root = parsed_object;
break;
}
case TokenType::ARRAY_OPEN: {
case TokenType::ARRAY_OPEN:
{
std::shared_ptr<Json::Node> parsed_list = parse_list();
if (!root) {
root = parsed_list;
}
root = parsed_list;
break;
}
case TokenType::STRING: {
case TokenType::STRING:
{
std::shared_ptr<Json::Node> parsed_string = parse_string();
if (!root) {
root = parsed_string;
}
root = parsed_string;
break;
}
case TokenType::NUMBER: {
case TokenType::NUMBER:
{
std::shared_ptr<Json::Node> parsed_number = parse_number();
if (!root) {
root = parsed_number;
}
root = parsed_number;
break;
}
case TokenType::BOOLEAN: {
case TokenType::BOOLEAN:
{
std::shared_ptr<Json::Node> parsed_boolean = parse_boolean();
break;
}
@ -91,7 +86,7 @@ namespace Bk::Json
std::shared_ptr<Json::Node> Parser::parse_list()
{
std::shared_ptr<Json::Node> node = std::make_shared<Json::Node>();
Json::List *list = new Json::List();
Json::List* list = new Json::List();
bool has_completed = false;
Token next_token;
while (!has_completed) {
@ -104,6 +99,10 @@ namespace Bk::Json
next_token = lexer.get_token();
if (next_token.type == TokenType::COLON || next_token.type == TokenType::COMMA)
continue;
if (next_token.type == TokenType::ARRAY_CLOSE) {
has_completed = true;
break;
}
std::shared_ptr<Json::Node> node;
switch (next_token.type)
{
@ -138,9 +137,6 @@ namespace Bk::Json
}
}
list->push_back(node);
if (next_token.type == TokenType::ARRAY_CLOSE) {
has_completed = true;
}
}
}
node->set_list(list);

@ -5,8 +5,8 @@
namespace Bk::Json
{
class Parser {
Type::DataStream data;
class Parser
{
std::shared_ptr<Json::Node> root;
std::unique_ptr<Json::Node> current;
Lexer lexer;

Loading…
Cancel
Save