Browse Source

Util: Add JSON Array/Object/Value types

master
Riyyi 3 years ago
parent
commit
0880d98fe0
  1. 39
      src/util/json/array.h
  2. 41
      src/util/json/object.h
  3. 107
      src/util/json/value.cpp
  4. 66
      src/util/json/value.h

39
src/util/json/array.h

@ -0,0 +1,39 @@
/*
* Copyright (C) 2022 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#ifndef JSON_ARRAY_H
#define JSON_ARRAY_H
#include <utility> // move
#include <vector>
#include "util/json/parser.h"
namespace Json {
class Array {
public:
Array() {}
virtual ~Array() {}
Array(const Array& other)
: m_values(other.m_values)
{
}
void emplace(Value value)
{
m_values.emplace_back(std::move(value));
}
private:
std::vector<Value> m_values;
};
} // namespace Json
#endif // JSON_ARRAY_H

41
src/util/json/object.h

@ -0,0 +1,41 @@
/*
* Copyright (C) 2022 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#ifndef JSON_OBJECT_H
#define JSON_OBJECT_H
#include <map>
#include <string>
#include <utility> // move
#include "util/json/parser.h"
namespace Json {
class Object {
public:
Object() {}
virtual ~Object() {}
Object(const Object& other)
: m_members(other.m_members)
{
}
void emplace(const std::string& key, Value value)
{
m_members.emplace(key, std::move(value));
}
const std::map<std::string, Value>& members() const { return m_members; }
private:
std::map<std::string, Value> m_members;
};
} // namespace Json
#endif // JSON_OBJECT_H

107
src/util/json/value.cpp

@ -0,0 +1,107 @@
/*
* Copyright (C) 2022 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#include <string>
#include <utility> // move
#include "util/json/array.h"
#include "util/json/object.h"
#include "util/json/value.h"
namespace Json {
Value::Value(const Value& other)
{
copyFrom(other);
}
Value& Value::operator=(const Value& other)
{
if (this != &other) {
clear();
copyFrom(other);
}
return *this;
}
Value::Value(bool value)
: m_type(Type::Bool)
{
m_value.asBool = value;
}
Value::Value(double value)
: m_type(Type::Number)
{
m_value.asDouble = value;
}
Value::Value(const char* value)
: m_type(Type::String)
{
m_value.asString = new std::string(value);
}
Value::Value(const std::string& value)
: m_type(Type::String)
{
m_value.asString = new std::string(value);
}
Value::Value(const Array& value)
: m_type(Type::Array)
{
m_value.asArray = new Array(value);
}
Value::Value(const Object& value)
: m_type(Type::Object)
{
m_value.asObject = new Object(value);
}
// ------------------------------------------
void Value::clear()
{
if (m_type == Type::String) {
delete m_value.asString;
}
if (m_type == Type::Array) {
delete m_value.asArray;
}
if (m_type == Type::Object) {
delete m_value.asObject;
}
}
void Value::copyFrom(const Value& other)
{
m_type = other.m_type;
switch (m_type) {
case Type::Bool:
m_value.asBool = other.m_value.asBool;
break;
case Type::Number:
m_value.asDouble = other.m_value.asDouble;
break;
case Type::String:
m_value.asString = new std::string(*other.m_value.asString);
break;
case Type::Array:
m_value.asArray = new Array(*other.m_value.asArray);
break;
case Type::Object:
m_value.asObject = new Object(*other.m_value.asObject);
break;
default:
break;
}
}
} // namespace Json

66
src/util/json/value.h

@ -0,0 +1,66 @@
/*
* Copyright (C) 2022 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#ifndef JSON_VALUE_H
#define JSON_VALUE_H
#include <string>
namespace Json {
class Array;
class Object;
class Value {
public:
enum class Type {
Null, // null (case sensitive!)
Bool, // true/false (case sensitive!)
Number, // 123
String, // ""
Array, // []
Object, // {}
};
Value() {}
virtual ~Value() { clear(); }
// Copy constructor
Value(const Value& other);
// Assignment operator
Value& operator=(const Value& other);
Value(bool value);
Value(double value);
Value(const char* value);
Value(const std::string& value);
Value(const Array& value);
Value(const Object& value);
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; }
private:
void clear();
void copyFrom(const Value& other);
Type m_type { Type::Null };
union {
bool asBool;
double asDouble;
std::string* asString;
Array* asArray;
Object* asObject;
} m_value;
};
} // namespace Json
#endif // JSON_VALUE_H
Loading…
Cancel
Save