GroveEngine/include/grove/JsonDataValue.h
StillHammer fad105afb2 feat: Implement complete IDataNode/IDataTree system with JSON backend
Major feature: Unified config/data/runtime tree system

**New System Architecture:**
- Unified data tree for config, persistent data, and runtime state
- Three separate roots: config/ (read-only + hot-reload), data/ (read-write + save), runtime/ (temporary)
- Support for modding, saves, and hot-reload in single system

**Interfaces:**
- IDataValue: Abstract data value interface (type-safe access)
- IDataNode: Tree node with navigation, search, and modification
- IDataTree: Root container with config/data/runtime management

**Concrete Implementations:**
- JsonDataValue: nlohmann::json backed value
- JsonDataNode: Full tree navigation with pattern matching & queries
- JsonDataTree: File-based JSON storage with hot-reload

**Features:**
- Pattern matching search (wildcards support)
- Property-based queries with predicates
- SHA256 hashing for validation/sync
- Hot-reload for config/ directory
- Save operations for data/ persistence
- Read-only enforcement for config/

**API Changes:**
- All namespaces changed from 'warfactory' to 'grove'
- IDataTree: Added getConfigRoot(), getDataRoot(), getRuntimeRoot()
- IDataTree: Added saveData(), saveNode() for persistence
- IDataNode: Added setChild(), removeChild(), clearChildren()
- CMakeLists.txt: Added OpenSSL dependency for hashing

**Usage:**
```cpp
auto tree = DataTreeFactory::create("json", "./gamedata");
auto config = tree->getConfigRoot();     // Read-only game config
auto data = tree->getDataRoot();         // Player saves
auto runtime = tree->getRuntimeRoot();   // Temporary state

// Hot-reload config on file changes
if (tree->reloadIfChanged()) { /* refresh modules */ }

// Save player progress
data->setChild("progress", progressNode);
tree->saveData();
```

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-28 15:36:25 +08:00

54 lines
1.5 KiB
C++

#pragma once
#include "IDataValue.h"
#include <nlohmann/json.hpp>
#include <memory>
#include <string>
#include <vector>
namespace grove {
using json = nlohmann::json;
/**
* @brief Concrete implementation of IDataValue backed by nlohmann::json
*/
class JsonDataValue : public IDataValue {
public:
explicit JsonDataValue(const json& value);
explicit JsonDataValue(json&& value);
virtual ~JsonDataValue() = default;
// Type checking
bool isNull() const override;
bool isBool() const override;
bool isNumber() const override;
bool isString() const override;
bool isArray() const override;
bool isObject() const override;
// Value access with defaults
bool asBool(bool defaultValue = false) const override;
int asInt(int defaultValue = 0) const override;
double asDouble(double defaultValue = 0.0) const override;
std::string asString(const std::string& defaultValue = "") const override;
// Array/Object access
size_t size() const override;
std::unique_ptr<IDataValue> get(size_t index) const override;
std::unique_ptr<IDataValue> get(const std::string& key) const override;
bool has(const std::string& key) const override;
// Serialization
std::string toString() const override;
// Direct JSON access (for internal use)
const json& getJson() const { return m_value; }
json& getJson() { return m_value; }
private:
json m_value;
};
} // namespace grove