20
20
// / for the different sub trees.
21
21
// / \param include_comments: when writing JSON, should the comments
22
22
// / sub tree be included.
23
- json_irept::json_irept (bool include_comments):
24
- include_comments(include_comments)
25
- {}
23
+ json_irept::json_irept (bool _include_comments):
24
+ include_comments(_include_comments)
25
+ {
26
+ }
26
27
27
28
// / To convert to JSON from an irep structure by recursively generating JSON
28
29
// / for the different sub trees.
29
30
// / \param irep: The irep structure to turn into json
30
- // / \param json : The json object to be filled up .
31
- void json_irept::convert_from_irep (const irept &irep, jsont &json ) const
31
+ // / \return : The json object.
32
+ json_objectt json_irept::convert_from_irep (const irept &irep) const
32
33
{
33
- json_objectt &irep_object=json.make_object ();
34
+ json_objectt irep_object;
35
+
34
36
if (irep.id ()!=ID_nil)
35
37
irep_object[" id" ]=json_stringt (irep.id_string ());
36
38
37
39
convert_sub_tree (" sub" , irep.get_sub (), irep_object);
38
40
convert_named_sub_tree (" namedSub" , irep.get_named_sub (), irep_object);
41
+
39
42
if (include_comments)
40
- {
41
43
convert_named_sub_tree (" comment" , irep.get_comments (), irep_object);
42
- }
44
+
45
+ return irep_object;
43
46
}
44
47
45
48
// / To convert to JSON from a list of ireps that are in an unlabelled subtree.
@@ -48,6 +51,7 @@ void json_irept::convert_from_irep(const irept &irep, jsont &json) const
48
51
// / \param sub_tree_id: the name to give the subtree in the parent object
49
52
// / \param sub_trees: the list of subtrees to parse
50
53
// / \param parent: the parent JSON object who should be added to
54
+
51
55
void json_irept::convert_sub_tree (
52
56
const std::string &sub_tree_id,
53
57
const irept::subt &sub_trees,
@@ -58,8 +62,7 @@ void json_irept::convert_sub_tree(
58
62
json_arrayt sub_objects;
59
63
for (const irept &sub_tree : sub_trees)
60
64
{
61
- json_objectt sub_object;
62
- convert_from_irep (sub_tree, sub_object);
65
+ json_objectt sub_object=convert_from_irep (sub_tree);
63
66
sub_objects.push_back (sub_object);
64
67
}
65
68
parent[sub_tree_id]=sub_objects;
@@ -83,8 +86,7 @@ void json_irept::convert_named_sub_tree(
83
86
json_objectt sub_objects;
84
87
for (const auto &sub_tree : sub_trees)
85
88
{
86
- json_objectt sub_object;
87
- convert_from_irep (sub_tree.second , sub_object);
89
+ json_objectt sub_object=convert_from_irep (sub_tree.second );
88
90
sub_objects[id2string (sub_tree.first )]=sub_object;
89
91
}
90
92
parent[sub_tree_id]=sub_objects;
@@ -94,7 +96,7 @@ void json_irept::convert_named_sub_tree(
94
96
// / Deserialize a JSON irep representation.
95
97
// / \param input: json object to convert
96
98
// / \return result - irep equivalent of input
97
- void json_irept::convert_from_json (const jsont &in, irept &out ) const
99
+ irept json_irept::convert_from_json (const jsont &in) const
98
100
{
99
101
std::vector<std::string> have_keys;
100
102
for (const auto &keyval : in.object )
@@ -104,17 +106,16 @@ void json_irept::convert_from_json(const jsont &in, irept &out) const
104
106
throw " irep JSON representation is missing one of needed keys: "
105
107
" 'id', 'sub', 'namedSub', 'comment'" ;
106
108
107
- out. id (in[" id" ].value );
109
+ irept out (in[" id" ].value );
108
110
109
111
for (const auto &sub : in[" sub" ].array )
110
- {
111
- out.get_sub ().push_back (irept ());
112
- convert_from_json (sub, out.get_sub ().back ());
113
- }
112
+ out.get_sub ().push_back (convert_from_json (sub));
114
113
115
114
for (const auto &named_sub : in[" namedSub" ].object )
116
- convert_from_json (named_sub. second , out.add (named_sub.first ));
115
+ out.add (named_sub.first )= convert_from_json (named_sub. second );
117
116
118
117
for (const auto &comment : in[" comment" ].object )
119
- convert_from_json (comment.second , out.add (comment.first ));
118
+ out.add (comment.first )=convert_from_json (comment.second );
119
+
120
+ return out;
120
121
}
0 commit comments