Jzon: algorithm conversions

Signed-off-by: Rosen Penev <rosenp@gmail.com>
main
Rosen Penev 4 years ago committed by Luis Díaz Más
parent 4d2bc3c752
commit ceed79c95e

@ -87,9 +87,6 @@ namespace Jzon
return ((c >= '0' && c <= '9') || c == '.' || c == '-'); return ((c >= '0' && c <= '9') || c == '.' || c == '-');
} }
Node::Node() = default;
Node::~Node() = default;
Object &Node::AsObject() Object &Node::AsObject()
{ {
if (IsObject()) if (IsObject())
@ -184,8 +181,6 @@ namespace Jzon
{ {
Set(value); Set(value);
} }
Value::~Value() = default;
Node::Type Value::GetType() const Node::Type Value::GetType() const
{ {
return T_VALUE; return T_VALUE;
@ -436,18 +431,13 @@ namespace Jzon
} }
Object::Object(const Object &other) : Node() Object::Object(const Object &other) : Node()
{ {
for (auto &&it : other.children) { std::transform(other.children.begin(), other.children.end(), std::back_inserter(children),
const std::string &name = it.first; [](const NamedNodePtr &child) { return NamedNodePtr(child.first, child.second->GetCopy()); });
Node &value = *it.second;
children.push_back(NamedNodePtr(name, value.GetCopy()));
}
} }
Object::Object(const Node &other) : Node() Object::Object(const Node &other) : Node()
{ {
for (auto &&child : other.AsObject().children) { std::transform(other.AsObject().children.begin(), other.AsObject().children.end(), std::back_inserter(children),
children.push_back(NamedNodePtr(child.first, child.second->GetCopy())); [](const NamedNodePtr &child) { return NamedNodePtr(child.first, child.second->GetCopy()); });
}
} }
Object::~Object() Object::~Object()
{ {
@ -642,7 +632,6 @@ namespace Jzon
FileWriter::FileWriter(std::string filename) : filename(std::move(filename)) 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) void FileWriter::WriteFile(const std::string &filename, const Node &root, const Format &format)
{ {
@ -668,7 +657,6 @@ namespace Jzon
error = "Failed to load file"; error = "Failed to load file";
} }
} }
FileReader::~FileReader() = default;
bool FileReader::ReadFile(const std::string &filename, Node &node) bool FileReader::ReadFile(const std::string &filename, Node &node)
{ {
@ -759,56 +747,49 @@ namespace Jzon
{ {
result += "{" + fi->GetNewline(); result += "{" + fi->GetNewline();
for (Object::const_iterator it = node.begin(); it != node.end(); ++it) for (auto it = node.begin(); it != node.end(); ++it) {
{ const std::string &name = (*it).first;
const std::string &name = (*it).first; // const Node &value = (*it).second;
// const Node &value = (*it).second;
if (it != node.begin()) if (it != node.begin())
result += "," + fi->GetNewline(); result += "," + fi->GetNewline();
result += fi->GetIndentation(level+1) + "\""+name+"\"" + ":" + fi->GetSpacing(); result += fi->GetIndentation(level + 1) + "\"" + name + "\"" + ":" + fi->GetSpacing();
writeNode((*it).second, level+1); writeNode((*it).second, level + 1);
} }
result += fi->GetNewline() + fi->GetIndentation(level) + "}";
}
void Writer::writeArray(const Array &node, unsigned int level)
{
result += "[" + fi->GetNewline();
for (Array::const_iterator it = node.begin(); it != node.end(); ++it) result += fi->GetNewline() + fi->GetIndentation(level) + "}";
{ }
const Node &value = (*it); void Writer::writeArray(const Array &node, unsigned int level)
{
result += "[" + fi->GetNewline();
if (it != node.begin()) for (auto it = node.begin(); it != node.end(); ++it) {
result += "," + fi->GetNewline(); const Node &value = (*it);
result += fi->GetIndentation(level+1);
writeNode(value, level+1);
}
result += fi->GetNewline() + fi->GetIndentation(level) + "]"; if (it != node.begin())
} result += "," + fi->GetNewline();
void Writer::writeValue(const Value &node) result += fi->GetIndentation(level + 1);
{ writeNode(value, level + 1);
if (node.IsString()) }
{
result += "\""+Value::EscapeString(node.ToString())+"\"";
}
else
{
result += node.ToString();
}
}
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) : root(root)
{ {
} }
Parser::Parser(Node &root, const std::string &json) : jsonSize(0), cursor(0), root(root) Parser::Parser(Node &root, const std::string &json) : root(root)
{ {
SetJson(json); SetJson(json);
} }
Parser::~Parser() = default;
void Parser::SetJson(const std::string &json) void Parser::SetJson(const std::string &json)
{ {
@ -1169,24 +1150,17 @@ namespace Jzon
} }
bool Parser::interpretValue(const std::string &value) bool Parser::interpretValue(const std::string &value)
{ {
std::string upperValue(value.size(), '\0'); std::string upperValue;
upperValue.reserve(value.size());
std::transform(value.begin(), value.end(), upperValue.begin(), toupper); std::transform(value.begin(), value.end(), upperValue.begin(), toupper);
if (upperValue == "NULL") if (upperValue == "NULL") {
{ data.push(std::make_pair(Value::VT_NULL, std::string("")));
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 == "TRUE") } else if (upperValue == "FALSE") {
{ data.push(std::make_pair(Value::VT_BOOL, std::string("false")));
data.push(std::make_pair(Value::VT_BOOL, std::string("true"))); } else {
}
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); bool number = std::all_of(value.begin(), value.end(), IsNumber);
if (!number) { if (!number) {
return false; return false;

@ -83,8 +83,8 @@ namespace Jzon
T_VALUE T_VALUE
}; };
Node(); Node() noexcept = default;
virtual ~Node(); virtual ~Node() noexcept = default;
virtual Type GetType() const = 0; virtual Type GetType() const = 0;
@ -142,7 +142,7 @@ namespace Jzon
Value(const float value); Value(const float value);
Value(const double value); Value(const double value);
Value(const bool value); Value(const bool value);
~Value() override; ~Value() override = default;
Type GetType() const override; Type GetType() const override;
ValueType GetValueType() const; ValueType GetValueType() const;
@ -344,7 +344,7 @@ namespace Jzon
{ {
public: public:
FileWriter(std::string filename); FileWriter(std::string filename);
~FileWriter(); ~FileWriter() = default;
static void WriteFile(const std::string &filename, const Node &root, const Format &format = NoFormat); static void WriteFile(const std::string &filename, const Node &root, const Format &format = NoFormat);
@ -358,7 +358,7 @@ namespace Jzon
{ {
public: public:
FileReader(const std::string &filename); FileReader(const std::string &filename);
~FileReader(); ~FileReader() = default;
static bool ReadFile(const std::string &filename, Node &node); static bool ReadFile(const std::string &filename, Node &node);
@ -406,7 +406,7 @@ namespace Jzon
public: public:
Parser(Node &root); Parser(Node &root);
Parser(Node &root, const std::string &json); Parser(Node &root, const std::string &json);
~Parser(); ~Parser() = default;
void SetJson(const std::string &json); void SetJson(const std::string &json);
bool Parse(); bool Parse();
@ -439,12 +439,12 @@ namespace Jzon
bool interpretValue(const std::string &value); bool interpretValue(const std::string &value);
std::string json; std::string json;
std::size_t jsonSize; std::size_t jsonSize{0};
std::queue<Token> tokens; std::queue<Token> tokens;
std::queue<std::pair<Value::ValueType, std::string> > data; std::queue<std::pair<Value::ValueType, std::string> > data;
std::size_t cursor; std::size_t cursor{0};
Node &root; Node &root;

Loading…
Cancel
Save