载入中...
搜索中...
未找到
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 <string_view>
26#include <unordered_map>
27#include <vector>
28
29namespace eve::json {
30
31struct Node;
32class Document;
33
40public:
42 Value() = default;
43
46 explicit operator bool() const { return node_ != nullptr; }
47
49 bool isNull() const;
51 bool isBool() const;
53 bool isNumber() const;
55 bool isInt64() const;
57 bool isIntegerLiteral() const;
59 bool isString() const;
61 bool isObject() const;
63 bool isArray() const;
64
65 // --- object access -----------------------------------------------------
67 bool has(const char* key) const;
70 Value get(const char* key) const;
73 std::vector<std::string> keys() const;
74
75 // --- array access ------------------------------------------------------
78 size_t size() const;
81 Value at(size_t index) const;
82
83 // --- scalars -----------------------------------------------------------
84 // Conversions are lenient (a numeric string reads as a number and back)
85 // and fall back rather than throw.
87 bool asBool(bool fallback = false) const;
89 int asInt(int fallback = 0) const;
91 float asFloat(float fallback = 0.f) const;
93 double asDouble(double fallback = 0.0) const;
95 std::int64_t asInt64(std::int64_t fallback = 0) const;
97 std::string asString(const std::string& fallback = {}) const;
98
99 // --- keyed shorthand ---------------------------------------------------
100 // get(key).asX(fallback), which is the dominant shape in config loaders.
102 bool getBool(const char* key, bool fallback = false) const;
104 int getInt(const char* key, int fallback = 0) const;
106 float getFloat(const char* key, float fallback = 0.f) const;
108 double getDouble(const char* key, double fallback = 0.0) const;
110 std::string getString(const char* key, const std::string& fallback = {}) const;
111
112 // --- collections -------------------------------------------------------
113 // Empty when the key is absent or the value has the wrong shape.
115 std::vector<std::string> getStringArray(const char* key) const;
117 std::vector<int> getIntArray(const char* key) const;
119 std::vector<float> getFloatArray(const char* key) const;
120 std::unordered_map<std::string, std::string> getStringMap(const char* key) const;
121 std::unordered_map<std::string, int> getIntMap(const char* key) const;
122
125 std::vector<std::string> toStringArray() const;
126
127private:
128 friend class Document;
129 explicit Value(const Node* node) : node_(node) {}
130 const Node* node_ = nullptr;
131};
132
138public:
144 Document(Document&&) noexcept;
146 Document& operator=(Document&&) noexcept;
147 Document(const Document&) = delete;
148 Document& operator=(const Document&) = delete;
149
155 static Document parse(const std::string& text, std::string* error = nullptr);
156
158 bool valid() const { return root_ != nullptr; }
160 Value root() const { return Value(root_.get()); }
161
162private:
163 std::unique_ptr<Node> root_;
164};
165
166} // namespace eve::json
167
168namespace eve {
169
170class Value;
171
172namespace json {
173
175enum class StringEscapeStyle { Canonical, UnicodeBackspaceFormFeed };
176
185[[nodiscard]] EVENGINE_API_FOUNDATION std::string quoteString(std::string_view value, StringEscapeStyle style);
186
188enum class UnsignedIntegerSyntax { Decimal, LegacyStoull };
189
191struct UnsignedIntegerScan {
193 std::uint64_t value = 0;
195 std::size_t consumed = 0;
197 std::size_t size = 0;
198};
199
209[[nodiscard]] EVENGINE_API_FOUNDATION Result<UnsignedIntegerScan> scanUnsignedInteger(std::string_view text,
210 UnsignedIntegerSyntax syntax);
211
219[[nodiscard]] EVENGINE_API_FOUNDATION Result<std::string> stringify(const eve::Value& value);
220
221} // namespace json
222} // namespace eve
SQObjectPtr key
Definition ECS.cpp:155
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
bool valid
std::string error
Definition Package.cpp:174
const eve::script::ScriptFunctionSyntax * syntax
Object::Ptr value
Move-only, checked operation results for the common layer.
const RoadNode * node
std::string text
float size
Definition TreeMesh.cpp:158
std::size_t at
uint32_t index
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
EVENGINE_API_FOUNDATION public API.
Definition Json.h:137
Document(Document &&) noexcept
Document.
~Document()
Document.
Value root() const
Root.
Definition Json.h:160
EVENGINE_API_FOUNDATION public API.
Definition Json.h:39
Value()=default
Value.
std::string quoteString(std::string_view value, StringEscapeStyle style)
Encode an owning JSON string using the canonical serializer's escaping implementation.
Definition Json.cpp:576
Result< std::string > stringify(const eve::Value &value)
Serialize the canonical owning Value as deterministic compact JSON.
Definition Json.cpp:605
Result< UnsignedIntegerScan > scanUnsignedInteger(std::string_view text, UnsignedIntegerSyntax syntax)
Scan an unsigned integer prefix without mutating caller state.
Definition Json.cpp:583
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
bool consumed
Definition Graphics.cpp:201