Skip to content

Commit 7252355

Browse files
authored
Merge pull request #181 from BlueAndi/feature/ArduinoJSONv7
Upgrade to ArduinoJSON v7.2.0
2 parents 287bbdb + a77dc91 commit 7252355

File tree

10 files changed

+144
-167
lines changed

10 files changed

+144
-167
lines changed

lib/ConvoyFollower/src/App.cpp

+6-9
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ const char* App::TOPIC_NAME_BIRTH = "dcs/birth";
8585
/* MQTT topic name for will messages. */
8686
const char* App::TOPIC_NAME_WILL = "dcs/will";
8787

88-
/** Buffer size for JSON serialization of birth / will message */
89-
static const uint32_t JSON_BIRTHMESSAGE_MAX_SIZE = 64U;
90-
9188
/******************************************************************************
9289
* Public Methods
9390
*****************************************************************************/
@@ -273,14 +270,14 @@ void App::fatalErrorHandler()
273270
bool App::setupMqttClient()
274271
{
275272
/* Setup MQTT Server, Birth and Will messages. */
276-
bool isSuccessful = false;
277-
SettingsHandler& settings = SettingsHandler::getInstance();
278-
StaticJsonDocument<JSON_BIRTHMESSAGE_MAX_SIZE> birthDoc;
279-
String birthMessage;
273+
bool isSuccessful = false;
274+
SettingsHandler& settings = SettingsHandler::getInstance();
275+
JsonDocument jsonBirthDoc;
276+
String birthMessage;
280277

281-
birthDoc["name"] = settings.getRobotName();
278+
jsonBirthDoc["name"] = settings.getRobotName();
282279

283-
if (0U == serializeJson(birthDoc, birthMessage))
280+
if (0U == serializeJson(jsonBirthDoc, birthMessage))
284281
{
285282
/* Non-fatal error. Birth message will be empty. */
286283
LOG_ERROR("Failed to serialize birth message.");

lib/ConvoyLeader/src/App.cpp

+6-9
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ const char* App::TOPIC_NAME_BIRTH = "dcs/birth";
8585
/* MQTT topic name for will messages. */
8686
const char* App::TOPIC_NAME_WILL = "dcs/will";
8787

88-
/** Buffer size for JSON serialization of birth / will message */
89-
static const uint32_t JSON_BIRTHMESSAGE_MAX_SIZE = 64U;
90-
9188
/******************************************************************************
9289
* Public Methods
9390
*****************************************************************************/
@@ -274,14 +271,14 @@ void App::fatalErrorHandler()
274271
bool App::setupMqttClient()
275272
{
276273
/* Setup MQTT Server, Birth and Will messages. */
277-
bool isSuccessful = false;
278-
SettingsHandler& settings = SettingsHandler::getInstance();
279-
StaticJsonDocument<JSON_BIRTHMESSAGE_MAX_SIZE> birthDoc;
280-
String birthMessage;
274+
bool isSuccessful = false;
275+
SettingsHandler& settings = SettingsHandler::getInstance();
276+
JsonDocument jsonBirthDoc;
277+
String birthMessage;
281278

282-
birthDoc["name"] = settings.getRobotName();
279+
jsonBirthDoc["name"] = settings.getRobotName();
283280

284-
if (0U == serializeJson(birthDoc, birthMessage))
281+
if (0U == serializeJson(jsonBirthDoc, birthMessage))
285282
{
286283
/* Non-fatal error. Birth message will be empty. */
287284
LOG_ERROR("Failed to serialize birth message.");

lib/MainNative/src/main.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,7 @@ static int createConfigFile(const PrgArguments& prgArgs)
527527
}
528528
else
529529
{
530-
const size_t JSON_DOC_SIZE = 2048U;
531-
DynamicJsonDocument jsonDoc(JSON_DOC_SIZE);
530+
JsonDocument jsonDoc;
532531

533532
jsonDoc[ConfigurationKeys::ROBOT_NAME] = prgArgs.robotName;
534533
jsonDoc[ConfigurationKeys::WIFI][ConfigurationKeys::SSID] = WIFI_SSID_DEFAULT;

lib/PlatoonService/src/V2VCommManager.cpp

+33-38
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,6 @@ const char* V2VCommManager::TOPIC_NAME_IVS = "ivs";
7777
/* MQTT subtopic name for Platoon Length. */
7878
const char* V2VCommManager::TOPIC_NAME_PLATOON_LENGTH = "length";
7979

80-
/** Buffer size for JSON serialization of heartbeat messages. */
81-
static const uint32_t JSON_HEARTBEAT_MAX_SIZE = 128U;
82-
83-
/** Default size of the JSON Document for parsing. */
84-
static const uint32_t JSON_DOC_DEFAULT_SIZE = 512U;
85-
8680
/******************************************************************************
8781
* Public Methods
8882
*****************************************************************************/
@@ -302,8 +296,8 @@ bool V2VCommManager::sendIVS(const int32_t ivs) const
302296
V2VEventType type = V2V_EVENT_IVS;
303297

304298
/* Create JSON document. */
305-
StaticJsonDocument<JSON_DOC_DEFAULT_SIZE> jsonPayload;
306-
String payload;
299+
JsonDocument jsonPayload;
300+
String payload;
307301

308302
jsonPayload["ivs"] = ivs;
309303

@@ -375,29 +369,29 @@ int32_t V2VCommManager::getPlatoonLength() const
375369
void V2VCommManager::eventCallback(const String& payload)
376370
{
377371
/* Deserialize payload. */
378-
StaticJsonDocument<JSON_DOC_DEFAULT_SIZE> jsonPayload;
379-
DeserializationError error = deserializeJson(jsonPayload, payload.c_str());
372+
JsonDocument jsonPayload;
373+
DeserializationError error = deserializeJson(jsonPayload, payload.c_str());
380374

381375
if (DeserializationError::Ok != error)
382376
{
383377
LOG_ERROR("JSON Deserialization Error %d.", error);
384378
}
385379
else
386380
{
387-
JsonVariant jsonEventVehicleId = jsonPayload["id"]; /* Vehicle ID. */
388-
JsonVariant jsonEventType = jsonPayload["type"]; /* Event type. */
389-
JsonVariant jsonEventTimestamp = jsonPayload["timestamp"]; /* Timestamp [ms]. */
390-
JsonVariant jsonEventData = jsonPayload["data"]; /* Event data. */
381+
JsonVariantConst jsonEventVehicleId = jsonPayload["id"]; /* Vehicle ID. */
382+
JsonVariantConst jsonEventType = jsonPayload["type"]; /* Event type. */
383+
JsonVariantConst jsonEventTimestamp = jsonPayload["timestamp"]; /* Timestamp [ms]. */
384+
JsonVariantConst jsonEventData = jsonPayload["data"]; /* Event data. */
391385

392386
if ((false == jsonEventVehicleId.isNull()) && (false == jsonEventType.isNull()) &&
393387
(false == jsonEventTimestamp.isNull()) && (false == jsonEventData.isNull()))
394388
{
395-
bool pushEventToQueue = false;
396-
uint8_t eventVehicleId = jsonEventVehicleId.as<uint8_t>();
397-
V2VEventType eventType = jsonEventType.as<V2VEventType>();
398-
uint32_t eventTimestamp = jsonEventTimestamp.as<uint32_t>();
399-
JsonObject eventDataAsJson = jsonEventData.as<JsonObject>();
400-
void* eventData = nullptr;
389+
bool pushEventToQueue = false;
390+
uint8_t eventVehicleId = jsonEventVehicleId.as<uint8_t>();
391+
V2VEventType eventType = jsonEventType.as<V2VEventType>();
392+
uint32_t eventTimestamp = jsonEventTimestamp.as<uint32_t>();
393+
JsonObjectConst eventDataAsJson = jsonEventData.as<JsonObjectConst>();
394+
void* eventData = nullptr;
401395

402396
switch (eventType)
403397
{
@@ -446,8 +440,8 @@ void V2VCommManager::eventCallback(const String& payload)
446440
}
447441
else
448442
{
449-
JsonVariant jsonEventDataStatus = eventDataAsJson["status"]; /* Vehicle status. */
450-
JsonVariant jsonEventDataTimestamp = eventDataAsJson["timestamp"]; /* Timestamp [ms]. */
443+
JsonVariantConst jsonEventDataStatus = eventDataAsJson["status"]; /* Vehicle status. */
444+
JsonVariantConst jsonEventDataTimestamp = eventDataAsJson["timestamp"]; /* Timestamp [ms]. */
451445

452446
if ((false == jsonEventDataStatus.isNull()) && (false == jsonEventDataTimestamp.isNull()))
453447
{
@@ -466,18 +460,19 @@ void V2VCommManager::eventCallback(const String& payload)
466460
case V2V_EVENT_PLATOON_HEARTBEAT:
467461
if (PLATOON_LEADER_ID != m_vehicleId)
468462
{
469-
JsonVariant jsonEventDataTimestamp = eventDataAsJson["timestamp"]; /* Timestamp [ms]. */
463+
JsonVariantConst jsonEventDataTimestamp = eventDataAsJson["timestamp"]; /* Timestamp [ms]. */
470464

471465
if (false == jsonEventDataTimestamp.isNull())
472466
{
473467
/* Timestamp is sent back to acknowledge synchronization. */
474-
uint32_t eventDataTimestamp = jsonEventDataTimestamp.as<uint32_t>();
475-
StaticJsonDocument<JSON_HEARTBEAT_MAX_SIZE> heartbeatDoc;
476-
heartbeatDoc["timestamp"] = eventDataTimestamp;
477-
heartbeatDoc["status"] = m_vehicleStatus;
468+
uint32_t eventDataTimestamp = jsonEventDataTimestamp.as<uint32_t>();
469+
JsonDocument jsonHeartbeatDoc;
470+
471+
jsonHeartbeatDoc["timestamp"] = eventDataTimestamp;
472+
jsonHeartbeatDoc["status"] = m_vehicleStatus;
478473

479474
if (false == publishEvent(m_heartbeatResponseTopic, V2V_EVENT_VEHICLE_HEARTBEAT,
480-
heartbeatDoc.as<JsonObject>()))
475+
jsonHeartbeatDoc.as<JsonObject>()))
481476
{
482477
LOG_ERROR("Failed to publish MQTT message to %s.", m_heartbeatResponseTopic.c_str());
483478
}
@@ -500,7 +495,7 @@ void V2VCommManager::eventCallback(const String& payload)
500495
}
501496
else
502497
{
503-
JsonVariant jsonEventDataIVS = eventDataAsJson["ivs"]; /* Inter Vehicle Space. */
498+
JsonVariantConst jsonEventDataIVS = eventDataAsJson["ivs"]; /* Inter Vehicle Space. */
504499

505500
if (false == jsonEventDataIVS.isNull())
506501
{
@@ -729,13 +724,13 @@ bool V2VCommManager::sendPlatoonHeartbeat()
729724
bool isSuccessful = false;
730725

731726
/* Send platoon heartbeat. */
732-
StaticJsonDocument<JSON_HEARTBEAT_MAX_SIZE> heartbeatDoc;
733-
String heartbeatPayload;
734-
uint32_t timestamp = millis();
727+
JsonDocument jsonHeartbeatDoc;
728+
String heartbeatPayload;
729+
uint32_t timestamp = millis();
735730

736-
heartbeatDoc["timestamp"] = timestamp;
731+
jsonHeartbeatDoc["timestamp"] = timestamp;
737732

738-
if (0U == serializeJson(heartbeatDoc, heartbeatPayload))
733+
if (0U == serializeJson(jsonHeartbeatDoc, heartbeatPayload))
739734
{
740735
LOG_ERROR("Failed to serialize heartbeat.");
741736
}
@@ -762,8 +757,8 @@ bool V2VCommManager::publishEvent(const String& topic, V2VEventType type, const
762757
bool isSuccessful = false;
763758

764759
/* Create JSON document. */
765-
StaticJsonDocument<JSON_DOC_DEFAULT_SIZE> jsonPayload;
766-
String payload;
760+
JsonDocument jsonPayload;
761+
String payload;
767762

768763
jsonPayload["id"] = m_vehicleId;
769764
jsonPayload["type"] = type;
@@ -844,8 +839,8 @@ bool V2VCommManager::sendPlatoonLength(const int32_t length) const
844839
V2VEventType type = V2V_EVENT_IVS;
845840

846841
/* Create JSON document. */
847-
StaticJsonDocument<JSON_DOC_DEFAULT_SIZE> jsonPayload;
848-
String payload;
842+
JsonDocument jsonPayload;
843+
String payload;
849844

850845
jsonPayload["length"] = length;
851846

lib/PlatoonService/src/Waypoint.cpp

+24-32
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,15 @@
5656
* Local Variables
5757
*****************************************************************************/
5858

59-
/** Default size of the JSON Document for parsing. */
60-
static const uint32_t JSON_DOC_DEFAULT_SIZE = 1024U;
61-
6259
/******************************************************************************
6360
* Public Methods
6461
*****************************************************************************/
6562

6663
Waypoint* Waypoint::deserialize(const String& serializedWaypoint)
6764
{
68-
Waypoint* waypoint = nullptr;
69-
StaticJsonDocument<JSON_DOC_DEFAULT_SIZE> jsonPayload;
70-
DeserializationError error = deserializeJson(jsonPayload, serializedWaypoint.c_str());
65+
Waypoint* waypoint = nullptr;
66+
JsonDocument jsonPayload;
67+
DeserializationError error = deserializeJson(jsonPayload, serializedWaypoint.c_str());
7168

7269
if (DeserializationError::Ok != error)
7370
{
@@ -82,41 +79,36 @@ Waypoint* Waypoint::deserialize(const String& serializedWaypoint)
8279
return waypoint;
8380
}
8481

85-
Waypoint* Waypoint::fromJsonObject(const JsonObject& jsonWaypoint)
82+
Waypoint* Waypoint::fromJsonObject(const JsonObjectConst& jsonWaypoint)
8683
{
87-
Waypoint* waypoint = nullptr;
88-
89-
if (jsonWaypoint.containsKey("X") && jsonWaypoint.containsKey("Y") && jsonWaypoint.containsKey("Orientation") &&
90-
jsonWaypoint.containsKey("Left") && jsonWaypoint.containsKey("Right") && jsonWaypoint.containsKey("Center"))
84+
Waypoint* waypoint = nullptr;
85+
JsonVariantConst jsonXPos = jsonWaypoint["X"]; /* X position [mm]. */
86+
JsonVariantConst jsonYPos = jsonWaypoint["Y"]; /* Y position [mm]. */
87+
JsonVariantConst jsonOrientation = jsonWaypoint["Orientation"]; /* Orientation [mrad]. */
88+
JsonVariantConst jsonLeft = jsonWaypoint["Left"]; /* Left motor speed [mm/s]. */
89+
JsonVariantConst jsonRight = jsonWaypoint["Right"]; /* Right motor speed [mm/s]. */
90+
JsonVariantConst jsonCenter = jsonWaypoint["Center"]; /* Center speed [mm/s]. */
91+
92+
if ((false == jsonXPos.isNull()) && (false == jsonYPos.isNull()) && (false == jsonOrientation.isNull()) &&
93+
(false == jsonLeft.isNull()) && (false == jsonRight.isNull()) && (false == jsonCenter.isNull()))
9194
{
92-
JsonVariant jsonXPos = jsonWaypoint["X"]; /* X position [mm]. */
93-
JsonVariant jsonYPos = jsonWaypoint["Y"]; /* Y position [mm]. */
94-
JsonVariant jsonOrientation = jsonWaypoint["Orientation"]; /* Orientation [mrad]. */
95-
JsonVariant jsonLeft = jsonWaypoint["Left"]; /* Left motor speed [mm/s]. */
96-
JsonVariant jsonRight = jsonWaypoint["Right"]; /* Right motor speed [mm/s]. */
97-
JsonVariant jsonCenter = jsonWaypoint["Center"]; /* Center speed [mm/s]. */
98-
99-
if ((false == jsonXPos.isNull()) && (false == jsonYPos.isNull()) && (false == jsonOrientation.isNull()) &&
100-
(false == jsonLeft.isNull()) && (false == jsonRight.isNull()) && (false == jsonCenter.isNull()))
101-
{
102-
103-
int32_t xPos = jsonXPos.as<int32_t>();
104-
int32_t yPos = jsonYPos.as<int32_t>();
105-
int32_t orientation = jsonOrientation.as<int32_t>();
106-
int32_t left = jsonLeft.as<int32_t>();
107-
int32_t right = jsonRight.as<int32_t>();
108-
int32_t center = jsonCenter.as<int32_t>();
109-
110-
waypoint = new (std::nothrow) Waypoint(xPos, yPos, orientation, left, right, center);
111-
}
95+
96+
int32_t xPos = jsonXPos.as<int32_t>();
97+
int32_t yPos = jsonYPos.as<int32_t>();
98+
int32_t orientation = jsonOrientation.as<int32_t>();
99+
int32_t left = jsonLeft.as<int32_t>();
100+
int32_t right = jsonRight.as<int32_t>();
101+
int32_t center = jsonCenter.as<int32_t>();
102+
103+
waypoint = new (std::nothrow) Waypoint(xPos, yPos, orientation, left, right, center);
112104
}
113105

114106
return waypoint;
115107
}
116108

117109
void Waypoint::serialize(String& serializedWaypoint) const
118110
{
119-
StaticJsonDocument<JSON_DOC_DEFAULT_SIZE> jsonPayload;
111+
JsonDocument jsonPayload;
120112

121113
jsonPayload["X"] = xPos; /* X position [mm]. */
122114
jsonPayload["Y"] = yPos; /* Y position [mm]. */

lib/PlatoonService/src/Waypoint.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ struct Waypoint
114114
*
115115
* @return Pointer to a waypoint object. In case of an error, it returns nullptr.
116116
*/
117-
static Waypoint* fromJsonObject(const JsonObject& jsonWaypoint);
117+
static Waypoint* fromJsonObject(const JsonObjectConst& jsonWaypoint);
118118

119119
/**
120120
* Serialize the waypoint.

lib/RemoteControl/src/App.cpp

+13-16
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ const char* App::TOPIC_NAME_CMD = "cmd";
8787
/* MQTT topic name for receiving motor speeds. */
8888
const char* App::TOPIC_NAME_MOTOR_SPEEDS = "motorSpeeds";
8989

90-
/** Default size of the JSON Document for parsing. */
91-
static const uint32_t JSON_DOC_DEFAULT_SIZE = 1024U;
92-
9390
/** Buffer size for JSON serialization of birth / will message */
9491
static const uint32_t JSON_BIRTHMESSAGE_MAX_SIZE = 64U;
9592

@@ -147,12 +144,12 @@ void App::setup()
147144
else
148145
{
149146
/* Setup MQTT Server, Birth and Will messages. */
150-
StaticJsonDocument<JSON_BIRTHMESSAGE_MAX_SIZE> birthDoc;
151-
char birthMsgArray[JSON_BIRTHMESSAGE_MAX_SIZE];
152-
String birthMessage;
147+
JsonDocument jsonBirthDoc;
148+
char birthMsgArray[JSON_BIRTHMESSAGE_MAX_SIZE];
149+
String birthMessage;
153150

154-
birthDoc["name"] = settings.getRobotName().c_str();
155-
(void)serializeJson(birthDoc, birthMsgArray);
151+
jsonBirthDoc["name"] = settings.getRobotName().c_str();
152+
(void)serializeJson(jsonBirthDoc, birthMsgArray);
156153
birthMessage = birthMsgArray;
157154

158155
/* Setup SerialMuxProt Channels */
@@ -233,7 +230,7 @@ void App::loop()
233230
initialVehicleData.orientation = settings.getInitialHeading();
234231

235232
if (true == m_smpServer.sendData(m_serialMuxProtChannelInitialVehicleData, &initialVehicleData,
236-
sizeof(initialVehicleData)))
233+
sizeof(initialVehicleData)))
237234
{
238235
LOG_DEBUG("Initial vehicle data sent.");
239236
m_initialDataSent = true;
@@ -275,16 +272,16 @@ void App::fatalErrorHandler()
275272

276273
void App::cmdTopicCallback(const String& payload)
277274
{
278-
StaticJsonDocument<JSON_DOC_DEFAULT_SIZE> jsonPayload;
279-
DeserializationError error = deserializeJson(jsonPayload, payload.c_str());
275+
JsonDocument jsonPayload;
276+
DeserializationError error = deserializeJson(jsonPayload, payload.c_str());
280277

281278
if (error != DeserializationError::Ok)
282279
{
283280
LOG_ERROR("JSON Deserialization Error %d.", error);
284281
}
285282
else
286283
{
287-
JsonVariant command = jsonPayload["CMD_ID"];
284+
JsonVariantConst command = jsonPayload["CMD_ID"];
288285

289286
if (false == command.isNull())
290287
{
@@ -352,17 +349,17 @@ void App::cmdTopicCallback(const String& payload)
352349

353350
void App::motorSpeedsTopicCallback(const String& payload)
354351
{
355-
StaticJsonDocument<JSON_DOC_DEFAULT_SIZE> jsonPayload;
356-
DeserializationError error = deserializeJson(jsonPayload, payload.c_str());
352+
JsonDocument jsonPayload;
353+
DeserializationError error = deserializeJson(jsonPayload, payload.c_str());
357354

358355
if (error != DeserializationError::Ok)
359356
{
360357
LOG_ERROR("JSON deserialization error %d.", error);
361358
}
362359
else
363360
{
364-
JsonVariant leftSpeed = jsonPayload["LEFT"];
365-
JsonVariant rightSpeed = jsonPayload["RIGHT"];
361+
JsonVariantConst leftSpeed = jsonPayload["LEFT"];
362+
JsonVariantConst rightSpeed = jsonPayload["RIGHT"];
366363

367364
if ((false == leftSpeed.isNull()) && (false == rightSpeed.isNull()))
368365
{

0 commit comments

Comments
 (0)