载入中...
搜索中...
未找到
Json.h
浏览该文件的文档.
1#pragma once
2
3// Read-only JSON facade shared by every module that loads data-driven config.
4//
5// The backend is a self-contained recursive-descent parser (Json.cpp) rather
6// than Poco, so EVCommon stays dependency-free and modules that only read JSON
7// (rpg / inventory / building / card / voxel / housegen / i18n) no longer pull
8// Poco in just to parse a config file. Writing JSON still goes through
9// data::JsonDocument / Poco.
10//
11// Accessors never throw: a missing key, a wrong type or an out-of-range index
12// yields the supplied fallback (or a null Value), which is what config loading
13// wants and what the per-module helpers this replaces already did.
14
15#include "common/Export.h"
16
17#include <cstddef>
18#include <memory>
19#include <string>
20#include <unordered_map>
21#include <vector>
22
23namespace eve::json {
24
25struct Node;
26class Document;
27
33public:
34 Value() = default;
35
37 explicit operator bool() const { return node_ != nullptr; }
38
39 bool isNull() const;
40 bool isBool() const;
41 bool isNumber() const;
42 bool isString() const;
43 bool isObject() const;
44 bool isArray() const;
45
46 // --- object access -----------------------------------------------------
47 bool has(const char* key) const;
49 Value get(const char* key) const;
51 std::vector<std::string> keys() const;
52
53 // --- array access ------------------------------------------------------
55 size_t size() const;
57 Value at(size_t index) const;
58
59 // --- scalars -----------------------------------------------------------
60 // Conversions are lenient (a numeric string reads as a number and back)
61 // and fall back rather than throw.
62 bool asBool(bool fallback = false) const;
63 int asInt(int fallback = 0) const;
64 float asFloat(float fallback = 0.f) const;
65 double asDouble(double fallback = 0.0) const;
66 std::string asString(const std::string& fallback = {}) const;
67
68 // --- keyed shorthand ---------------------------------------------------
69 // get(key).asX(fallback), which is the dominant shape in config loaders.
70 bool getBool(const char* key, bool fallback = false) const;
71 int getInt(const char* key, int fallback = 0) const;
72 float getFloat(const char* key, float fallback = 0.f) const;
73 double getDouble(const char* key, double fallback = 0.0) const;
74 std::string getString(const char* key, const std::string& fallback = {}) const;
75
76 // --- collections -------------------------------------------------------
77 // Empty when the key is absent or the value has the wrong shape.
78 std::vector<std::string> getStringArray(const char* key) const;
79 std::vector<int> getIntArray(const char* key) const;
80 std::vector<float> getFloatArray(const char* key) const;
81 std::unordered_map<std::string, std::string> getStringMap(const char* key) const;
82 std::unordered_map<std::string, int> getIntMap(const char* key) const;
83
85 std::vector<std::string> toStringArray() const;
86
87private:
88 friend class Document;
89 explicit Value(const Node* node) : node_(node) {}
90 const Node* node_ = nullptr;
91};
92
97public:
100 Document(Document&&) noexcept;
101 Document& operator=(Document&&) noexcept;
102 Document(const Document&) = delete;
103 Document& operator=(const Document&) = delete;
104
109 static Document parse(const std::string& text, std::string* error = nullptr);
110
111 bool valid() const { return root_ != nullptr; }
112 Value root() const { return Value(root_.get()); }
113
114private:
115 std::unique_ptr<Node> root_;
116};
117
118} // namespace eve::json
#define EVENGINE_API
宿主(eve / libmain)导出宏;插件从进程导入同一批符号。
Definition Export.h:16
std::string error
bool valid
Document(Document &&) noexcept
Value root() const
Definition Json.h:112