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) {
// 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 ']'");
break;
}
@ -396,7 +396,7 @@ Value Parser::getObject()
token = consume();
if (token.type == Token::Type::BraceClose) {
// 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 '}'");
}
// Empty object
@ -416,7 +416,7 @@ Value Parser::getObject()
}
// Check if name exists in hashmap
name = *tmpName.m_value.asString;
name = *tmpName.m_value.string;
if (unique.find(name) != unique.end()) {
reportError(token, "duplicate name '" + token.symbol + "', names should be unique");
break;

10
src/util/json/serializer.cpp

@ -43,16 +43,16 @@ std::string Serializer::dumpHelper(const Value& value, const uint32_t indentLeve
return "null";
break;
case Value::Type::Bool:
return value.m_value.asBool ? "true" : "false";
return value.m_value.boolean ? "true" : "false";
break;
case Value::Type::Number: {
std::ostringstream os;
os << value.m_value.asDouble;
os << value.m_value.number;
return os.str();
break;
}
case Value::Type::String:
return "\"" + *value.m_value.asString + "\"";
return "\"" + *value.m_value.string + "\"";
break;
case Value::Type::Array:
return dumpArray(value, indentLevel);
@ -77,7 +77,7 @@ std::string Serializer::dumpArray(const Value& value, const uint32_t indentLevel
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) {
result += std::string(m_indent * (indentLevel + 1), m_indentCharacter);
result += dumpHelper(*it, indentLevel + 1);
@ -110,7 +110,7 @@ std::string Serializer::dumpObject(const Value& value, const uint32_t indentLeve
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) {
result += std::string(m_indent * (indentLevel + 1), m_indentCharacter);
result += "\"" + it->first + "\":";

28
src/util/json/tojson.h

@ -28,7 +28,7 @@ struct jsonConstructor {
{
json.clear();
json.m_type = Json::Type::Bool;
json.m_value.asBool = boolean;
json.m_value.boolean = boolean;
}
template<typename Json>
@ -36,7 +36,7 @@ struct jsonConstructor {
{
json.clear();
json.m_type = Json::Type::Number;
json.m_value.asDouble = (double)number;
json.m_value.number = (double)number;
}
template<typename Json>
@ -44,7 +44,7 @@ struct jsonConstructor {
{
json.clear();
json.m_type = Json::Type::Number;
json.m_value.asDouble = number;
json.m_value.number = number;
}
template<typename Json>
@ -52,7 +52,7 @@ struct jsonConstructor {
{
json.clear();
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>
@ -60,7 +60,7 @@ struct jsonConstructor {
{
json.clear();
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>
@ -68,7 +68,7 @@ struct jsonConstructor {
{
json.clear();
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>
@ -76,10 +76,10 @@ struct jsonConstructor {
{
json.clear();
json.m_type = Json::Type::Array;
json.m_value.asArray = new Array;
json.m_value.asArray->reserve(array.size());
json.m_value.array = new Array;
json.m_value.array->reserve(array.size());
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.m_type = Json::Type::Object;
json.m_value.asObject = new Object(object);
json.m_value.object = new Object(object);
}
template<typename Json, typename T>
@ -96,9 +96,9 @@ struct jsonConstructor {
{
json.clear();
json.m_type = Json::Type::Object;
json.m_value.asObject = new Object;
json.m_value.object = new 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.m_type = Json::Type::Object;
json.m_value.asObject = new Object;
json.m_value.object = new 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) {
m_type = Type::Array;
m_value.asArray = new Array(values);
m_value.array = new Array(values);
}
else {
m_type = Type::Object;
m_value.asObject = new Object;
m_value.object = new Object;
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]));
}
}
@ -91,11 +91,11 @@ void Value::emplace_back(Value value)
// Implicitly convert null to an array
if (m_type == Type::Null) {
m_type = Type::Array;
m_value.asArray = new Array;
m_value.array = new 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)
@ -103,11 +103,11 @@ void Value::emplace(const std::string& key, Value value)
// Implicitly convert null to an object
if (m_type == Type::Null) {
m_type = Type::Object;
m_value.asObject = new Object;
m_value.object = new 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
@ -118,7 +118,7 @@ bool Value::exists(size_t index) const
bool Value::exists(const std::string& key) const
{
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
if (m_type == Type::Null) {
m_type = Type::Array;
m_value.asArray = new Array;
m_value.array = new Array;
}
assert(m_type == Type::Array);
return (*m_value.asArray)[index];
return (*m_value.array)[index];
}
Value& Value::operator[](const std::string& key)
@ -140,47 +140,47 @@ Value& Value::operator[](const std::string& key)
// Implicitly convert null to an object
if (m_type == Type::Null) {
m_type = Type::Object;
m_value.asObject = new Object;
m_value.object = new Object;
}
assert(m_type == Type::Object);
return (*m_value.asObject)[key];
return (*m_value.object)[key];
}
const Value& Value::operator[](size_t index) const
{
assert(m_type == Type::Array);
return (*m_value.asArray)[index];
return (*m_value.array)[index];
}
const Value& Value::operator[](const std::string& key) const
{
assert(m_type == Type::Object);
return (*m_value.asObject)[key];
return (*m_value.object)[key];
}
Value& Value::at(size_t index)
{
assert(m_type == Type::Array);
return m_value.asArray->at(index);
return m_value.array->at(index);
}
Value& Value::at(const std::string& key)
{
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
{
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
{
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:
return 1;
case Type::Array:
return m_value.asArray->size();
return m_value.array->size();
case Type::Object:
return m_value.asObject->size();
return m_value.object->size();
default:
return 1;
}
@ -209,19 +209,19 @@ void Value::create()
{
switch (m_type) {
case Type::Bool:
m_value.asBool = false;
m_value.boolean = false;
break;
case Type::Number:
m_value.asDouble = 0.0;
m_value.number = 0.0;
break;
case Type::String:
m_value.asString = new std::string;
m_value.string = new std::string;
break;
case Type::Array:
m_value.asArray = new Array;
m_value.array = new Array;
break;
case Type::Object:
m_value.asObject = new Object;
m_value.object = new Object;
break;
default:
break;
@ -232,13 +232,13 @@ void Value::clear()
{
switch (m_type) {
case Type::String:
delete m_value.asString;
delete m_value.string;
break;
case Type::Array:
delete m_value.asArray;
delete m_value.array;
break;
case Type::Object:
delete m_value.asObject;
delete m_value.object;
break;
default:
break;
@ -251,19 +251,19 @@ void Value::copyFrom(const Value& other)
switch (m_type) {
case Type::Bool:
m_value.asBool = other.m_value.asBool;
m_value.boolean = other.m_value.boolean;
break;
case Type::Number:
m_value.asDouble = other.m_value.asDouble;
m_value.number = other.m_value.number;
break;
case Type::String:
m_value.asString = new std::string(*other.m_value.asString);
m_value.string = new std::string(*other.m_value.string);
break;
case Type::Array:
m_value.asArray = new Array(*other.m_value.asArray);
m_value.array = new Array(*other.m_value.array);
break;
case Type::Object:
m_value.asObject = new Object(*other.m_value.asObject);
m_value.object = new Object(*other.m_value.object);
break;
default:
break;

20
src/util/json/value.h

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

Loading…
Cancel
Save