载入中...
搜索中...
未找到
Json.h
浏览该文件的文档.
1#pragma once
2
3// Read-only JSON facade and canonical Value serializer shared by every module
4// that loads data-driven config.
5//
6// The backend is a self-contained recursive-descent parser (Json.cpp) rather
7// than Poco, so EVCommon stays dependency-free and modules that only read JSON
8// (rpg / inventory / building / card / voxel / housegen / i18n) no longer pull
9// Poco in just to parse a config file. Writing JSON still goes through
10// `stringify` is the single canonical serializer for common::Value. The
11// legacy data::JsonDocument/Poco facade remains available for mutable legacy
12// integrations, but must not be used as a second Value serialization format.
13//
14// Accessors never throw: a missing key, a wrong type or an out-of-range index
15// yields the supplied fallback (or a null Value), which is what config loading
16// wants and what the per-module helpers this replaces already did.
17
18#include "common/Export.h"
19#include "common/Result.h"
20
21#include <cstddef>
22#include <cstdint>
23#include <memory>
24#include <string>
25#include <unordered_map>
26#include <vector>
27
28namespace eve::json {
29
30struct Node;
31class Document;
32
39public:
41 Value() = default;
42
45 explicit operator bool() const { return node_ != nullptr; }
46
48 bool isNull() const;
50 bool isBool() const;
52 bool isNumber() const;
54 bool isInt64() const;
56 bool isIntegerLiteral() const;
58 bool isString() const;
60 bool isObject() const;
62 bool isArray() const;
63
64 // --- object access -----------------------------------------------------
66 bool has(const char* key) const;
69 Value get(const char* key) const;
72 std::vector<std::string> keys() const;
73
74 // --- array access ------------------------------------------------------
77 size_t size() const;
80 Value at(size_t index) const;
81
82 // --- scalars -----------------------------------------------------------
83 // Conversions are lenient (a numeric string reads as a number and back)
84 // and fall back rather than throw.
86 bool asBool(bool fallback = false) const;
88 int asInt(int fallback = 0) const;
90 float asFloat(float fallback = 0.f) const;
92 double asDouble(double fallback = 0.0) const;
94 std::int64_t asInt64(std::int64_t fallback = 0) const;
96 std::string asString(const std::string& fallback = {}) const;
97
98 // --- keyed shorthand ---------------------------------------------------
99 // get(key).asX(fallback), which is the dominant shape in config loaders.
101 bool getBool(const char* key, bool fallback = false) const;
103 int getInt(const char* key, int fallback = 0) const;
105 float getFloat(const char* key, float fallback = 0.f) const;
107 double getDouble(const char* key, double fallback = 0.0) const;
109 std::string getString(const char* key, const std::string& fallback = {}) const;
110
111 // --- collections -------------------------------------------------------
112 // Empty when the key is absent or the value has the wrong shape.
114 std::vector<std::string> getStringArray(const char* key) const;
116 std::vector<int> getIntArray(const char* key) const;
118 std::vector<float> getFloatArray(const char* key) const;
119 std::unordered_map<std::string, std::string> getStringMap(const char* key) const;
120 std::unordered_map<std::string, int> getIntMap(const char* key) const;
121
124 std::vector<std::string> toStringArray() const;
125
126private:
127 friend class Document;
128 explicit Value(const Node* node) : node_(node) {}
129 const Node* node_ = nullptr;
130};
131
137public:
143 Document(Document&&) noexcept;
145 Document& operator=(Document&&) noexcept;
146 Document(const Document&) = delete;
147 Document& operator=(const Document&) = delete;
148
154 static Document parse(const std::string& text, std::string* error = nullptr);
155
157 bool valid() const { return root_ != nullptr; }
159 Value root() const { return Value(root_.get()); }
160
161private:
162 std::unique_ptr<Node> root_;
163};
164
165} // namespace eve::json
166
167namespace eve {
168
169class Value;
170
171namespace json {
172
180[[nodiscard]] EVENGINE_API_FOUNDATION Result<std::string> stringify(const eve::Value& value);
181
182} // namespace json
183} // namespace eve
double value
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
std::uint32_t key
std::string text
bool valid
std::string error
Definition Package.cpp:60
Move-only, checked operation results for the common layer.
const RoadNode * node
float size
Definition TreeMesh.cpp:156
uint32_t index
std::size_t at
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
EVENGINE_API_FOUNDATION public API.
Definition Json.h:136
Document(Document &&) noexcept
Document.
~Document()
Document.
Value root() const
Root.
Definition Json.h:159
EVENGINE_API_FOUNDATION public API.
Definition Json.h:38
Value()=default
Value.
Result< std::string > stringify(const eve::Value &value)
Serialize the canonical owning Value as deterministic compact JSON.
Definition Json.cpp:563
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16