Skip to content

Commit

Permalink
Move from json11 to jsoncpp
Browse files Browse the repository at this point in the history
This way we can make use of system-wide installed jsoncpp.
  • Loading branch information
julianoes authored and JonasVautherin committed Sep 30, 2019
1 parent 1a5e360 commit 57d1dbd
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 75 deletions.
2 changes: 1 addition & 1 deletion MAVSDKConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if(NOT @BUILD_SHARED_LIBS@)
find_dependency(CURL REQUIRED CONFIG)
endif()

find_dependency(json11)
find_dependency(jsoncpp)
find_dependency(tinyxml2)

if(@BUILD_BACKEND@)
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/mission/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
find_package(json11 REQUIRED)
find_package(jsoncpp REQUIRED)

add_library(mavsdk_mission
mission.cpp
Expand All @@ -19,7 +19,7 @@ target_link_libraries(mavsdk_mission
PUBLIC
mavsdk
PRIVATE
json11::json11
jsoncpp_lib
)

target_include_directories(mavsdk_mission PUBLIC
Expand Down
41 changes: 22 additions & 19 deletions src/plugins/mission/mission_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1433,26 +1433,29 @@ Mission::Result MissionImpl::import_qgroundcontrol_mission(
Mission::mission_items_t& mission_items, const std::string& qgc_plan_file)
{
std::ifstream file(qgc_plan_file);
if (!file) { // File open error
if (!file) {
return Mission::Result::FAILED_TO_OPEN_QGC_PLAN;
}

// Read QGC plan into a string stream
std::stringstream ss;
ss << file.rdbuf();
file.close();

std::string err;
const auto parsed_plan = Json::parse(ss.str(), err); // parse QGC plan
if (!err.empty()) { // Parse error
const auto raw_json = ss.str();

Json::CharReaderBuilder builder;
const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
Json::Value root;
JSONCPP_STRING err;
const bool ok =
reader->parse(raw_json.c_str(), raw_json.c_str() + raw_json.length(), &root, &err);
if (!ok) {
LogErr() << "Parse error: " << err;
return Mission::Result::FAILED_TO_PARSE_QGC_PLAN;
}

// Clear old mission items
mission_items.clear();

// Import mission items
return import_mission_items(mission_items, parsed_plan);
return import_mission_items(mission_items, root);
}

// Build a mission item out of command, params and add them to the mission vector.
Expand Down Expand Up @@ -1543,36 +1546,36 @@ Mission::Result MissionImpl::build_mission_items(
}

Mission::Result MissionImpl::import_mission_items(
Mission::mission_items_t& all_mission_items, const Json& qgc_plan_json)
Mission::mission_items_t& all_mission_items, const Json::Value& qgc_plan_json)
{
const auto json_mission_items = qgc_plan_json["mission"];
Mission::Result result = Mission::Result::SUCCESS;
auto new_mission_item = std::make_shared<MissionItem>();

// Iterate JSON mission items and build Mavsdk mission items
for (auto& json_mission_item : json_mission_items["items"].array_items()) {
// Iterate mission items and build Mavsdk mission items.
for (auto& json_mission_item : json_mission_items["items"]) {
// Parameters of Mission item & MAV command of it.
MAV_CMD command = static_cast<MAV_CMD>(json_mission_item["command"].int_value());
MAV_CMD command = static_cast<MAV_CMD>(json_mission_item["command"].asInt());

// Extract parameters of each mission item
std::vector<double> params;
for (auto& p : json_mission_item["params"].array_items()) {
if (p.is_null()) {
for (auto& p : json_mission_item["params"]) {
if (p.type() == Json::nullValue) {
// QGC sets params as `null` if they should be unchanged.
params.push_back(double(NAN));
} else {
params.push_back(p.number_value());
params.push_back(p.asDouble());
}
}

result = build_mission_items(command, params, new_mission_item, all_mission_items);
Mission::Result result =
build_mission_items(command, params, new_mission_item, all_mission_items);
if (result != Mission::Result::SUCCESS) {
break;
}
}
// Don't forget to add the last mission which possibly didn't have position set.
all_mission_items.push_back(new_mission_item);
return result;
return Mission::Result::SUCCESS;
}

} // namespace mavsdk
9 changes: 4 additions & 5 deletions src/plugins/mission/mission_impl.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <json11.hpp>
#include <json/json.h>
#include <map>
#include <memory>
#include <mutex>
Expand All @@ -10,8 +10,6 @@
#include "plugin_impl_base.h"
#include "system.h"

using namespace json11;

namespace mavsdk {

class MissionImpl : public PluginImplBase {
Expand Down Expand Up @@ -92,8 +90,9 @@ class MissionImpl : public PluginImplBase {

void reset_mission_progress();

static Mission::Result
import_mission_items(Mission::mission_items_t& mission_items, const Json& mission_json);
static Mission::Result import_mission_items(
Mission::mission_items_t& all_mission_items, const Json::Value& qgc_plan_json);

static Mission::Result build_mission_items(
MAV_CMD command,
std::vector<double> params,
Expand Down
2 changes: 1 addition & 1 deletion third_party/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ include(cmake/build_target.cmake)
list(APPEND CMAKE_PREFIX_PATH "${DEPS_INSTALL_PATH}")
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} PARENT_SCOPE)

build_target(json11)
build_target(jsoncpp)
build_target(tinyxml2)

if(NOT IOS)
Expand Down
40 changes: 0 additions & 40 deletions third_party/json11/export_config.patch

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.1)

project(external-json11)
project(external-jsoncpp)
include(ExternalProject)

list(APPEND CMAKE_ARGS
Expand All @@ -22,16 +22,15 @@ elseif(IOS)
)
endif()

message(STATUS "Preparing external project \"json11\" with args:")
message(STATUS "Preparing external project \"jsoncpp\" with args:")
foreach(CMAKE_ARG ${CMAKE_ARGS})
message(STATUS "-- ${CMAKE_ARG}")
endforeach()

ExternalProject_Add(
json11
GIT_REPOSITORY https://github.com/dropbox/json11
GIT_TAG v1.0.0
PREFIX json11
PATCH_COMMAND git apply ${PROJECT_SOURCE_DIR}/export_config.patch
jsoncpp
GIT_REPOSITORY https://github.com/open-source-parsers/jsoncpp
GIT_TAG 1.9.1
PREFIX jsonpp
CMAKE_ARGS "${CMAKE_ARGS}"
)

0 comments on commit 57d1dbd

Please sign in to comment.