mirror of
https://codeberg.org/vyn/rei-json.git
synced 2025-07-03 10:13:20 +00:00
first commit
This commit is contained in:
commit
eb5191a722
15 changed files with 1075 additions and 0 deletions
42
include/rei-json/Array.h
Normal file
42
include/rei-json/Array.h
Normal file
|
@ -0,0 +1,42 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace rei::json {
|
||||
class JsonObject;
|
||||
class FieldValue;
|
||||
class JsonArray {
|
||||
|
||||
public:
|
||||
|
||||
JsonArray();
|
||||
|
||||
JsonArray& push(int value);
|
||||
JsonArray& push(bool value);
|
||||
JsonArray& push(const std::string& value);
|
||||
JsonArray& push(const std::string&& value);
|
||||
JsonArray& push(const char * value);
|
||||
JsonArray& pushObject(const JsonObject& object);
|
||||
JsonArray& pushObject(const JsonObject&& object);
|
||||
JsonArray& pushArray(const JsonArray& array);
|
||||
JsonArray& pushArray(const JsonArray&& array);
|
||||
JsonArray& pushNull();
|
||||
|
||||
int getNumber(int index);
|
||||
bool getBool(int index);
|
||||
std::string getString(int index);
|
||||
JsonObject& getObject(int index);
|
||||
JsonArray& getArray(int index);
|
||||
bool hasNull(int index);
|
||||
|
||||
void forEach(std::function<void(FieldValue&, int)>);
|
||||
size_t count() const;
|
||||
|
||||
private:
|
||||
std::vector<FieldValue> elements;
|
||||
};
|
||||
|
||||
}
|
70
include/rei-json/Field.h
Normal file
70
include/rei-json/Field.h
Normal file
|
@ -0,0 +1,70 @@
|
|||
#pragma once
|
||||
|
||||
#include "Object.h"
|
||||
#include "Array.h"
|
||||
#include <string>
|
||||
|
||||
namespace rei::json {
|
||||
|
||||
class JsonObject;
|
||||
class JsonArray;
|
||||
|
||||
enum FieldType {
|
||||
Null,
|
||||
Number,
|
||||
Bool,
|
||||
String,
|
||||
Object,
|
||||
Array
|
||||
};
|
||||
|
||||
class FieldValue {
|
||||
|
||||
public:
|
||||
|
||||
FieldValue(const FieldValue& other) noexcept;
|
||||
FieldValue& operator=(const FieldValue& other) noexcept;
|
||||
FieldValue& operator=(FieldValue&& other) noexcept;
|
||||
FieldValue(FieldValue&& other) noexcept;
|
||||
|
||||
FieldValue(FieldType type);
|
||||
FieldValue(int value);
|
||||
FieldValue(bool value);
|
||||
FieldValue(const std::string& value);
|
||||
FieldValue(std::string&& value);
|
||||
FieldValue(const JsonObject& value);
|
||||
FieldValue(JsonObject&& value);
|
||||
FieldValue(const JsonArray& value);
|
||||
FieldValue(JsonArray&& value) noexcept;
|
||||
|
||||
~FieldValue();
|
||||
|
||||
inline int asNumber() { return number; }
|
||||
inline bool asBool() { return boolean; }
|
||||
inline std::string& asString() { return string; }
|
||||
inline JsonObject& asObject() { return object; }
|
||||
inline JsonArray& asArray() { return array; }
|
||||
|
||||
inline bool isNumber() const { return type == FieldType::Number; }
|
||||
inline bool isBool() const { return type == FieldType::Bool; }
|
||||
inline bool isString() const { return type == FieldType::String; }
|
||||
inline bool isObject() const { return type == FieldType::Object; }
|
||||
inline bool isArray() const { return type == FieldType::Array; }
|
||||
inline bool isNull() const { return type == FieldType::Null; }
|
||||
|
||||
private:
|
||||
|
||||
void _copyValues(const FieldValue& other);
|
||||
void _moveValues(FieldValue&& other);
|
||||
void _destroy();
|
||||
|
||||
FieldType type;
|
||||
union {
|
||||
int number;
|
||||
bool boolean;
|
||||
std::string string;
|
||||
JsonObject object;
|
||||
JsonArray array;
|
||||
};
|
||||
};
|
||||
}
|
44
include/rei-json/Object.h
Normal file
44
include/rei-json/Object.h
Normal file
|
@ -0,0 +1,44 @@
|
|||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
namespace rei::json {
|
||||
|
||||
class JsonArray;
|
||||
class FieldValue;
|
||||
|
||||
class JsonObject {
|
||||
public:
|
||||
|
||||
JsonObject();
|
||||
|
||||
bool contains(const std::string& key) const;
|
||||
|
||||
JsonObject& set(const std::string& key, int value);
|
||||
JsonObject& set(const std::string& key, bool value);
|
||||
JsonObject& set(const std::string& key, const std::string& value);
|
||||
JsonObject& set(const std::string& key, const std::string&& value);
|
||||
JsonObject& set(const std::string& key, const char * value);
|
||||
JsonObject& setNull(const std::string& key);
|
||||
JsonObject& addArray(const std::string& key, const JsonArray& array);
|
||||
JsonObject& addArray(const std::string& key, JsonArray&& array);
|
||||
JsonObject& addObject(const std::string& key, const JsonObject& object);
|
||||
JsonObject& addObject(const std::string& key, JsonObject&& object);
|
||||
|
||||
int getNumber(const std::string& key);
|
||||
bool getBool(const std::string& key);
|
||||
std::string getString(const std::string& key);
|
||||
JsonObject& getObject(const std::string& key);
|
||||
JsonArray& getArray(const std::string& key);
|
||||
bool hasNull(const std::string& key);
|
||||
|
||||
void forEach(std::function<void(FieldValue&, const std::string& key)>);
|
||||
size_t count() const;
|
||||
|
||||
private:
|
||||
|
||||
std::map<std::string, FieldValue> elements;
|
||||
};
|
||||
}
|
14
include/rei-json/json.h
Normal file
14
include/rei-json/json.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
#pragma once
|
||||
#include "Object.h"
|
||||
#include "Array.h"
|
||||
#include "Field.h"
|
||||
#include <string>
|
||||
#include <variant>
|
||||
namespace rei::json {
|
||||
class std::variant<JsonObject, JsonArray> parse(const std::string& jsonStr);
|
||||
|
||||
std::string toString(std::variant<JsonObject, JsonArray>& element);
|
||||
std::string toString(JsonObject& object);
|
||||
std::string toString(JsonArray& object);
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue