|
10 | 10 | #include "json.h"
|
11 | 11 | #include "json_irep.h"
|
12 | 12 |
|
| 13 | +#include <algorithm> |
| 14 | + |
13 | 15 | /*******************************************************************\
|
14 | 16 |
|
15 | 17 | Function: json_irept::json_irept
|
16 | 18 |
|
17 | 19 | Inputs:
|
18 |
| - include_comments - when generating the JSON, should the comments |
| 20 | + include_comments - when writing JSON, should the comments |
19 | 21 | sub tree be included.
|
20 | 22 |
|
21 | 23 | Outputs:
|
@@ -131,3 +133,39 @@ void json_irept::convert_named_sub_tree(
|
131 | 133 | }
|
132 | 134 | }
|
133 | 135 |
|
| 136 | +/*******************************************************************\ |
| 137 | +
|
| 138 | +Function: json_irept::convert_from_json |
| 139 | +
|
| 140 | + Inputs: input - json object to convert |
| 141 | +
|
| 142 | + Outputs: result - irep equivalent of input |
| 143 | +
|
| 144 | + Purpose: Deserialize a JSON irep representation. |
| 145 | +
|
| 146 | +\*******************************************************************/ |
| 147 | + |
| 148 | +void json_irept::convert_from_json(const jsont &in, irept &out) const |
| 149 | +{ |
| 150 | + std::vector<std::string> have_keys; |
| 151 | + for(const auto &keyval : in.object) |
| 152 | + have_keys.push_back(keyval.first); |
| 153 | + std::sort(have_keys.begin(), have_keys.end()); |
| 154 | + if(have_keys!=std::vector<std::string>{"comment", "id", "namedSub", "sub"}) |
| 155 | + throw "irep JSON representation is missing one of needed keys: " |
| 156 | + "'id', 'sub', 'namedSub', 'comment'"; |
| 157 | + |
| 158 | + out.id(in["id"].value); |
| 159 | + |
| 160 | + for(const auto &sub : in["sub"].array) |
| 161 | + { |
| 162 | + out.get_sub().push_back(irept()); |
| 163 | + convert_from_json(sub, out.get_sub().back()); |
| 164 | + } |
| 165 | + |
| 166 | + for(const auto &named_sub : in["namedSub"].object) |
| 167 | + convert_from_json(named_sub.second, out.get_named_sub()[named_sub.first]); |
| 168 | + |
| 169 | + for(const auto &comment : in["comment"].object) |
| 170 | + convert_from_json(comment.second, out.get_comments()[comment.first]); |
| 171 | +} |
0 commit comments