optimize ArduinoJson serialization: avoid copies of strings

This commit is contained in:
Nicu Hodos 2024-10-18 12:18:19 +02:00
parent baa3d2d0b8
commit fe8b33df72

View File

@ -88,7 +88,7 @@ namespace Ha {
const char* entityCategory = nullptr;
const char* deviceClass = nullptr;
const char* name = nullptr;
char* id = nullptr;
const char* id = nullptr;
const char* type = nullptr;
char configTopic[TOPIC_SIZE];
JsonPairs<bool> jsonBooleans;
@ -98,7 +98,7 @@ namespace Ha {
static List<Component> components;
bool multiValueComponent = false;
Component(const char* name, const char* id, const char* type) : name(name), id((char*)id), type(type) {
Component(const char* name, const char* id, const char* type) : name(name), id(id), type(type) {
components.add(this);
}
@ -134,7 +134,7 @@ namespace Ha {
if (deviceClass) jsonDoc["device_class"] = deviceClass;
jsonDoc["name"] = name;
buildUniqueId();
jsonDoc["unique_id"] = uniqueId;
jsonDoc["unique_id"] = (const char*)uniqueId;
buildConfigTopic();
buildComponentConfig(jsonDoc);
@ -276,7 +276,7 @@ namespace Ha {
virtual void buildCommandConfig(JsonDocument& jsonDoc) = 0;
void buildComponentConfig(JsonDocument& jsonDoc) override {
jsonDoc["command_topic"] = commandTopic;
jsonDoc["command_topic"] = (const char*)commandTopic;
if (retain) jsonDoc["retain"] = true;
buildCommandConfig(jsonDoc);
}
@ -308,7 +308,7 @@ namespace Ha {
protected:
Component* cmp;
void buildConfig(JsonDocument& jsonDoc) {
if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic;
if (stateTopic[0]) jsonDoc["state_topic"] = (const char*)stateTopic;
}
};