Skip to content

Commit 22bcdb9

Browse files
Harmonize Json parsing olp-cpp-sdk-core and write
Migrating from RapidJSON Temporary introduced boost_parser to keep same names and later replace rapidjson based parser with this boost based. Relates-To: OCMAM-445 Signed-off-by: Rustam Gamidov <ext-rustam.gamidov@here.com>
1 parent 5d02af8 commit 22bcdb9

7 files changed

Lines changed: 219 additions & 28 deletions

File tree

olp-cpp-sdk-core/include/olp/core/generated/parser/JsonParser.h

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2024 HERE Europe B.V.
2+
* Copyright (C) 2019-2026 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@
2626

2727
#include <rapidjson/document.h>
2828
#include <rapidjson/istreamwrapper.h>
29+
#include <boost/json/parse.hpp>
2930

3031
#include "ParserWrapper.h"
3132

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

77+
namespace boost_parser {
78+
79+
template <typename T>
80+
inline T parse(const std::string& json) {
81+
boost::json::error_code ec;
82+
auto value = boost::json::parse(json, ec);
83+
T result{};
84+
if (value.is_object() || value.is_array()) {
85+
from_json(value, result);
86+
}
87+
return result;
88+
}
89+
90+
template <typename T>
91+
inline T parse(std::stringstream& json_stream, bool& res) {
92+
res = false;
93+
boost::json::error_code ec;
94+
auto value = boost::json::parse(json_stream, ec);
95+
T result{};
96+
if (value.is_object() || value.is_array()) {
97+
from_json(value, result);
98+
res = true;
99+
}
100+
return result;
101+
}
102+
103+
template <typename T>
104+
inline T parse(std::stringstream& json_stream) {
105+
bool res = true;
106+
return parse<T>(json_stream, res);
107+
}
108+
109+
template <typename T>
110+
inline T parse(const std::shared_ptr<std::vector<unsigned char>>& json_bytes) {
111+
boost::json::string_view json(reinterpret_cast<char*>(json_bytes->data()),
112+
json_bytes->size());
113+
boost::json::error_code ec;
114+
auto value = boost::json::parse(json, ec);
115+
T result{};
116+
if (value.is_object() || value.is_array()) {
117+
from_json(value, result);
118+
}
119+
return result;
120+
}
121+
122+
} // namespace boost_parser
123+
76124
} // namespace parser
77125
} // namespace olp

olp-cpp-sdk-core/include/olp/core/generated/parser/ParserWrapper.h

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2023 HERE Europe B.V.
2+
* Copyright (C) 2019-2026 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,10 +22,13 @@
2222
#include <map>
2323
#include <memory>
2424
#include <string>
25+
#include <utility>
2526
#include <vector>
2627

2728
#include <olp/core/porting/optional.h>
29+
#include <rapidjson/document.h>
2830
#include <rapidjson/rapidjson.h>
31+
#include <boost/json/value.hpp>
2932

3033
namespace olp {
3134
namespace parser {
@@ -94,5 +97,72 @@ inline T parse(const rapidjson::Value& value, const std::string& name) {
9497
return result;
9598
}
9699

100+
inline void from_json(const boost::json::value& value, std::string& x) {
101+
const auto& str = value.get_string();
102+
x.assign(str.begin(), str.end());
103+
}
104+
105+
inline void from_json(const boost::json::value& value, int32_t& x) {
106+
x = static_cast<int32_t>(value.to_number<int64_t>());
107+
}
108+
109+
inline void from_json(const boost::json::value& value, int64_t& x) {
110+
x = value.to_number<int64_t>();
111+
}
112+
113+
inline void from_json(const boost::json::value& value, double& x) {
114+
x = value.to_number<double>();
115+
}
116+
117+
inline void from_json(const boost::json::value& value, bool& x) {
118+
x = value.get_bool();
119+
}
120+
121+
inline void from_json(const boost::json::value& value,
122+
std::shared_ptr<std::vector<unsigned char>>& x) {
123+
const auto& s = value.get_string();
124+
x = std::make_shared<std::vector<unsigned char>>(s.begin(), s.end());
125+
}
126+
127+
template <typename T>
128+
inline void from_json(const boost::json::value& value,
129+
porting::optional<T>& x) {
130+
T result = T();
131+
from_json(value, result);
132+
x = result;
133+
}
134+
135+
template <typename T>
136+
inline void from_json(const boost::json::value& value,
137+
std::map<std::string, T>& results) {
138+
const auto& object = value.get_object();
139+
for (const auto& object_value : object) {
140+
std::string key = object_value.key();
141+
from_json(object_value.value(), results[key]);
142+
}
143+
}
144+
145+
template <typename T>
146+
inline void from_json(const boost::json::value& value,
147+
std::vector<T>& results) {
148+
const auto& array = value.get_array();
149+
for (const auto& array_value : array) {
150+
T result;
151+
from_json(array_value, result);
152+
results.emplace_back(std::move(result));
153+
}
154+
}
155+
156+
template <typename T>
157+
inline T parse(const boost::json::value& value, const std::string& name) {
158+
T result = T();
159+
const auto& object = value.get_object();
160+
auto itr = object.find(name);
161+
if (itr != object.end()) {
162+
from_json(itr->value(), result);
163+
}
164+
return result;
165+
}
166+
97167
} // namespace parser
98168
} // namespace olp

olp-cpp-sdk-core/include/olp/core/generated/serializer/SerializerWrapper.h

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2021 HERE Europe B.V.
2+
* Copyright (C) 2019-2026 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727

2828
#include <olp/core/porting/optional.h>
2929
#include <rapidjson/document.h>
30+
#include <boost/json/value.hpp>
3031

3132
namespace olp {
3233
namespace serializer {
@@ -110,5 +111,72 @@ inline void serialize(const std::string& key, const T& x,
110111
}
111112
}
112113

114+
inline void to_json(const std::string& x, boost::json::value& value) {
115+
value.emplace_string() = x;
116+
}
117+
118+
inline void to_json(int32_t x, boost::json::value& value) {
119+
value.emplace_int64() = x;
120+
}
121+
122+
inline void to_json(int64_t x, boost::json::value& value) {
123+
value.emplace_int64() = x;
124+
}
125+
126+
inline void to_json(double x, boost::json::value& value) {
127+
value.emplace_double() = x;
128+
}
129+
inline void to_json(bool x, boost::json::value& value) {
130+
value.emplace_bool() = x;
131+
}
132+
133+
inline void to_json(const std::shared_ptr<std::vector<unsigned char>>& x,
134+
boost::json::value& value) {
135+
value.emplace_string().assign(x->begin(), x->end());
136+
}
137+
138+
template <typename T>
139+
inline void to_json(const porting::optional<T>& x, boost::json::value& value) {
140+
if (x) {
141+
to_json(*x, value);
142+
} else {
143+
value.emplace_null();
144+
}
145+
}
146+
147+
template <typename T>
148+
inline void to_json(const std::map<std::string, T>& x,
149+
boost::json::value& value) {
150+
auto& object = value.emplace_object();
151+
for (auto itr = x.begin(); itr != x.end(); ++itr) {
152+
const auto& key = itr->first;
153+
boost::json::value item_value;
154+
to_json(itr->second, item_value);
155+
object.emplace(key, std::move(item_value));
156+
}
157+
}
158+
159+
template <typename T>
160+
inline void to_json(const std::vector<T>& x, boost::json::value& value) {
161+
auto& array = value.emplace_array();
162+
array.reserve(x.size());
163+
for (typename std::vector<T>::const_iterator itr = x.begin(); itr != x.end();
164+
++itr) {
165+
boost::json::value item_value;
166+
to_json(*itr, item_value);
167+
array.emplace_back(std::move(item_value));
168+
}
169+
}
170+
171+
template <typename T>
172+
inline void serialize(const std::string& key, const T& x,
173+
boost::json::object& value) {
174+
boost::json::value item_value;
175+
to_json(x, item_value);
176+
if (!item_value.is_null()) {
177+
value.emplace(key, std::move(item_value));
178+
}
179+
}
180+
113181
} // namespace serializer
114182
} // namespace olp

olp-cpp-sdk-dataservice-write/src/CatalogSettings.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 HERE Europe B.V.
2+
* Copyright (C) 2020-2026 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,8 @@
1919

2020
#include "CatalogSettings.h"
2121

22+
#include <utility>
23+
2224
#include <olp/core/cache/CacheSettings.h>
2325
#include <olp/core/client/OlpClientSettingsFactory.h>
2426
#include <boost/format.hpp>
@@ -102,7 +104,7 @@ CatalogSettings::LayerSettingsResult CatalogSettings::GetLayerSettings(
102104

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

108110
if (cached_catalog.empty()) {

olp-cpp-sdk-dataservice-write/src/JsonResultParser.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 HERE Europe B.V.
2+
* Copyright (C) 2020-2026 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,7 +38,7 @@ typename std::enable_if<
3838
OutputResult>::type
3939
parse_result(std::stringstream& json_stream, const AdditionalArgs&... args) {
4040
bool res = true;
41-
auto obj = parse<ParsingType>(json_stream, res);
41+
auto obj = boost_parser::parse<ParsingType>(json_stream, res);
4242

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

6161
if (res) {
6262
return OutputResult({std::move(obj), args...});

olp-cpp-sdk-dataservice-write/src/StreamLayerClientImpl.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2021 HERE Europe B.V.
2+
* Copyright (C) 2019-2026 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,10 @@
1919

2020
#include "StreamLayerClientImpl.h"
2121

22+
#include <memory>
23+
#include <string>
24+
#include <utility>
25+
2226
#include <boost/uuid/uuid.hpp>
2327
#include <boost/uuid/uuid_generators.hpp>
2428
#include <boost/uuid/uuid_io.hpp>
@@ -168,7 +172,7 @@ StreamLayerClientImpl::PopFromQueue() {
168172
const auto publish_data_key = uuid_list.substr(0, pos);
169173
auto publish_data_any =
170174
cache_->Get(publish_data_key, [](const std::string& s) {
171-
return olp::parser::parse<model::PublishDataRequest>(s);
175+
return olp::parser::boost_parser::parse<model::PublishDataRequest>(s);
172176
});
173177

174178
cache_->Remove(publish_data_key);

0 commit comments

Comments
 (0)