Skip to content

Commit ce703d0

Browse files
committed
Convert to use nlohmann::json instead of jsoncpp.
1 parent c090bad commit ce703d0

29 files changed

+27204
-8627
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ endif()
227227

228228
if(ENABLE_MATHEMATICA)
229229
print_header("Configuring Mathematica")
230+
cmake_policy(SET CMP0077 NEW)
230231
set(Mathematica_USE_STATIC_LIBRARIES TRUE)
231232
find_package(Mathematica COMPONENTS WSTP)
232233
endif()

client_server/CMakeLists.txt

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ find_package(GLIBMM REQUIRED)
1919
find_package(SQLite3 REQUIRED)
2020
find_package(Threads REQUIRED)
2121
find_package(Boost 1.53.0 COMPONENTS system program_options date_time filesystem REQUIRED)
22-
if(ENABLE_SYSTEM_JSONCPP)
23-
message("-- Linking against system-provided jsoncpp")
24-
find_package(JSONCPP REQUIRED)
25-
else()
26-
message("-- Linking against included jsoncpp")
27-
endif()
2822

2923
#---------------------------------------------------------------------------
3024
# Enumerate input files.
@@ -57,15 +51,6 @@ set(CADABRA_CLIENT_SRC
5751
${CADABRA_LIBS_DIR}/whereami/whereami.c
5852
)
5953

60-
if(NOT ENABLE_SYSTEM_JSONCPP)
61-
list(APPEND CADABRA_SERVER_SRC
62-
${CADABRA_LIBS_DIR}/jsoncpp/jsoncpp.cpp
63-
)
64-
list(APPEND CADABRA_CLIENT_SRC
65-
${CADABRA_LIBS_DIR}/jsoncpp/jsoncpp.cpp
66-
)
67-
endif()
68-
6954
set(JUPYTER_KERNEL_SRC
7055
cadabra-jupyter-kernel.cc
7156
cadabra-jupyter-kernel.hh
@@ -75,7 +60,6 @@ set(JUPYTER_KERNEL_SRC
7560
${CADABRA_CORE_DIR}/DataCell.cc
7661
${CADABRA_CORE_DIR}/CdbPython.cc
7762
${CADABRA_CORE_DIR}/Stopwatch.cc
78-
${CADABRA_LIBS_DIR}/jsoncpp/jsoncpp.cpp
7963
${CADABRA_LIBS_DIR}/whereami/whereami.c
8064
${CADABRA_LIBS_DIR}/base64/base64.cc
8165
)
@@ -102,20 +86,12 @@ include_directories(
10286
"${CADABRA_LIBS_DIR}/websocketpp"
10387
"${CADABRA_LIBS_DIR}/internal/include"
10488
"${CADABRA_LIBS_DIR}/whereami"
105-
"${CADABRA_LIBS_DIR}/base64"
89+
"${CADABRA_LIBS_DIR}/base64"
90+
"${CADABRA_LIBS_DIR}/nlohmann"
10691
${Boost_INCLUDE_DIRS}
10792
${PYTHON_INCLUDE_DIRS}
10893
${SQLITE3_INCLUDE_DIR}
10994
)
110-
if(ENABLE_SYSTEM_JSONCPP)
111-
include_directories(
112-
${JSONCPP_INCLUDE_DIRS}
113-
)
114-
else()
115-
include_directories(
116-
"${CADABRA_LIBS_DIR}/jsoncpp"
117-
)
118-
endif()
11995

12096
add_definitions(
12197
-D_WEBSOCKETPP_CPP11_STL_
@@ -148,11 +124,6 @@ target_link_libraries(cadabra-server
148124
${GLIBMM3_LIBRARIES}
149125
Threads::Threads
150126
)
151-
if(ENABLE_SYSTEM_JSONCPP)
152-
target_link_libraries(cadabra-server
153-
${JSONCPP_LIBRARIES}
154-
)
155-
endif()
156127
if(WIN32)
157128
target_link_libraries(cadabra-server ws2_32 mswsock bcrypt)
158129
endif()
@@ -173,11 +144,6 @@ target_link_libraries(cadabra_client
173144
${GLIBMM3_LIBRARIES}
174145
Threads::Threads
175146
)
176-
if(ENABLE_SYSTEM_JSONCPP)
177-
target_link_libraries(cadabra_client
178-
${JSONCPP_LIBRARIES}
179-
)
180-
endif()
181147
if(WIN32)
182148
target_link_libraries(cadabra_client ws2_32 bcrypt)
183149
endif()
@@ -208,9 +174,6 @@ if(ENABLE_JUPYTER)
208174
target_compile_definitions(cadabra-jupyter-kernel PRIVATE CDB_DONT_ACQUIRE_GIL=1)
209175
message("-- UUID library at ${UUID_LIBRARIES}")
210176
target_link_libraries(cadabra-jupyter-kernel xeus Threads::Threads uuid ${Boost_LIBRARIES} ${GLIBMM3_LIBRARIES} ${PYTHON_LIBRARIES})
211-
if(ENABLE_SYSTEM_JSONCPP)
212-
target_link_libraries(cadabra-jupyter-kernel ${JSONCPP_LIBRARIES})
213-
endif()
214177
else()
215178
message("-- Not building Xeus Jupyter kernel")
216179
endif()

client_server/ComputeThread.cc

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -277,25 +277,28 @@ void ComputeThread::on_message(websocketpp::connection_hdl hdl, message_ptr msg)
277277
// std::cerr << msg->get_payload() << std::endl;
278278

279279
// Parse the JSON message.
280-
Json::Value root;
281-
Json::Reader reader;
282-
bool success = reader.parse( msg->get_payload(), root );
283-
if ( !success ) {
280+
nlohmann::json root;
281+
282+
try {
283+
root=nlohmann::json::parse(msg->get_payload());
284+
}
285+
catch(nlohmann::json::exception& e) {
284286
std::cerr << "cadabra-client: cannot parse message" << std::endl;
285287
return;
286288
}
287-
const Json::Value header = root["header"];
288-
const Json::Value content = root["content"];
289-
const Json::Value msg_type = root["msg_type"];
289+
const nlohmann::json& header = root["header"];
290+
const nlohmann::json& content = root["content"];
291+
const std::string msg_type = root.value("msg_type", "");
292+
290293
DataCell::id_t parent_id;
291-
parent_id.id = header["parent_id"].asUInt64();
292-
if(header["parent_origin"].asString()=="client")
294+
parent_id.id = header["parent_id"].get<uint64_t>();
295+
if(header.value("parent_origin", "")=="client")
293296
parent_id.created_by_client=true;
294297
else
295298
parent_id.created_by_client=false;
296299
DataCell::id_t cell_id;
297-
cell_id.id = header["cell_id"].asUInt64();
298-
if(header["cell_origin"].asString()=="client")
300+
cell_id.id = header["cell_id"].get<uint64_t>();
301+
if(header["cell_origin"].get<std::string>()=="client")
299302
cell_id.created_by_client=true;
300303
else
301304
cell_id.created_by_client=false;
@@ -307,27 +310,27 @@ void ComputeThread::on_message(websocketpp::connection_hdl hdl, message_ptr msg)
307310
else if (cell_id.id == interactive_cell || std::find(console_child_ids.begin(), console_child_ids.end(), parent_id.id) != console_child_ids.end()) {
308311
docthread->on_interactive_output(root);
309312
}
310-
else if (msg_type.asString().find("csl_") == 0) {
313+
else if (msg_type.find("csl_") == 0) {
311314
root["header"]["from_server"] = true;
312315
docthread->on_interactive_output(root);
313316
}
314-
else if(msg_type.asString()=="completed") {
317+
else if(msg_type=="completed") {
315318
// std::cerr << "received completion of " << content["original"] << " -> " << content["completed"] << std::endl;
316319

317320
// Finally, the action to add the output cell.
318-
std::string toadd=content["completed"].asString();
321+
std::string toadd=content["completed"].get<std::string>();
319322
if(toadd.size()>0) {
320-
toadd=toadd.substr(content["original"].asString().size());
321-
int pos=content["position"].asInt();
322-
int alternative=content["alternative"].asInt();
323+
toadd=toadd.substr(content["original"].get<std::string>().size());
324+
int pos=content["position"].get<int>();
325+
int alternative=content["alternative"].get<int>();
323326
std::shared_ptr<ActionBase> action =
324327
std::make_shared<ActionCompleteText>(cell_id, pos, toadd, alternative);
325328
docthread->queue_action(action);
326329
}
327330
}
328331
else {
329332
try {
330-
bool finished = header["last_in_sequence"].asBool();
333+
bool finished = header["last_in_sequence"].get<bool>();
331334

332335
if (finished) {
333336
std::shared_ptr<ActionBase> rs_action =
@@ -336,9 +339,9 @@ void ComputeThread::on_message(websocketpp::connection_hdl hdl, message_ptr msg)
336339
cell_finished_running(parent_id);
337340
}
338341

339-
if (content["output"].asString().size() > 0) {
340-
if (msg_type.asString() == "output") {
341-
std::string output = "\\begin{verbatim}" + content["output"].asString() + "\\end{verbatim}";
342+
if (content.count("output")>0 && content["output"].get<std::string>().size() > 0) {
343+
if (msg_type == "output") {
344+
std::string output = "\\begin{verbatim}" + content["output"].get<std::string>() + "\\end{verbatim}";
342345

343346
// Stick an AddCell action onto the stack. We instruct the
344347
// action to add this result output cell as a child of the
@@ -350,8 +353,8 @@ void ComputeThread::on_message(websocketpp::connection_hdl hdl, message_ptr msg)
350353
std::make_shared<ActionAddCell>(result, parent_id, ActionAddCell::Position::child);
351354
docthread->queue_action(action);
352355
}
353-
else if (msg_type.asString() == "verbatim") {
354-
std::string output = "\\begin{verbatim}" + content["output"].asString() + "\\end{verbatim}";
356+
else if (msg_type == "verbatim") {
357+
std::string output = "\\begin{verbatim}" + content["output"].get<std::string>() + "\\end{verbatim}";
355358

356359
// Stick an AddCell action onto the stack. We instruct the
357360
// action to add this result output cell as a child of the
@@ -363,23 +366,23 @@ void ComputeThread::on_message(websocketpp::connection_hdl hdl, message_ptr msg)
363366
std::make_shared<ActionAddCell>(result, parent_id, ActionAddCell::Position::child);
364367
docthread->queue_action(action);
365368
}
366-
else if (msg_type.asString() == "latex_view") {
369+
else if (msg_type == "latex_view") {
367370
// std::cerr << "received latex cell " << content["output"].asString() << std::endl;
368-
DataCell result(cell_id, DataCell::CellType::latex_view, content["output"].asString());
371+
DataCell result(cell_id, DataCell::CellType::latex_view, content["output"].get<std::string>());
369372
std::shared_ptr<ActionBase> action =
370373
std::make_shared<ActionAddCell>(result, parent_id, ActionAddCell::Position::child);
371374
docthread->queue_action(action);
372375
}
373-
else if (msg_type.asString() == "input_form") {
374-
DataCell result(cell_id, DataCell::CellType::input_form, content["output"].asString());
376+
else if (msg_type == "input_form") {
377+
DataCell result(cell_id, DataCell::CellType::input_form, content["output"].get<std::string>());
375378
std::shared_ptr<ActionBase> action =
376379
std::make_shared<ActionAddCell>(result, parent_id, ActionAddCell::Position::child);
377380
docthread->queue_action(action);
378381
}
379-
else if (msg_type.asString() == "error") {
380-
std::string error = "{\\color{red}{\\begin{verbatim}" + content["output"].asString()
382+
else if (msg_type == "error") {
383+
std::string error = "{\\color{red}{\\begin{verbatim}" + content["output"].get<std::string>()
381384
+ "\\end{verbatim}}}";
382-
if (msg_type.asString() == "fault") {
385+
if (msg_type == "fault") {
383386
error = "{\\color{red}{Kernel fault}}\\begin{small}" + error + "\\end{small}";
384387
}
385388

@@ -401,15 +404,15 @@ void ComputeThread::on_message(websocketpp::connection_hdl hdl, message_ptr msg)
401404

402405
// FIXME: iterate over all cells and set the running flag to false.
403406
}
404-
else if (msg_type.asString() == "image_png") {
405-
DataCell result(cell_id, DataCell::CellType::image_png, content["output"].asString());
407+
else if (msg_type == "image_png") {
408+
DataCell result(cell_id, DataCell::CellType::image_png, content["output"].get<std::string>());
406409
std::shared_ptr<ActionBase> action =
407410
std::make_shared<ActionAddCell>(result, parent_id, ActionAddCell::Position::child);
408411
docthread->queue_action(action);
409412
}
410413
else {
411414
std::cerr << "cadabra-client: received cell we did not expect: "
412-
<< msg_type.asString() << std::endl;
415+
<< msg_type << std::endl;
413416
}
414417
}
415418
}
@@ -438,12 +441,12 @@ void ComputeThread::execute_interactive(const std::string& code)
438441
if (code.substr(0, 7) == "reset()")
439442
return restart_kernel();
440443

441-
Json::Value req, header, content;
444+
nlohmann::json req, header, content;
442445

443-
header["msg_type"] = "execute_request";
444-
header["cell_id"] = static_cast<Json::UInt64>(interactive_cell);
446+
header["msg_type"] = "execute_request";
447+
header["cell_id"] = interactive_cell;
445448
header["interactive"] = true;
446-
content["code"] = code.c_str();
449+
content["code"] = code.c_str();
447450

448451
req["auth_token"] = authentication_token;
449452
req["header"] = header;
@@ -498,9 +501,9 @@ void ComputeThread::execute_cell(DTree::iterator it)
498501
std::make_shared<ActionSetRunStatus>(it->id(), true);
499502
docthread->queue_action(rs_action);
500503

501-
Json::Value req, header, content;
504+
nlohmann::json req, header, content;
502505
header["uuid"]="none";
503-
header["cell_id"]=(Json::UInt64)dc.id().id;
506+
header["cell_id"]=dc.id().id;
504507
if(dc.id().created_by_client)
505508
header["cell_origin"]="client";
506509
else
@@ -542,7 +545,7 @@ void ComputeThread::stop()
542545
if(connection_is_open==false)
543546
return;
544547

545-
Json::Value req, header, content;
548+
nlohmann::json req, header, content;
546549
header["uuid"]="none";
547550
header["msg_type"]="execute_interrupt";
548551
req["auth_token"]=authentication_token;
@@ -570,7 +573,7 @@ void ComputeThread::restart_kernel()
570573
gui->on_kernel_runstatus(false);
571574

572575
// std::cerr << "cadabra-client: restarting kernel" << std::endl;
573-
Json::Value req, header, content;
576+
nlohmann::json req, header, content;
574577
header["uuid"]="none";
575578
header["msg_type"]="exit";
576579
header["from_server"] = true;
@@ -593,9 +596,9 @@ bool ComputeThread::complete(DTree::iterator it, int pos, int alternative)
593596

594597
const DataCell& dc=(*it);
595598

596-
Json::Value req, header, content;
599+
nlohmann::json req, header, content;
597600
header["uuid"]="none";
598-
header["cell_id"]=(Json::UInt64)dc.id().id;
601+
header["cell_id"]=dc.id().id;
599602
if(dc.id().created_by_client)
600603
header["cell_origin"]="client";
601604
else

client_server/DocumentThread.cc

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
#include <internal/unistd.h>
1515
#include <sys/types.h>
16-
#include <json/json.h>
1716
#ifndef EMSCRIPTEN
1817
#include <glibmm/miscutils.h>
1918
#include "Snoop.hh"
@@ -35,7 +34,7 @@ DocumentThread::DocumentThread(GUIBase* g)
3534

3635
}
3736

38-
void DocumentThread::on_interactive_output(const Json::Value& )
37+
void DocumentThread::on_interactive_output(const nlohmann::json& )
3938
{
4039

4140
}
@@ -219,36 +218,36 @@ DocumentThread::Prefs::Prefs(bool use_defaults)
219218
}
220219
}
221220
}
222-
font_step = data.get("font_step", 0).asInt();
223-
highlight = data.get("highlight", false).asBool();
224-
is_registered = data.get("is_registered", false).asBool();
225-
is_anonymous = data.get("is_anonymous", false).asBool();
226-
git_path = data.get("git_path", "").asString();
227-
python_path = data.get("python_path", "").asString();
228-
move_into_new_cell = data.get("move_into_new_cell", false).asBool();
229-
tab_completion = data.get("tab_completion", true).asBool();
221+
font_step = data.value("font_step", 0);
222+
highlight = data.value("highlight", false);
223+
is_registered = data.value("is_registered", false);
224+
is_anonymous = data.value("is_anonymous", false);
225+
git_path = data.value("git_path", "");
226+
python_path = data.value("python_path", "");
227+
move_into_new_cell = data.value("move_into_new_cell", false);
228+
tab_completion = data.value("tab_completion", true);
230229

231230
if(git_path=="")
232231
git_path="/usr/bin/git";
233232
// Get the colours for syntax highlighting.
234-
auto python_colours = data.get("colours", Json::Value()).get("python", Json::Value());
235-
colours["python"]["keyword"] = (python_colours.get("keyword", "RoyalBlue").asString());
236-
colours["python"]["operator"] = (python_colours.get("operator", "SlateGray").asString());
237-
colours["python"]["brace"] = (python_colours.get("brace", "SlateGray").asString());
238-
colours["python"]["string"] = (python_colours.get("string", "ForestGreen").asString());
239-
colours["python"]["comment"] = (python_colours.get("comment", "Silver").asString());
240-
colours["python"]["object"] = (python_colours.get("object", "DarkGray").asString());
241-
colours["python"]["number"] = (python_colours.get("number", "Sienna").asString());
242-
colours["python"]["maths"] = (python_colours.get("maths", "Olive").asString());
243-
colours["python"]["function"] = (python_colours.get("function", "FireBrick").asString());
244-
colours["python"]["decorator"] = (python_colours.get("decorator", "DarkViolet").asString());
245-
colours["python"]["class"] = (python_colours.get("class", "MediumOrchid").asString());
246-
247-
auto latex_colours = data.get("colours", Json::Value()).get("latex", Json::Value());
248-
colours["latex"]["command"] = (latex_colours.get("command", "rgb(52,101,164)").asString());
249-
colours["latex"]["parameter"] = (latex_colours.get("brace", "rgb(245,121,0)").asString());
250-
colours["latex"]["comment"] = (latex_colours.get("comment", "Silver").asString());
251-
colours["latex"]["maths"] = (latex_colours.get("maths", "Sienna").asString());
233+
auto python_colours = data["colours"]["python"];
234+
colours["python"]["keyword"] = python_colours.value("keyword", "RoyalBlue");
235+
colours["python"]["operator"] = python_colours.value("operator", "SlateGray");
236+
colours["python"]["brace"] = python_colours.value("brace", "SlateGray");
237+
colours["python"]["string"] = python_colours.value("string", "ForestGreen");
238+
colours["python"]["comment"] = python_colours.value("comment", "Silver");
239+
colours["python"]["object"] = python_colours.value("object", "DarkGray");
240+
colours["python"]["number"] = python_colours.value("number", "Sienna");
241+
colours["python"]["maths"] = python_colours.value("maths", "Olive");
242+
colours["python"]["function"] = python_colours.value("function", "FireBrick");
243+
colours["python"]["decorator"] = python_colours.value("decorator", "DarkViolet");
244+
colours["python"]["class"] = python_colours.value("class", "MediumOrchid");
245+
246+
auto latex_colours = data["colours"]["latex"];
247+
colours["latex"]["command"] = latex_colours.value("command", "rgb(52,101,164)");
248+
colours["latex"]["parameter"] = latex_colours.value("brace", "rgb(245,121,0)");
249+
colours["latex"]["comment"] = latex_colours.value("comment", "Silver");
250+
colours["latex"]["maths"] = latex_colours.value("maths", "Sienna");
252251
#endif
253252
}
254253

0 commit comments

Comments
 (0)