- Add abstract JsonSerializable base class with toJson/fromJson methods - Create comprehensive response classes for complete JSON document handling: * ClusterMembersResponse for cluster member data * TaskStatusResponse and TaskControlResponse for task operations * NodeStatusResponse, NodeEndpointsResponse, NodeOperationResponse for node data - Implement concrete serializable classes for all data types: * NodeInfoSerializable, TaskInfoSerializable, SystemInfoSerializable * TaskSummarySerializable, EndpointInfoSerializable - Refactor all service classes to use new serialization system - Reduce service method complexity from 20-30 lines to 2-3 lines - Eliminate manual JsonDocument creation and field mapping - Ensure type safety and compile-time validation - Maintain backward compatibility while improving maintainability Breaking change: Service classes now use response objects instead of manual JSON creation
57 lines
1.7 KiB
C++
57 lines
1.7 KiB
C++
#pragma once
|
|
#include <ArduinoJson.h>
|
|
|
|
namespace spore {
|
|
namespace util {
|
|
|
|
/**
|
|
* Abstract base class for objects that can be serialized to/from JSON
|
|
* Provides a clean interface for converting objects to JsonObject and back
|
|
*/
|
|
class JsonSerializable {
|
|
public:
|
|
virtual ~JsonSerializable() = default;
|
|
|
|
/**
|
|
* Serialize this object to a JsonObject
|
|
* @param obj The JsonObject to populate with this object's data
|
|
*/
|
|
virtual void toJson(JsonObject& obj) const = 0;
|
|
|
|
/**
|
|
* Deserialize this object from a JsonObject
|
|
* @param obj The JsonObject containing the data to populate this object
|
|
*/
|
|
virtual void fromJson(const JsonObject& obj) = 0;
|
|
|
|
/**
|
|
* Convenience method to create a JsonObject from this object
|
|
* @param doc The JsonDocument to create the object in
|
|
* @return A JsonObject containing this object's serialized data
|
|
*/
|
|
JsonObject toJsonObject(JsonDocument& doc) const {
|
|
JsonObject obj = doc.to<JsonObject>();
|
|
toJson(obj);
|
|
return obj;
|
|
}
|
|
|
|
/**
|
|
* Convenience method to create a JsonArray from a collection of serializable objects
|
|
* @param doc The JsonDocument to create the array in
|
|
* @param objects Collection of objects implementing JsonSerializable
|
|
* @return A JsonArray containing all serialized objects
|
|
*/
|
|
template<typename Container>
|
|
static JsonArray toJsonArray(JsonDocument& doc, const Container& objects) {
|
|
JsonArray arr = doc.to<JsonArray>();
|
|
for (const auto& obj : objects) {
|
|
JsonObject item = arr.add<JsonObject>();
|
|
obj.toJson(item);
|
|
}
|
|
return arr;
|
|
}
|
|
};
|
|
|
|
} // namespace util
|
|
} // namespace spore
|