Skip to content

Commit df36049

Browse files
smowtonpeterschrammel
authored andcommitted
Add json->irep deserialization routine
1 parent 938d739 commit df36049

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/util/json_irep.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ Author: Thomas Kiley, [email protected]
1010
#include "json.h"
1111
#include "json_irep.h"
1212

13+
#include <algorithm>
14+
1315
/*******************************************************************\
1416
1517
Function: json_irept::json_irept
1618
1719
Inputs:
18-
include_comments - when generating the JSON, should the comments
20+
include_comments - when writing JSON, should the comments
1921
sub tree be included.
2022
2123
Outputs:
@@ -131,3 +133,39 @@ void json_irept::convert_named_sub_tree(
131133
}
132134
}
133135

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+
}

src/util/json_irep.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class json_irept
1818
public:
1919
explicit json_irept(bool include_comments);
2020
void convert_from_irep(const irept &irep, jsont &json) const;
21+
void convert_from_json(const jsont &, irept &) const;
2122

2223
private:
2324
void convert_sub_tree(

0 commit comments

Comments
 (0)