Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion olp-cpp-sdk-core/include/olp/core/generated/parser/JsonParser.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2024 HERE Europe B.V.
* Copyright (C) 2019-2026 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,6 +26,7 @@

#include <rapidjson/document.h>
#include <rapidjson/istreamwrapper.h>
#include <boost/json/parse.hpp>

#include "ParserWrapper.h"

Expand Down Expand Up @@ -73,5 +74,52 @@ inline T parse(const std::shared_ptr<std::vector<unsigned char>>& json_bytes) {
return result;
}

namespace boost_parser {

template <typename T>
inline T parse(const std::string& json) {
boost::json::error_code ec;
auto value = boost::json::parse(json, ec);
T result{};
if (value.is_object() || value.is_array()) {
from_json(value, result);
}
return result;
}

template <typename T>
inline T parse(std::stringstream& json_stream, bool& res) {
res = false;
boost::json::error_code ec;
auto value = boost::json::parse(json_stream, ec);
T result{};
if (value.is_object() || value.is_array()) {
from_json(value, result);
res = true;
}
return result;
}

template <typename T>
inline T parse(std::stringstream& json_stream) {
bool res = true;
return parse<T>(json_stream, res);
}

template <typename T>
inline T parse(const std::shared_ptr<std::vector<unsigned char>>& json_bytes) {
boost::json::string_view json(reinterpret_cast<char*>(json_bytes->data()),
json_bytes->size());
boost::json::error_code ec;
auto value = boost::json::parse(json, ec);
T result{};
if (value.is_object() || value.is_array()) {
from_json(value, result);
}
return result;
}

} // namespace boost_parser

} // namespace parser
} // namespace olp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2023 HERE Europe B.V.
* Copyright (C) 2019-2026 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,10 +22,13 @@
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include <olp/core/porting/optional.h>
#include <rapidjson/document.h>
#include <rapidjson/rapidjson.h>
#include <boost/json/value.hpp>

namespace olp {
namespace parser {
Expand Down Expand Up @@ -94,5 +97,72 @@ inline T parse(const rapidjson::Value& value, const std::string& name) {
return result;
}

inline void from_json(const boost::json::value& value, std::string& x) {
const auto& str = value.get_string();
x.assign(str.begin(), str.end());
}

inline void from_json(const boost::json::value& value, int32_t& x) {
x = static_cast<int32_t>(value.to_number<int64_t>());
}

inline void from_json(const boost::json::value& value, int64_t& x) {
x = value.to_number<int64_t>();
}

inline void from_json(const boost::json::value& value, double& x) {
x = value.to_number<double>();
}

inline void from_json(const boost::json::value& value, bool& x) {
x = value.get_bool();
}

inline void from_json(const boost::json::value& value,
std::shared_ptr<std::vector<unsigned char>>& x) {
const auto& s = value.get_string();
x = std::make_shared<std::vector<unsigned char>>(s.begin(), s.end());
}

template <typename T>
inline void from_json(const boost::json::value& value,
porting::optional<T>& x) {
T result = T();
from_json(value, result);
x = result;
}

template <typename T>
inline void from_json(const boost::json::value& value,
std::map<std::string, T>& results) {
const auto& object = value.get_object();
for (const auto& object_value : object) {
std::string key = object_value.key();
from_json(object_value.value(), results[key]);
}
}

template <typename T>
inline void from_json(const boost::json::value& value,
std::vector<T>& results) {
const auto& array = value.get_array();
for (const auto& array_value : array) {
T result;
from_json(array_value, result);
results.emplace_back(std::move(result));
}
}

template <typename T>
inline T parse(const boost::json::value& value, const std::string& name) {
T result = T();
const auto& object = value.get_object();
auto itr = object.find(name);
if (itr != object.end()) {
from_json(itr->value(), result);
}
return result;
}

} // namespace parser
} // namespace olp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2021 HERE Europe B.V.
* Copyright (C) 2019-2026 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,7 @@

#include <olp/core/porting/optional.h>
#include <rapidjson/document.h>
#include <boost/json/value.hpp>

namespace olp {
namespace serializer {
Expand Down Expand Up @@ -110,5 +111,72 @@ inline void serialize(const std::string& key, const T& x,
}
}

inline void to_json(const std::string& x, boost::json::value& value) {
value.emplace_string() = x;
}

inline void to_json(int32_t x, boost::json::value& value) {
value.emplace_int64() = x;
}

inline void to_json(int64_t x, boost::json::value& value) {
value.emplace_int64() = x;
}

inline void to_json(double x, boost::json::value& value) {
value.emplace_double() = x;
}
inline void to_json(bool x, boost::json::value& value) {
value.emplace_bool() = x;
}

inline void to_json(const std::shared_ptr<std::vector<unsigned char>>& x,
boost::json::value& value) {
value.emplace_string().assign(x->begin(), x->end());
}

template <typename T>
inline void to_json(const porting::optional<T>& x, boost::json::value& value) {
if (x) {
to_json(*x, value);
} else {
value.emplace_null();
}
}

template <typename T>
inline void to_json(const std::map<std::string, T>& x,
boost::json::value& value) {
auto& object = value.emplace_object();
for (auto itr = x.begin(); itr != x.end(); ++itr) {
const auto& key = itr->first;
boost::json::value item_value;
to_json(itr->second, item_value);
object.emplace(key, std::move(item_value));
}
}

template <typename T>
inline void to_json(const std::vector<T>& x, boost::json::value& value) {
auto& array = value.emplace_array();
array.reserve(x.size());
for (typename std::vector<T>::const_iterator itr = x.begin(); itr != x.end();
++itr) {
boost::json::value item_value;
to_json(*itr, item_value);
array.emplace_back(std::move(item_value));
}
}

template <typename T>
inline void serialize(const std::string& key, const T& x,
boost::json::object& value) {
boost::json::value item_value;
to_json(x, item_value);
if (!item_value.is_null()) {
value.emplace(key, std::move(item_value));
}
}

} // namespace serializer
} // namespace olp
20 changes: 16 additions & 4 deletions olp-cpp-sdk-dataservice-write/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
project(olp-cpp-sdk-dataservice-write VERSION 1.24.0)
set(DESCRIPTION "C++ API library for writing data to OLP")

find_package(Boost REQUIRED)

set(OLP_SDK_DATASERVICE_WRITE_API_HEADERS
./include/olp/dataservice/write/DataServiceWriteApi.h
./include/olp/dataservice/write/IndexLayerClient.h
Expand Down Expand Up @@ -159,16 +161,21 @@ set(OLP_SDK_DATASERVICE_WRITE_SOURCES
./src/generated/serializer/PublishPartitionsSerializer.h
./src/generated/serializer/UpdateIndexRequestSerializer.cpp
./src/generated/serializer/UpdateIndexRequestSerializer.h

./src/utils/BoostJsonSrc.cpp
)

add_library(${PROJECT_NAME}
${OLP_SDK_DATASERVICE_WRITE_INCLUDES}
${OLP_SDK_DATASERVICE_WRITE_SOURCES})

target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/src>)
target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/src>
$<BUILD_INTERFACE:${Boost_INCLUDE_DIRS}>)

target_compile_definitions(${PROJECT_NAME}
PRIVATE DATASERVICE_WRITE_LIBRARY)
Expand All @@ -177,6 +184,11 @@ if(BUILD_SHARED_LIBS)
PUBLIC DATASERVICE_WRITE_SHARED_LIBRARY)
endif()

target_compile_definitions(${PROJECT_NAME}
PRIVATE
BOOST_ALL_NO_LIB
BOOST_JSON_NO_LIB)

# Used also in the package config file
set(PROJECT_LIBS olp-cpp-sdk-core)

Expand Down
6 changes: 4 additions & 2 deletions olp-cpp-sdk-dataservice-write/src/CatalogSettings.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 HERE Europe B.V.
* Copyright (C) 2020-2026 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,8 @@

#include "CatalogSettings.h"

#include <utility>

#include <olp/core/cache/CacheSettings.h>
#include <olp/core/client/OlpClientSettingsFactory.h>
#include <boost/format.hpp>
Expand Down Expand Up @@ -102,7 +104,7 @@ CatalogSettings::LayerSettingsResult CatalogSettings::GetLayerSettings(

const auto& cached_catalog =
cache_->Get(catalog_settings_key, [](const std::string& model) {
return parser::parse<model::Catalog>(model);
return olp::parser::boost_parser::parse<model::Catalog>(model);
});

if (cached_catalog.empty()) {
Expand Down
6 changes: 3 additions & 3 deletions olp-cpp-sdk-dataservice-write/src/JsonResultParser.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 HERE Europe B.V.
* Copyright (C) 2020-2026 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -38,7 +38,7 @@ typename std::enable_if<
OutputResult>::type
parse_result(std::stringstream& json_stream, const AdditionalArgs&... args) {
bool res = true;
auto obj = parse<ParsingType>(json_stream, res);
auto obj = boost_parser::parse<ParsingType>(json_stream, res);

if (res) {
return ParsingType(std::move(obj), args...);
Expand All @@ -56,7 +56,7 @@ typename std::enable_if<
OutputResult>::type
parse_result(std::stringstream& json_stream, const AdditionalArgs&... args) {
bool res = true;
auto obj = parse<ParsingType>(json_stream, res);
auto obj = boost_parser::parse<ParsingType>(json_stream, res);

if (res) {
return OutputResult({std::move(obj), args...});
Expand Down
8 changes: 6 additions & 2 deletions olp-cpp-sdk-dataservice-write/src/StreamLayerClientImpl.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2021 HERE Europe B.V.
* Copyright (C) 2019-2026 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,10 @@

#include "StreamLayerClientImpl.h"

#include <memory>
#include <string>
#include <utility>

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
Expand Down Expand Up @@ -168,7 +172,7 @@ StreamLayerClientImpl::PopFromQueue() {
const auto publish_data_key = uuid_list.substr(0, pos);
auto publish_data_any =
cache_->Get(publish_data_key, [](const std::string& s) {
return olp::parser::parse<model::PublishDataRequest>(s);
return olp::parser::boost_parser::parse<model::PublishDataRequest>(s);
});

cache_->Remove(publish_data_key);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 HERE Europe B.V.
* Copyright (C) 2019-2026 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,11 +19,13 @@

#include "ApiParser.h"

#include <map>

#include <olp/core/generated/parser/ParserWrapper.h>

namespace olp {
namespace parser {
void from_json(const rapidjson::Value& value,
void from_json(const boost::json::value& value,
olp::dataservice::write::model::Api& x) {
x.SetApi(parse<std::string>(value, "api"));
x.SetVersion(parse<std::string>(value, "version"));
Expand Down
Loading
Loading