Skip to content

Commit cf7441c

Browse files
committed
Reintroduce table name for links in xjson_plus format
1 parent caf04ac commit cf7441c

File tree

5 files changed

+88
-36
lines changed

5 files changed

+88
-36
lines changed

src/realm/to_json.cpp

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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()) {

test/expected_xjson_plus_link.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
{
33
"primaryKey": "t1o1",
44
"str1": "hello",
5-
"linkA": "t2o1"
5+
"linkA": {
6+
"$link": {
7+
"table": "table2",
8+
"key": "t2o1"
9+
}
10+
}
611
},
712
{
813
"primaryKey": "t1o2",

test/expected_xjson_plus_linkdict1.json

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
},
77
"dict": {
88
"$dictionary": {
9-
"key1": "t2o2"
9+
"key1": {
10+
"$link": {
11+
"table": "table2",
12+
"key": "t2o2"
13+
}
14+
}
1015
}
1116
}
1217
},
@@ -26,8 +31,18 @@
2631
},
2732
"dict": {
2833
"$dictionary": {
29-
"key2": "t2o2",
30-
"key3": "t2o3",
34+
"key2": {
35+
"$link": {
36+
"table": "table2",
37+
"key": "t2o2"
38+
}
39+
},
40+
"key3": {
41+
"$link": {
42+
"table": "table2",
43+
"key": "t2o3"
44+
}
45+
},
3146
"key4": null,
3247
"key5": null
3348
}

test/expected_xjson_plus_linklist1.json

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@
55
"$numberLong": "100"
66
},
77
"linkA": [
8-
"t2o2"
8+
{
9+
"$link": {
10+
"table": "table2",
11+
"key": "t2o2"
12+
}
13+
}
914
],
10-
"linkB": "t2o1"
15+
"linkB": {
16+
"$link": {
17+
"table": "table2",
18+
"key": "t2o1"
19+
}
20+
}
1121
},
1222
{
1323
"primaryKey": "t1o3",
@@ -23,8 +33,18 @@
2333
"$numberLong": "200"
2434
},
2535
"linkA": [
26-
"t2o2",
27-
"t2o3"
36+
{
37+
"$link": {
38+
"table": "table2",
39+
"key": "t2o2"
40+
}
41+
},
42+
{
43+
"$link": {
44+
"table": "table2",
45+
"key": "t2o3"
46+
}
47+
}
2848
],
2949
"linkB": null
3050
}

test/expected_xjson_plus_linkset1.json

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
"$numberLong": "100"
66
},
77
"linkA": [
8-
"t2o2"
8+
{
9+
"$link": {
10+
"table": "table2",
11+
"key": "t2o2"
12+
}
13+
}
914
]
1015
},
1116
{
@@ -21,8 +26,18 @@
2126
"$numberLong": "200"
2227
},
2328
"linkA": [
24-
"t2o2",
25-
"t2o3"
29+
{
30+
"$link": {
31+
"table": "table2",
32+
"key": "t2o2"
33+
}
34+
},
35+
{
36+
"$link": {
37+
"table": "table2",
38+
"key": "t2o3"
39+
}
40+
}
2641
]
2742
}
2843
]

0 commit comments

Comments
 (0)