@@ -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
0 commit comments