Skip to content

Commit ce05270

Browse files
committed
Add support for rapidjson as the JSON library
nlohmann is still the default, but if `CPPDAP_JSON_DIR` points to rapidjson, then this will be used instead Most of this was upstreamed from: https://fuchsia-review.googlesource.com/c/third_party/github.com/google/cppdap/+/456566
1 parent 1fd23dd commit ce05270

13 files changed

+690
-177
lines changed

CMakeLists.txt

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,34 @@ if(CPPDAP_BUILD_TESTS)
6464
endif()
6565
endif(CPPDAP_BUILD_TESTS)
6666

67+
###########################################################
68+
# JSON library
69+
###########################################################
70+
if(NOT DEFINED CPPDAP_JSON_LIBRARY)
71+
# Attempt to detect JSON library from CPPDAP_JSON_DIR
72+
if(NOT EXISTS "${CPPDAP_JSON_DIR}")
73+
message(FATAL_ERROR "CPPDAP_JSON_DIR '${CPPDAP_JSON_DIR}' does not exist")
74+
endif()
75+
76+
if(EXISTS "${CPPDAP_JSON_DIR}/include/nlohmann")
77+
set(CPPDAP_JSON_LIBRARY "nlohmann")
78+
elseif(EXISTS "${CPPDAP_JSON_DIR}/include/rapidjson")
79+
set(CPPDAP_JSON_LIBRARY "rapid")
80+
else()
81+
message(FATAL_ERROR "Could not determine JSON library from ${CPPDAP_JSON_LIBRARY}")
82+
endif()
83+
endif()
84+
string(TOUPPER ${CPPDAP_JSON_LIBRARY} CPPDAP_JSON_LIBRARY_UPPER)
85+
6786
###########################################################
6887
# File lists
6988
###########################################################
7089
set(CPPDAP_LIST
7190
${CPPDAP_SRC_DIR}/content_stream.cpp
7291
${CPPDAP_SRC_DIR}/io.cpp
73-
${CPPDAP_SRC_DIR}/json_serializer.cpp
92+
${CPPDAP_SRC_DIR}/${CPPDAP_JSON_LIBRARY}_json_serializer.cpp
7493
${CPPDAP_SRC_DIR}/network.cpp
94+
${CPPDAP_SRC_DIR}/null_json_serializer.cpp
7595
${CPPDAP_SRC_DIR}/protocol_events.cpp
7696
${CPPDAP_SRC_DIR}/protocol_requests.cpp
7797
${CPPDAP_SRC_DIR}/protocol_response.cpp
@@ -117,6 +137,11 @@ function(cppdap_set_target_options target)
117137
)
118138
endif()
119139

140+
# Add define for JSON library in use
141+
set_target_properties(${target} PROPERTIES
142+
COMPILE_DEFINITIONS "CPPDAP_JSON_${CPPDAP_JSON_LIBRARY_UPPER}=1"
143+
)
144+
120145
# Treat all warnings as errors
121146
if(CPPDAP_WARNINGS_AS_ERRORS)
122147
if(MSVC)
@@ -151,9 +176,7 @@ endfunction(cppdap_set_target_options)
151176

152177
# dap
153178
add_library(cppdap STATIC ${CPPDAP_LIST})
154-
set_target_properties(cppdap PROPERTIES
155-
POSITION_INDEPENDENT_CODE 1
156-
)
179+
set_target_properties(cppdap PROPERTIES POSITION_INDEPENDENT_CODE 1)
157180

158181
target_include_directories(cppdap PRIVATE "${CPPDAP_JSON_DIR}/include/")
159182

src/chan.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,4 @@ void Chan<T>::put(const T& in) {
8787

8888
} // namespace dap
8989

90-
#endif // dap_chan_h
90+
#endif // dap_chan_h

src/content_stream.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,4 @@ bool ContentWriter::write(const std::string& msg) const {
177177
writer->write(msg.data(), msg.size());
178178
}
179179

180-
} // namespace dap
180+
} // namespace dap

src/io.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class File : public dap::ReaderWriter {
145145
const bool closable;
146146
std::mutex readMutex;
147147
std::mutex writeMutex;
148-
std::atomic<bool> closed = { false };
148+
std::atomic<bool> closed = {false};
149149
};
150150

151151
class ReaderSpy : public dap::Reader {
@@ -254,4 +254,4 @@ bool writef(const std::shared_ptr<Writer>& w, const char* msg, ...) {
254254
return w->write(buf, strlen(buf));
255255
}
256256

257-
} // namespace dap
257+
} // namespace dap

src/json_serializer.h

Lines changed: 18 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019 Google LLC
1+
// Copyright 2020 Google LLC
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -15,119 +15,28 @@
1515
#ifndef dap_json_serializer_h
1616
#define dap_json_serializer_h
1717

18-
#include "dap/protocol.h"
19-
#include "dap/serialization.h"
20-
#include "dap/types.h"
21-
22-
#include <nlohmann/json_fwd.hpp>
18+
#if defined(CPPDAP_JSON_NLOHMANN)
19+
#include "nlohmann_json_serializer.h"
20+
#elif defined(CPPDAP_JSON_RAPID)
21+
#include "rapid_json_serializer.h"
22+
#else
23+
#error "Unrecognised cppdap JSON library"
24+
#endif
2325

2426
namespace dap {
2527
namespace json {
2628

27-
struct Deserializer : public dap::Deserializer {
28-
explicit Deserializer(const std::string&);
29-
~Deserializer();
30-
31-
// dap::Deserializer compliance
32-
bool deserialize(boolean* v) const override;
33-
bool deserialize(integer* v) const override;
34-
bool deserialize(number* v) const override;
35-
bool deserialize(string* v) const override;
36-
bool deserialize(object* v) const override;
37-
bool deserialize(any* v) const override;
38-
size_t count() const override;
39-
bool array(const std::function<bool(dap::Deserializer*)>&) const override;
40-
bool field(const std::string& name,
41-
const std::function<bool(dap::Deserializer*)>&) const override;
42-
43-
// Unhide base overloads
44-
template <typename T>
45-
inline bool field(const std::string& name, T* v) {
46-
return dap::Deserializer::field(name, v);
47-
}
48-
49-
template <typename T,
50-
typename = std::enable_if<TypeOf<T>::has_custom_serialization>>
51-
inline bool deserialize(T* v) const {
52-
return dap::Deserializer::deserialize(v);
53-
}
54-
55-
template <typename T>
56-
inline bool deserialize(dap::array<T>* v) const {
57-
return dap::Deserializer::deserialize(v);
58-
}
59-
60-
template <typename T>
61-
inline bool deserialize(dap::optional<T>* v) const {
62-
return dap::Deserializer::deserialize(v);
63-
}
64-
65-
template <typename T0, typename... Types>
66-
inline bool deserialize(dap::variant<T0, Types...>* v) const {
67-
return dap::Deserializer::deserialize(v);
68-
}
69-
70-
template <typename T>
71-
inline bool field(const std::string& name, T* v) const {
72-
return dap::Deserializer::deserialize(name, v);
73-
}
74-
75-
private:
76-
Deserializer(const nlohmann::json*);
77-
const nlohmann::json* const json;
78-
const bool ownsJson;
79-
};
80-
81-
struct Serializer : public dap::Serializer {
82-
Serializer();
83-
~Serializer();
84-
85-
std::string dump() const;
86-
87-
// dap::Serializer compliance
88-
bool serialize(boolean v) override;
89-
bool serialize(integer v) override;
90-
bool serialize(number v) override;
91-
bool serialize(const string& v) override;
92-
bool serialize(const dap::object& v) override;
93-
bool serialize(const any& v) override;
94-
bool array(size_t count,
95-
const std::function<bool(dap::Serializer*)>&) override;
96-
bool object(const std::function<bool(dap::FieldSerializer*)>&) override;
97-
void remove() override;
98-
99-
// Unhide base overloads
100-
template <typename T,
101-
typename = std::enable_if<TypeOf<T>::has_custom_serialization>>
102-
inline bool serialize(const T& v) {
103-
return dap::Serializer::serialize(v);
104-
}
105-
106-
template <typename T>
107-
inline bool serialize(const dap::array<T>& v) {
108-
return dap::Serializer::serialize(v);
109-
}
110-
111-
template <typename T>
112-
inline bool serialize(const dap::optional<T>& v) {
113-
return dap::Serializer::serialize(v);
114-
}
115-
116-
template <typename T0, typename... Types>
117-
inline bool serialize(const dap::variant<T0, Types...>& v) {
118-
return dap::Serializer::serialize(v);
119-
}
120-
121-
inline bool serialize(const char* v) { return dap::Serializer::serialize(v); }
122-
123-
private:
124-
Serializer(nlohmann::json*);
125-
nlohmann::json* const json;
126-
const bool ownsJson;
127-
bool removed = false;
128-
};
29+
#if defined(CPPDAP_JSON_NLOHMANN)
30+
using Deserializer = NlohmannDeserializer;
31+
using Serializer = NlohmannSerializer;
32+
#elif defined(CPPDAP_JSON_RAPID)
33+
using Deserializer = RapidDeserializer;
34+
using Serializer = RapidSerializer;
35+
#else
36+
#error "Unrecognised cppdap JSON library"
37+
#endif
12938

13039
} // namespace json
13140
} // namespace dap
13241

133-
#endif // dap_json_serializer_h
42+
#endif // dap_json_serializer_h

0 commit comments

Comments
 (0)