Skip to content

Commit 5615b9a

Browse files
Copy json implementation wrappers to read lib
This allows to keep other libs untouched until fully migrated to the boost::json and remove write lib dependency from public json wrappers. Alternative solutions tried are: - add boost json wrappers to the same files in the core but to additional namespace - changes in core, json still exposed - move boost json wrappers from core to internal for the repository include location and add it to all DataSDK projects - huge change Relates-To: OCMAM-444 Signed-off-by: Rustam Gamidov <ext-rustam.gamidov@here.com>
1 parent 5fe3a9d commit 5615b9a

29 files changed

+315
-33
lines changed

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

Lines changed: 2 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.
@@ -23,8 +23,8 @@
2323
#include <string>
2424
#include <utility>
2525

26+
#include <generated/parser/JsonParser.h>
2627
#include <olp/core/client/ApiError.h>
27-
#include <olp/core/generated/parser/JsonParser.h>
2828
#include <olp/core/logging/Log.h>
2929

3030
namespace olp {

olp-cpp-sdk-dataservice-read/src/generated/parser/ApiParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#include <map>
2323

24-
#include <olp/core/generated/parser/ParserWrapper.h>
24+
#include <generated/parser/ParserWrapper.h>
2525

2626
namespace olp {
2727
namespace parser {

olp-cpp-sdk-dataservice-read/src/generated/parser/CatalogParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#include <vector>
2323

24-
#include <olp/core/generated/parser/ParserWrapper.h>
24+
#include <generated/parser/ParserWrapper.h>
2525

2626
namespace olp {
2727
namespace parser {

olp-cpp-sdk-dataservice-read/src/generated/parser/IndexParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <memory>
2323
#include <vector>
2424

25-
#include <olp/core/generated/parser/ParserWrapper.h>
25+
#include <generated/parser/ParserWrapper.h>
2626

2727
namespace olp {
2828
namespace parser {
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (C) 2019-2026 HERE Europe B.V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#pragma once
21+
22+
#include <memory>
23+
#include <sstream>
24+
#include <string>
25+
#include <vector>
26+
27+
#include <boost/json/parse.hpp>
28+
29+
#include "ParserWrapper.h"
30+
31+
namespace olp {
32+
namespace parser {
33+
34+
template <typename T>
35+
inline T parse(const std::string& json) {
36+
boost::json::error_code ec;
37+
auto value = boost::json::parse(json, ec);
38+
T result{};
39+
if (value.is_object() || value.is_array()) {
40+
from_json(value, result);
41+
}
42+
return result;
43+
}
44+
45+
template <typename T>
46+
inline T parse(std::stringstream& json_stream, bool& res) {
47+
res = false;
48+
boost::json::error_code ec;
49+
auto value = boost::json::parse(json_stream, ec);
50+
T result{};
51+
if (value.is_object() || value.is_array()) {
52+
from_json(value, result);
53+
res = true;
54+
}
55+
return result;
56+
}
57+
58+
template <typename T>
59+
inline T parse(std::stringstream& json_stream) {
60+
bool res = true;
61+
return parse<T>(json_stream, res);
62+
}
63+
64+
template <typename T>
65+
inline T parse(const std::shared_ptr<std::vector<unsigned char>>& json_bytes) {
66+
boost::json::string_view json(reinterpret_cast<char*>(json_bytes->data()),
67+
json_bytes->size());
68+
boost::json::error_code ec;
69+
auto value = boost::json::parse(json, ec);
70+
T result{};
71+
if (value.is_object() || value.is_array()) {
72+
from_json(value, result);
73+
}
74+
return result;
75+
}
76+
77+
} // namespace parser
78+
} // namespace olp

olp-cpp-sdk-dataservice-read/src/generated/parser/LayerVersionsParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#include <vector>
2323

24-
#include <olp/core/generated/parser/ParserWrapper.h>
24+
#include <generated/parser/ParserWrapper.h>
2525

2626
namespace olp {
2727
namespace parser {

olp-cpp-sdk-dataservice-read/src/generated/parser/MessagesParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
// clang-format off
2626
#include "generated/parser/StreamOffsetParser.h"
27-
#include <olp/core/generated/parser/ParserWrapper.h>
27+
#include <generated/parser/ParserWrapper.h>
2828
// clang-format on
2929

3030
namespace olp {
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (C) 2019-2026 HERE Europe B.V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#pragma once
21+
22+
#include <map>
23+
#include <memory>
24+
#include <string>
25+
#include <utility>
26+
#include <vector>
27+
28+
#include <olp/core/porting/optional.h>
29+
#include <boost/json/value.hpp>
30+
31+
namespace olp {
32+
namespace parser {
33+
34+
inline void from_json(const boost::json::value& value, std::string& x) {
35+
const auto& str = value.get_string();
36+
x.assign(str.begin(), str.end());
37+
}
38+
39+
inline void from_json(const boost::json::value& value, int32_t& x) {
40+
x = static_cast<int32_t>(value.to_number<int64_t>());
41+
}
42+
43+
inline void from_json(const boost::json::value& value, int64_t& x) {
44+
x = value.to_number<int64_t>();
45+
}
46+
47+
inline void from_json(const boost::json::value& value, double& x) {
48+
x = value.to_number<double>();
49+
}
50+
51+
inline void from_json(const boost::json::value& value, bool& x) {
52+
x = value.get_bool();
53+
}
54+
55+
inline void from_json(const boost::json::value& value,
56+
std::shared_ptr<std::vector<unsigned char>>& x) {
57+
const auto& s = value.get_string();
58+
x = std::make_shared<std::vector<unsigned char>>(s.begin(), s.end());
59+
}
60+
61+
template <typename T>
62+
inline void from_json(const boost::json::value& value,
63+
porting::optional<T>& x) {
64+
T result = T();
65+
from_json(value, result);
66+
x = result;
67+
}
68+
69+
template <typename T>
70+
inline void from_json(const boost::json::value& value,
71+
std::map<std::string, T>& results) {
72+
const auto& object = value.get_object();
73+
for (const auto& object_value : object) {
74+
std::string key = object_value.key();
75+
from_json(object_value.value(), results[key]);
76+
}
77+
}
78+
79+
template <typename T>
80+
inline void from_json(const boost::json::value& value,
81+
std::vector<T>& results) {
82+
const auto& array = value.get_array();
83+
for (const auto& array_value : array) {
84+
T result;
85+
from_json(array_value, result);
86+
results.emplace_back(std::move(result));
87+
}
88+
}
89+
90+
template <typename T>
91+
inline T parse(const boost::json::value& value, const std::string& name) {
92+
T result = T();
93+
const auto& object = value.get_object();
94+
auto itr = object.find(name);
95+
if (itr != object.end()) {
96+
from_json(itr->value(), result);
97+
}
98+
return result;
99+
}
100+
101+
} // namespace parser
102+
} // namespace olp

olp-cpp-sdk-dataservice-read/src/generated/parser/PartitionsParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#include <vector>
2323

24-
#include <olp/core/generated/parser/ParserWrapper.h>
24+
#include <generated/parser/ParserWrapper.h>
2525

2626
namespace olp {
2727
namespace parser {

olp-cpp-sdk-dataservice-read/src/generated/parser/StreamOffsetParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#include "StreamOffsetParser.h"
2121

22-
#include <olp/core/generated/parser/ParserWrapper.h>
22+
#include <generated/parser/ParserWrapper.h>
2323

2424
namespace olp {
2525
namespace parser {

0 commit comments

Comments
 (0)