Browse Source

Util: Rename Json::Value m_value union variables

master
Riyyi 2 years ago
parent
commit
d6cd90ae4a
  1. 6
      src/util/json/parser.cpp
  2. 10
      src/util/json/serializer.cpp
  3. 28
      src/util/json/tojson.h
  4. 66
      src/util/json/value.cpp
  5. 20
      src/util/json/value.h

6
src/util/json/parser.cpp

@ -338,7 +338,7 @@ Value Parser::getArray()
} }
else if (token.type == Token::Type::BracketClose) { else if (token.type == Token::Type::BracketClose) {
// Trailing comma // Trailing comma
if (array.m_value.asArray->size() > 0) { if (array.m_value.array->size() > 0) {
reportError(m_tokens->at(m_index - 1), "invalid comma, expecting ']'"); reportError(m_tokens->at(m_index - 1), "invalid comma, expecting ']'");
break; break;
} }
@ -396,7 +396,7 @@ Value Parser::getObject()
token = consume(); token = consume();
if (token.type == Token::Type::BraceClose) { if (token.type == Token::Type::BraceClose) {
// Trailing comma // Trailing comma
if (object.m_value.asObject->size() > 0) { if (object.m_value.object->size() > 0) {
reportError(m_tokens->at(m_index - 1), "invalid comma, expecting '}'"); reportError(m_tokens->at(m_index - 1), "invalid comma, expecting '}'");
} }
// Empty object // Empty object
@ -416,7 +416,7 @@ Value Parser::getObject()
} }
// Check if name exists in hashmap // Check if name exists in hashmap
name = *tmpName.m_value.asString; name = *tmpName.m_value.string;
if (unique.find(name) != unique.end()) { if (unique.find(name) != unique.end()) {
reportError(token, "duplicate name '" + token.symbol + "', names should be unique"); reportError(token, "duplicate name '" + token.symbol + "', names should be unique");
break; break;

10
src/util/json/serializer.cpp

@ -43,16 +43,16 @@ std::string Serializer::dumpHelper(const Value& value, const uint32_t indentLeve
return "null"; return "null";
break; break;
case Value::Type::Bool: case Value::Type::Bool:
return value.m_value.asBool ? "true" : "false"; return value.m_value.boolean ? "true" : "false";
break; break;
case Value::Type::Number: { case Value::Type::Number: {
std::ostringstream os; std::ostringstream os;
os << value.m_value.asDouble; os << value.m_value.number;
return os.str(); return os.str();
break; break;
} }
case Value::Type::String: case Value::Type::String:
return "\"" + *value.m_value.asString + "\""; return "\"" + *value.m_value.string + "\"";
break; break;
case Value::Type::Array: case Value::Type::Array:
return dumpArray(value, indentLevel); return dumpArray(value, indentLevel);
@ -77,7 +77,7 @@ std::string Serializer::dumpArray(const Value& value, const uint32_t indentLevel
result += '\n'; result += '\n';
} }
auto values = value.m_value.asArray->elements(); auto values = value.m_value.array->elements();
for (auto it = values.begin(); it != values.end(); ++it) { for (auto it = values.begin(); it != values.end(); ++it) {
result += std::string(m_indent * (indentLevel + 1), m_indentCharacter); result += std::string(m_indent * (indentLevel + 1), m_indentCharacter);
result += dumpHelper(*it, indentLevel + 1); result += dumpHelper(*it, indentLevel + 1);
@ -110,7 +110,7 @@ std::string Serializer::dumpObject(const Value& value, const uint32_t indentLeve
result += '\n'; result += '\n';
} }
auto members = value.m_value.asObject->members(); auto members = value.m_value.object->members();
for (auto it = members.begin(); it != members.end(); ++it) { for (auto it = members.begin(); it != members.end(); ++it) {
result += std::string(m_indent * (indentLevel + 1), m_indentCharacter); result += std::string(m_indent * (indentLevel + 1), m_indentCharacter);
result += "\"" + it->first + "\":"; result += "\"" + it->first + "\":";

28
src/util/json/tojson.h

@ -28,7 +28,7 @@ struct jsonConstructor {
{ {
json.clear(); json.clear();
json.m_type = Json::Type::Bool; json.m_type = Json::Type::Bool;
json.m_value.asBool = boolean; json.m_value.boolean = boolean;
} }
template<typename Json> template<typename Json>
@ -36,7 +36,7 @@ struct jsonConstructor {
{ {
json.clear(); json.clear();
json.m_type = Json::Type::Number; json.m_type = Json::Type::Number;
json.m_value.asDouble = (double)number; json.m_value.number = (double)number;
} }
template<typename Json> template<typename Json>
@ -44,7 +44,7 @@ struct jsonConstructor {
{ {
json.clear(); json.clear();
json.m_type = Json::Type::Number; json.m_type = Json::Type::Number;
json.m_value.asDouble = number; json.m_value.number = number;
} }
template<typename Json> template<typename Json>
@ -52,7 +52,7 @@ struct jsonConstructor {
{ {
json.clear(); json.clear();
json.m_type = Json::Type::String; json.m_type = Json::Type::String;
json.m_value.asString = new std::string(string); json.m_value.string = new std::string(string);
} }
template<typename Json> template<typename Json>
@ -60,7 +60,7 @@ struct jsonConstructor {
{ {
json.clear(); json.clear();
json.m_type = Json::Type::String; json.m_type = Json::Type::String;
json.m_value.asString = new std::string(string); json.m_value.string = new std::string(string);
} }
template<typename Json> template<typename Json>
@ -68,7 +68,7 @@ struct jsonConstructor {
{ {
json.clear(); json.clear();
json.m_type = Json::Type::Array; json.m_type = Json::Type::Array;
json.m_value.asArray = new Array(array); json.m_value.array = new Array(array);
} }
template<typename Json, typename T> template<typename Json, typename T>
@ -76,10 +76,10 @@ struct jsonConstructor {
{ {
json.clear(); json.clear();
json.m_type = Json::Type::Array; json.m_type = Json::Type::Array;
json.m_value.asArray = new Array; json.m_value.array = new Array;
json.m_value.asArray->reserve(array.size()); json.m_value.array->reserve(array.size());
for (const T& value : array) { for (const T& value : array) {
json.m_value.asArray->emplace_back(value); json.m_value.array->emplace_back(value);
} }
} }
@ -88,7 +88,7 @@ struct jsonConstructor {
{ {
json.clear(); json.clear();
json.m_type = Json::Type::Object; json.m_type = Json::Type::Object;
json.m_value.asObject = new Object(object); json.m_value.object = new Object(object);
} }
template<typename Json, typename T> template<typename Json, typename T>
@ -96,9 +96,9 @@ struct jsonConstructor {
{ {
json.clear(); json.clear();
json.m_type = Json::Type::Object; json.m_type = Json::Type::Object;
json.m_value.asObject = new Object; json.m_value.object = new Object;
for (const auto& [name, value] : object) { for (const auto& [name, value] : object) {
json.m_value.asObject->emplace(name, value); json.m_value.object->emplace(name, value);
} }
} }
@ -107,9 +107,9 @@ struct jsonConstructor {
{ {
json.clear(); json.clear();
json.m_type = Json::Type::Object; json.m_type = Json::Type::Object;
json.m_value.asObject = new Object; json.m_value.object = new Object;
for (const auto& [name, value] : object) { for (const auto& [name, value] : object) {
json.m_value.asObject->emplace(name, value); json.m_value.object->emplace(name, value);
} }
} }
}; };

66
src/util/json/value.cpp

@ -40,14 +40,14 @@ Value::Value(const std::initializer_list<Value>& values)
if (!isObject) { if (!isObject) {
m_type = Type::Array; m_type = Type::Array;
m_value.asArray = new Array(values); m_value.array = new Array(values);
} }
else { else {
m_type = Type::Object; m_type = Type::Object;
m_value.asObject = new Object; m_value.object = new Object;
for (auto& value : values) { for (auto& value : values) {
m_value.asObject->emplace(std::move(*value[0].m_value.asString), m_value.object->emplace(std::move(*value[0].m_value.string),
std::move(value[1])); std::move(value[1]));
} }
} }
@ -91,11 +91,11 @@ void Value::emplace_back(Value value)
// Implicitly convert null to an array // Implicitly convert null to an array
if (m_type == Type::Null) { if (m_type == Type::Null) {
m_type = Type::Array; m_type = Type::Array;
m_value.asArray = new Array; m_value.array = new Array;
} }
assert(m_type == Type::Array); assert(m_type == Type::Array);
m_value.asArray->emplace_back(value); m_value.array->emplace_back(value);
} }
void Value::emplace(const std::string& key, Value value) void Value::emplace(const std::string& key, Value value)
@ -103,11 +103,11 @@ void Value::emplace(const std::string& key, Value value)
// Implicitly convert null to an object // Implicitly convert null to an object
if (m_type == Type::Null) { if (m_type == Type::Null) {
m_type = Type::Object; m_type = Type::Object;
m_value.asObject = new Object; m_value.object = new Object;
} }
assert(m_type == Type::Object); assert(m_type == Type::Object);
m_value.asObject->emplace(key, value); m_value.object->emplace(key, value);
} }
bool Value::exists(size_t index) const bool Value::exists(size_t index) const
@ -118,7 +118,7 @@ bool Value::exists(size_t index) const
bool Value::exists(const std::string& key) const bool Value::exists(const std::string& key) const
{ {
assert(m_type == Type::Object); assert(m_type == Type::Object);
return m_value.asObject->members().find(key) != m_value.asObject->members().end(); return m_value.object->members().find(key) != m_value.object->members().end();
} }
// ------------------------------------------ // ------------------------------------------
@ -128,11 +128,11 @@ Value& Value::operator[](size_t index)
// Implicitly convert null to an array // Implicitly convert null to an array
if (m_type == Type::Null) { if (m_type == Type::Null) {
m_type = Type::Array; m_type = Type::Array;
m_value.asArray = new Array; m_value.array = new Array;
} }
assert(m_type == Type::Array); assert(m_type == Type::Array);
return (*m_value.asArray)[index]; return (*m_value.array)[index];
} }
Value& Value::operator[](const std::string& key) Value& Value::operator[](const std::string& key)
@ -140,47 +140,47 @@ Value& Value::operator[](const std::string& key)
// Implicitly convert null to an object // Implicitly convert null to an object
if (m_type == Type::Null) { if (m_type == Type::Null) {
m_type = Type::Object; m_type = Type::Object;
m_value.asObject = new Object; m_value.object = new Object;
} }
assert(m_type == Type::Object); assert(m_type == Type::Object);
return (*m_value.asObject)[key]; return (*m_value.object)[key];
} }
const Value& Value::operator[](size_t index) const const Value& Value::operator[](size_t index) const
{ {
assert(m_type == Type::Array); assert(m_type == Type::Array);
return (*m_value.asArray)[index]; return (*m_value.array)[index];
} }
const Value& Value::operator[](const std::string& key) const const Value& Value::operator[](const std::string& key) const
{ {
assert(m_type == Type::Object); assert(m_type == Type::Object);
return (*m_value.asObject)[key]; return (*m_value.object)[key];
} }
Value& Value::at(size_t index) Value& Value::at(size_t index)
{ {
assert(m_type == Type::Array); assert(m_type == Type::Array);
return m_value.asArray->at(index); return m_value.array->at(index);
} }
Value& Value::at(const std::string& key) Value& Value::at(const std::string& key)
{ {
assert(m_type == Type::Object); assert(m_type == Type::Object);
return m_value.asObject->at(key); return m_value.object->at(key);
} }
const Value& Value::at(size_t index) const const Value& Value::at(size_t index) const
{ {
assert(m_type == Type::Array); assert(m_type == Type::Array);
return m_value.asArray->at(index); return m_value.array->at(index);
} }
const Value& Value::at(const std::string& key) const const Value& Value::at(const std::string& key) const
{ {
assert(m_type == Type::Object); assert(m_type == Type::Object);
return m_value.asObject->at(key); return m_value.object->at(key);
} }
// ------------------------------------------ // ------------------------------------------
@ -195,9 +195,9 @@ size_t Value::size() const
case Type::String: case Type::String:
return 1; return 1;
case Type::Array: case Type::Array:
return m_value.asArray->size(); return m_value.array->size();
case Type::Object: case Type::Object:
return m_value.asObject->size(); return m_value.object->size();
default: default:
return 1; return 1;
} }
@ -209,19 +209,19 @@ void Value::create()
{ {
switch (m_type) { switch (m_type) {
case Type::Bool: case Type::Bool:
m_value.asBool = false; m_value.boolean = false;
break; break;
case Type::Number: case Type::Number:
m_value.asDouble = 0.0; m_value.number = 0.0;
break; break;
case Type::String: case Type::String:
m_value.asString = new std::string; m_value.string = new std::string;
break; break;
case Type::Array: case Type::Array:
m_value.asArray = new Array; m_value.array = new Array;
break; break;
case Type::Object: case Type::Object:
m_value.asObject = new Object; m_value.object = new Object;
break; break;
default: default:
break; break;
@ -232,13 +232,13 @@ void Value::clear()
{ {
switch (m_type) { switch (m_type) {
case Type::String: case Type::String:
delete m_value.asString; delete m_value.string;
break; break;
case Type::Array: case Type::Array:
delete m_value.asArray; delete m_value.array;
break; break;
case Type::Object: case Type::Object:
delete m_value.asObject; delete m_value.object;
break; break;
default: default:
break; break;
@ -251,19 +251,19 @@ void Value::copyFrom(const Value& other)
switch (m_type) { switch (m_type) {
case Type::Bool: case Type::Bool:
m_value.asBool = other.m_value.asBool; m_value.boolean = other.m_value.boolean;
break; break;
case Type::Number: case Type::Number:
m_value.asDouble = other.m_value.asDouble; m_value.number = other.m_value.number;
break; break;
case Type::String: case Type::String:
m_value.asString = new std::string(*other.m_value.asString); m_value.string = new std::string(*other.m_value.string);
break; break;
case Type::Array: case Type::Array:
m_value.asArray = new Array(*other.m_value.asArray); m_value.array = new Array(*other.m_value.array);
break; break;
case Type::Object: case Type::Object:
m_value.asObject = new Object(*other.m_value.asObject); m_value.object = new Object(*other.m_value.object);
break; break;
default: default:
break; break;

20
src/util/json/value.h

@ -102,11 +102,11 @@ public:
Type type() const { return m_type; } Type type() const { return m_type; }
size_t size() const; size_t size() const;
bool asBool() const { return m_value.asBool; } bool asBool() const { return m_value.boolean; }
double asDouble() const { return m_value.asDouble; } double asDouble() const { return m_value.number; }
const std::string& asString() const { return *m_value.asString; } const std::string& asString() const { return *m_value.string; }
const Array& asArray() const { return *m_value.asArray; } const Array& asArray() const { return *m_value.array; }
const Object& asObject() const { return *m_value.asObject; } const Object& asObject() const { return *m_value.object; }
private: private:
void create(); void create();
@ -116,11 +116,11 @@ private:
Type m_type { Type::Null }; Type m_type { Type::Null };
union { union {
bool asBool; bool boolean;
double asDouble; double number;
std::string* asString; std::string* string;
Array* asArray; Array* array;
Object* asObject; Object* object;
} m_value {}; } m_value {};
}; };

Loading…
Cancel
Save