Skip to content

Commit

Permalink
Jzon: algorithm conversions
Browse files Browse the repository at this point in the history
Signed-off-by: Rosen Penev <[email protected]>
  • Loading branch information
neheb committed May 19, 2021
1 parent 17461a8 commit 3a41c9d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 86 deletions.
130 changes: 52 additions & 78 deletions samples/Jzon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,6 @@ namespace Jzon
return ((c >= '0' && c <= '9') || c == '.' || c == '-');
}

Node::Node() = default;
Node::~Node() = default;

Object &Node::AsObject()
{
if (IsObject())
Expand Down Expand Up @@ -184,8 +181,6 @@ namespace Jzon
{
Set(value);
}
Value::~Value() = default;

Node::Type Value::GetType() const
{
return T_VALUE;
Expand Down Expand Up @@ -432,18 +427,13 @@ namespace Jzon
}
Object::Object(const Object &other) : Node()
{
for (auto &&it : other.children) {
const std::string &name = it.first;
Node &value = *it.second;

children.push_back(NamedNodePtr(name, value.GetCopy()));
}
std::transform(other.children.begin(), other.children.end(), std::back_inserter(children),
[](const NamedNodePtr &child) { return NamedNodePtr(child.first, child.second->GetCopy()); });
}
Object::Object(const Node &other) : Node()
{
for (auto &&child : other.AsObject().children) {
children.push_back(NamedNodePtr(child.first, child.second->GetCopy()));
}
std::transform(other.AsObject().children.begin(), other.AsObject().children.end(), std::back_inserter(children),
[](const NamedNodePtr &child) { return NamedNodePtr(child.first, child.second->GetCopy()); });
}
Object::~Object()
{
Expand Down Expand Up @@ -638,7 +628,6 @@ namespace Jzon
FileWriter::FileWriter(std::string filename) : filename(std::move(filename))
{
}
FileWriter::~FileWriter() = default;

void FileWriter::WriteFile(const std::string &filename, const Node &root, const Format &format)
{
Expand All @@ -664,7 +653,6 @@ namespace Jzon
error = "Failed to load file";
}
}
FileReader::~FileReader() = default;

bool FileReader::ReadFile(const std::string &filename, Node &node)
{
Expand Down Expand Up @@ -755,56 +743,49 @@ namespace Jzon
{
result += "{" + fi->GetNewline();

for (Object::const_iterator it = node.begin(); it != node.end(); ++it)
{
const std::string &name = (*it).first;
// const Node &value = (*it).second;
for (auto it = node.begin(); it != node.end(); ++it) {
const std::string &name = (*it).first;
// const Node &value = (*it).second;

if (it != node.begin())
result += "," + fi->GetNewline();
result += fi->GetIndentation(level+1) + "\""+name+"\"" + ":" + fi->GetSpacing();
writeNode((*it).second, level+1);
}

result += fi->GetNewline() + fi->GetIndentation(level) + "}";
}
void Writer::writeArray(const Array &node, unsigned int level)
{
result += "[" + fi->GetNewline();
if (it != node.begin())
result += "," + fi->GetNewline();
result += fi->GetIndentation(level + 1) + "\"" + name + "\"" + ":" + fi->GetSpacing();
writeNode((*it).second, level + 1);
}

for (Array::const_iterator it = node.begin(); it != node.end(); ++it)
{
const Node &value = (*it);
result += fi->GetNewline() + fi->GetIndentation(level) + "}";
}
void Writer::writeArray(const Array &node, unsigned int level)
{
result += "[" + fi->GetNewline();

if (it != node.begin())
result += "," + fi->GetNewline();
result += fi->GetIndentation(level+1);
writeNode(value, level+1);
}
for (auto it = node.begin(); it != node.end(); ++it) {
const Node &value = (*it);

result += fi->GetNewline() + fi->GetIndentation(level) + "]";
}
void Writer::writeValue(const Value &node)
{
if (node.IsString())
{
result += "\""+Value::EscapeString(node.ToString())+"\"";
}
else
{
result += node.ToString();
}
}
if (it != node.begin())
result += "," + fi->GetNewline();
result += fi->GetIndentation(level + 1);
writeNode(value, level + 1);
}

result += fi->GetNewline() + fi->GetIndentation(level) + "]";
}
void Writer::writeValue(const Value &node)
{
if (node.IsString()) {
result += "\"" + Value::EscapeString(node.ToString()) + "\"";
} else {
result += node.ToString();
}
}

Parser::Parser(Node &root) : jsonSize(0), cursor(0), root(root)
{
}
Parser::Parser(Node &root, const std::string &json) : jsonSize(0), cursor(0), root(root)
{
SetJson(json);
}
Parser::~Parser() = default;
Parser::Parser(Node &root) : root(root)
{
}
Parser::Parser(Node &root, const std::string &json) : root(root)
{
SetJson(json);
}

void Parser::SetJson(const std::string &json)
{
Expand Down Expand Up @@ -1165,24 +1146,17 @@ namespace Jzon
}
bool Parser::interpretValue(const std::string &value)
{
std::string upperValue(value.size(), '\0');

std::transform(value.begin(), value.end(), upperValue.begin(), toupper);

if (upperValue == "NULL")
{
data.push(std::make_pair(Value::VT_NULL, std::string("")));
}
else if (upperValue == "TRUE")
{
data.push(std::make_pair(Value::VT_BOOL, std::string("true")));
}
else if (upperValue == "FALSE")
{
data.push(std::make_pair(Value::VT_BOOL, std::string("false")));
}
else
{
std::string upperValue;
upperValue.reserve(value.size());
std::transform(value.begin(), value.end(), upperValue.begin(), toupper);

if (upperValue == "NULL") {
data.push(std::make_pair(Value::VT_NULL, std::string("")));
} else if (upperValue == "TRUE") {
data.push(std::make_pair(Value::VT_BOOL, std::string("true")));
} else if (upperValue == "FALSE") {
data.push(std::make_pair(Value::VT_BOOL, std::string("false")));
} else {
bool number = std::all_of(value.begin(), value.end(), IsNumber);
if (!number) {
return false;
Expand Down
16 changes: 8 additions & 8 deletions samples/Jzon.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ namespace Jzon
T_VALUE
};

Node();
virtual ~Node();
Node() noexcept = default;
virtual ~Node() noexcept = default;

virtual Type GetType() const = 0;

Expand Down Expand Up @@ -142,7 +142,7 @@ namespace Jzon
Value(const float value);
Value(const double value);
Value(const bool value);
~Value() override;
~Value() override = default;

Type GetType() const override;
ValueType GetValueType() const;
Expand Down Expand Up @@ -344,7 +344,7 @@ namespace Jzon
{
public:
FileWriter(std::string filename);
~FileWriter();
~FileWriter() = default;

static void WriteFile(const std::string &filename, const Node &root, const Format &format = NoFormat);

Expand All @@ -358,7 +358,7 @@ namespace Jzon
{
public:
FileReader(const std::string &filename);
~FileReader();
~FileReader() = default;

static bool ReadFile(const std::string &filename, Node &node);

Expand Down Expand Up @@ -406,7 +406,7 @@ namespace Jzon
public:
Parser(Node &root);
Parser(Node &root, const std::string &json);
~Parser();
~Parser() = default;

void SetJson(const std::string &json);
bool Parse();
Expand Down Expand Up @@ -439,12 +439,12 @@ namespace Jzon
bool interpretValue(const std::string &value);

std::string json;
std::size_t jsonSize;
std::size_t jsonSize{0};

std::queue<Token> tokens;
std::queue<std::pair<Value::ValueType, std::string> > data;

std::size_t cursor;
std::size_t cursor{0};

Node &root;

Expand Down

0 comments on commit 3a41c9d

Please sign in to comment.