cprover
json_irep.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Util
4 
5 Author: Thomas Kiley, thomas.kiley@diffblue.com
6 
7 \*******************************************************************/
8 
11 
12 #include "json_irep.h"
13 
14 #include "irep.h"
15 #include "json.h"
16 
17 #include <algorithm>
18 
23 json_irept::json_irept(bool include_comments):
24  include_comments(include_comments)
25 {}
26 
31 void json_irept::convert_from_irep(const irept &irep, jsont &json) const
32 {
33  json_objectt &irep_object=json.make_object();
34  if(irep.id()!=ID_nil)
35  irep_object["id"]=json_stringt(irep.id_string());
36 
37  convert_sub_tree("sub", irep.get_sub(), irep_object);
38  convert_named_sub_tree("namedSub", irep.get_named_sub(), irep_object);
40  {
41  convert_named_sub_tree("comment", irep.get_comments(), irep_object);
42  }
43 }
44 
52  const std::string &sub_tree_id,
53  const irept::subt &sub_trees,
54  json_objectt &parent) const
55 {
56  if(!sub_trees.empty())
57  {
58  json_arrayt sub_objects;
59  for(const irept &sub_tree : sub_trees)
60  {
61  json_objectt sub_object;
62  convert_from_irep(sub_tree, sub_object);
63  sub_objects.push_back(sub_object);
64  }
65  parent[sub_tree_id]=sub_objects;
66  }
67 }
68 
77  const std::string &sub_tree_id,
78  const irept::named_subt &sub_trees,
79  json_objectt &parent) const
80 {
81  if(!sub_trees.empty())
82  {
83  json_objectt sub_objects;
84  for(const auto &sub_tree : sub_trees)
85  {
86  json_objectt sub_object;
87  convert_from_irep(sub_tree.second, sub_object);
88  sub_objects[id2string(sub_tree.first)]=sub_object;
89  }
90  parent[sub_tree_id]=sub_objects;
91  }
92 }
93 
97 void json_irept::convert_from_json(const jsont &in, irept &out) const
98 {
99  std::vector<std::string> have_keys;
100  for(const auto &keyval : in.object)
101  have_keys.push_back(keyval.first);
102  std::sort(have_keys.begin(), have_keys.end());
103  if(have_keys!=std::vector<std::string>{"comment", "id", "namedSub", "sub"})
104  throw "irep JSON representation is missing one of needed keys: "
105  "'id', 'sub', 'namedSub', 'comment'";
106 
107  out.id(in["id"].value);
108 
109  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  }
114 
115  for(const auto &named_sub : in["namedSub"].object)
116  convert_from_json(named_sub.second, out.add(named_sub.first));
117 
118  for(const auto &comment : in["comment"].object)
119  convert_from_json(comment.second, out.add(comment.first));
120 }
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
void convert_from_irep(const irept &irep, jsont &json) const
To convert to JSON from an irep structure by recursively generating JSON for the different sub trees...
Definition: json_irep.cpp:31
bool include_comments
Definition: json_irep.h:37
std::vector< irept > subt
Definition: irep.h:91
std::string comment(const rw_set_baset::entryt &entry, bool write)
Definition: race_check.cpp:115
Definition: json.h:21
subt & get_sub()
Definition: irep.h:245
jsont & push_back(const jsont &json)
Definition: json.h:157
const irep_idt & id() const
Definition: irep.h:189
void convert_named_sub_tree(const std::string &sub_tree_id, const irept::named_subt &sub_trees, json_objectt &parent) const
To convert to JSON from a map of ireps that are in a named subtree.
Definition: json_irep.cpp:76
named_subt & get_comments()
Definition: irep.h:249
Base class for tree-like data structures with sharing.
Definition: irep.h:87
std::map< irep_namet, irept > named_subt
Definition: irep.h:100
objectt object
Definition: json.h:129
named_subt & get_named_sub()
Definition: irep.h:247
json_irept(bool include_comments)
To convert to JSON from an irep structure by recursively generating JSON for the different sub trees...
Definition: json_irep.cpp:23
void convert_sub_tree(const std::string &sub_tree_id, const irept::subt &sub_trees, json_objectt &parent) const
To convert to JSON from a list of ireps that are in an unlabelled subtree.
Definition: json_irep.cpp:51
void convert_from_json(const jsont &, irept &) const
Deserialize a JSON irep representation.
Definition: json_irep.cpp:97
irept & add(const irep_namet &name)
Definition: irep.cpp:306
const std::string & id_string() const
Definition: irep.h:192
json_objectt & make_object()
Definition: json.h:234
json_objectt json(const source_locationt &location)
Definition: json_expr.cpp:23