Skip to content

Commit

Permalink
Fix Position serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
mairas committed Jun 1, 2024
1 parent 5140f6c commit 626cf93
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
21 changes: 21 additions & 0 deletions src/sensesp/types/position.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "sensesp/types/position.h"

#include <ArduinoJson.h>

namespace sensesp {

void convertFromJson(JsonVariantConst src, Position &dst) {
dst.longitude = src["longitude"].as<double>();
dst.latitude = src["latitude"].as<double>();

if (src.containsKey("altitude")) {
dst.altitude = src["altitude"].as<float>();
}
}

bool canConvertFromJson(JsonVariantConst src, const Position &) {
return src.containsKey("latitude") && src.containsKey("longitude");
}


} // namespace sensesp
14 changes: 3 additions & 11 deletions src/sensesp/types/position.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef SENSESP_TYPES_POSITION_H_
#define SENSESP_TYPES_POSITION_H_

#include <ArduinoJson.h>
#include <limits>

namespace sensesp {
Expand Down Expand Up @@ -46,14 +47,7 @@ struct ENUVector {
* @param src The JSON document containing the position data
* @param dst The address to a Position struct to write the data to
*/
void convertFromJson(JsonVariantConst src, Position &dst) {
dst.longitude = src["longitude"].as<double>();
dst.latitude = src["latitude"].as<double>();

if (src.containsKey("altitude")) {
dst.altitude = src["altitude"].as<float>();
}
}
void convertFromJson(JsonVariantConst src, Position &dst);

/**
* @brief Tells ArduinoJson whether the given JSON is a Position or not
Expand All @@ -66,9 +60,7 @@ void convertFromJson(JsonVariantConst src, Position &dst) {
* @return false if the given JSON doesn't contain "latitude" or "longitude"
* keys
*/
bool canConvertFromJson(JsonVariantConst src, const Position &) {
return src.containsKey("latitude") && src.containsKey("longitude");
}
bool canConvertFromJson(JsonVariantConst src, const Position &);

} // namespace sensesp

Expand Down

0 comments on commit 626cf93

Please sign in to comment.