Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jzon changes #1666

Merged
merged 2 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 75 additions & 96 deletions samples/Jzon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ THE SOFTWARE.

#include "Jzon.h"

#include <sstream>
#include <algorithm>
#include <array>
#include <fstream>
#include <sstream>
#include <stack>
#include <algorithm>

namespace Jzon
{
Expand Down Expand Up @@ -86,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 @@ -183,8 +181,6 @@ namespace Jzon
{
Set(value);
}
Value::~Value() = default;

Node::Type Value::GetType() const
{
return T_VALUE;
Expand Down Expand Up @@ -357,27 +353,31 @@ namespace Jzon
}

// This is not the most beautiful place for these, but it'll do
static const char charsUnescaped[] = {'\\', '/', '\"', '\n', '\t', '\b', '\f', '\r'};
static const char *charsEscaped[] = {"\\\\", "\\/", "\\\"", "\\n", "\\t", "\\b", "\\f", "\\r"};
static const unsigned int numEscapeChars = 8;
static const char nullUnescaped = '\0';
static const char *nullEscaped = "\0\0";
const char *&getEscaped(const char &c)
{
for (unsigned int i = 0; i < numEscapeChars; ++i) {
if (c == charsUnescaped[i]) {
return charsEscaped[i];
using chrPair = struct
{
char first;
const char *second;
};
static constexpr std::array<chrPair, 8> chars{
chrPair{'\\', "\\\\"}, chrPair{'/', "\\/"}, chrPair{'\"', "\\\""}, chrPair{'\n', "\\n"},
chrPair{'\t', "\\t"}, chrPair{'\b', "\\b"}, chrPair{'\f', "\\f"}, chrPair{'\r', "\\r"},
};
static constexpr char nullUnescaped = '\0';
static constexpr const char *nullEscaped = "\0\0";
const char *const &getEscaped(const char &c)
{
for (auto &&chr : chars) {
if (chr.first == c) {
return chr.second;
}
}
return nullEscaped;
}
const char &getUnescaped(const char &c1, const char &c2)
{
for (unsigned int i = 0; i < numEscapeChars; ++i) {
const char *&e = charsEscaped[i];

if (c1 == e[0] && c2 == e[1]) {
return charsUnescaped[i];
for (auto &&chr : chars) {
if (c1 == chars[0].first && c2 == chars[1].first) {
return chr.first;
}
}
return nullUnescaped;
Expand All @@ -388,7 +388,7 @@ namespace Jzon
std::string escaped;

for (auto &&c : value) {
const char *&a = getEscaped(c);
auto &&a = getEscaped(c);
if (a[0] != '\0') {
escaped += a[0];
escaped += a[1];
Expand Down Expand Up @@ -431,18 +431,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 @@ -637,7 +632,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 @@ -663,7 +657,6 @@ namespace Jzon
error = "Failed to load file";
}
}
FileReader::~FileReader() = default;

bool FileReader::ReadFile(const std::string &filename, Node &node)
{
Expand Down Expand Up @@ -754,56 +747,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;

if (it != node.begin())
result += "," + fi->GetNewline();
result += fi->GetIndentation(level+1) + "\""+name+"\"" + ":" + fi->GetSpacing();
writeNode((*it).second, level+1);
}
for (auto it = node.begin(); it != node.end(); ++it) {
const std::string &name = (*it).first;
// const Node &value = (*it).second;

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 @@ -1164,24 +1150,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