Skip to content

Commit 5fa1b6f

Browse files
committed
Fix the incompatibility of JSON serialization for higher version Boost
1 parent 85d7650 commit 5fa1b6f

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed

lib/HTTPLookupService.cc

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "CurlWrapper.h"
2727
#include "ExecutorService.h"
2828
#include "Int64SerDes.h"
29+
#include "JsonUtils.h"
2930
#include "LogUtils.h"
3031
#include "NamespaceName.h"
3132
#include "SchemaUtils.h"
@@ -409,15 +410,8 @@ void HTTPLookupService::handleGetSchemaHTTPRequest(GetSchemaPromise promise, con
409410
promise.setFailed(ResultInvalidMessage);
410411
return;
411412
}
412-
std::stringstream keyStream;
413-
ptree::write_json(keyStream, kvRoot.get_child("key"), false);
414-
std::stringstream valueStream;
415-
ptree::write_json(valueStream, kvRoot.get_child("value"), false);
416-
auto keyData = keyStream.str();
417-
auto valueData = valueStream.str();
418-
// Remove the last line break.
419-
keyData.pop_back();
420-
valueData.pop_back();
413+
const auto keyData = toJson(kvRoot.get_child("key"));
414+
const auto valueData = toJson(kvRoot.get_child("value"));
421415
schemaData = mergeKeyValueSchema(keyData, valueData);
422416
}
423417

lib/JsonUtils.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
#pragma once
20+
21+
#include <boost/property_tree/json_parser.hpp>
22+
#include <sstream>
23+
#include <string>
24+
25+
namespace pulsar {
26+
27+
template <typename Ptree>
28+
inline std::string toJson(const Ptree& pt) {
29+
std::ostringstream oss;
30+
boost::property_tree::write_json(oss, pt, false);
31+
// For Boost < 1.86, boost::property_tree will write a endline at the end
32+
#if BOOST_VERSION < 108600
33+
auto s = oss.str();
34+
s.pop_back();
35+
return s;
36+
#else
37+
return oss.str();
38+
#endif
39+
}
40+
41+
} // namespace pulsar

lib/Schema.cc

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@
2525
#include <map>
2626
#include <memory>
2727

28+
#include "JsonUtils.h"
2829
#include "SchemaUtils.h"
2930

3031
using boost::property_tree::ptree;
31-
using boost::property_tree::read_json;
32-
using boost::property_tree::write_json;
3332

3433
PULSAR_PUBLIC std::ostream &operator<<(std::ostream &s, pulsar::SchemaType schemaType) {
3534
return s << strSchemaType(schemaType);
@@ -170,11 +169,7 @@ SchemaInfo::SchemaInfo(const SchemaInfo &keySchema, const SchemaInfo &valueSchem
170169
for (auto &entry : properties) {
171170
pt.put(entry.first, entry.second);
172171
}
173-
std::ostringstream buf;
174-
write_json(buf, pt, false);
175-
auto s = buf.str();
176-
s.pop_back();
177-
return s;
172+
return toJson(pt);
178173
};
179174

180175
StringMap properties;

0 commit comments

Comments
 (0)