@@ -252,41 +252,38 @@ void Obj::to_json(std::ostream& out, JSONOutputMode output_mode) const
252252 auto print_link = [&](const Mixed& val) {
253253 REALM_ASSERT (val.is_type (type_Link, type_TypedLink));
254254 TableRef tt = target_table;
255- auto obj_key = val.get <ObjKey>();
256-
257- auto out_obj_key = [&] {
258- if (pk_col_key) {
259- tt->get_primary_key (obj_key).to_json (out, output_mode);
260- }
261- else {
262- out << obj_key.value ;
263- }
264- };
255+ bool typed_link = false ;
265256 if (!tt) {
266257 // It must be a typed link
267258 tt = m_table->get_parent_group ()->get_table (val.get_link ().get_table_key ());
268- pk_col_key = tt->get_primary_key_column ();
269- out << " { \" $link\" : {" ;
259+ typed_link = true ;
260+ }
261+ auto obj_key = val.get <ObjKey>();
262+ std::string closing;
270263
271- out << " \" table\" : \" " << tt->get_class_name () << " \" , \" key\" : " ;
272- out_obj_key ();
273- out << " }}" ;
264+ if (tt->is_embedded ()) {
265+ if (output_mode == output_mode_xjson_plus) {
266+ out << " { \" $embedded\" : {" ;
267+ out << " \" table\" : \" " << tt->get_name () << " \" , \" value\" : " ;
268+ closing = " }}" ;
269+ }
270+ tt->get_object (obj_key).to_json (out, output_mode);
274271 }
275272 else {
276- if (tt->is_embedded ()) {
277- if (output_mode == output_mode_xjson_plus) {
278- out << " { \" $embedded\" : {" ;
279- out << " \" table\" : \" " << tt->get_name () << " \" , \" value\" : " ;
280- }
281- tt->get_object (obj_key).to_json (out, output_mode);
282- if (output_mode == output_mode_xjson_plus) {
283- out << " }}" ;
284- }
273+ pk_col_key = tt->get_primary_key_column ();
274+ if (output_mode == output_mode_xjson_plus || typed_link) {
275+ out << " { \" $link\" : {" ;
276+ out << " \" table\" : \" " << tt->get_class_name () << " \" , \" key\" : " ;
277+ closing = " }}" ;
278+ }
279+ if (pk_col_key) {
280+ tt->get_primary_key (obj_key).to_json (out, output_mode);
285281 }
286282 else {
287- out_obj_key () ;
283+ out << obj_key. value ;
288284 }
289285 }
286+ out << closing;
290287 };
291288
292289 if (ck.is_collection ()) {
0 commit comments