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 }
27 
33 {
34  json_objectt irep_object;
35 
36  if(irep.id()!=ID_nil)
37  irep_object["id"]=json_stringt(irep.id_string());
38 
39  convert_sub_tree("sub", irep.get_sub(), irep_object);
40  convert_named_sub_tree("namedSub", irep.get_named_sub(), irep_object);
41 
43  convert_named_sub_tree("comment", irep.get_comments(), irep_object);
44 
45  return irep_object;
46 }
47 
54 
56  const std::string &sub_tree_id,
57  const irept::subt &sub_trees,
58  json_objectt &parent) const
59 {
60  if(!sub_trees.empty())
61  {
62  json_arrayt sub_objects;
63  for(const irept &sub_tree : sub_trees)
64  {
65  json_objectt sub_object=convert_from_irep(sub_tree);
66  sub_objects.push_back(sub_object);
67  }
68  parent[sub_tree_id]=sub_objects;
69  }
70 }
71 
80  const std::string &sub_tree_id,
81  const irept::named_subt &sub_trees,
82  json_objectt &parent) const
83 {
84  if(!sub_trees.empty())
85  {
86  json_objectt sub_objects;
87  for(const auto &sub_tree : sub_trees)
88  {
89  json_objectt sub_object=convert_from_irep(sub_tree.second);
90  sub_objects[id2string(sub_tree.first)]=sub_object;
91  }
92  parent[sub_tree_id]=sub_objects;
93  }
94 }
95 
100 {
101  std::vector<std::string> have_keys;
102  for(const auto &keyval : in.object)
103  have_keys.push_back(keyval.first);
104  std::sort(have_keys.begin(), have_keys.end());
105  if(have_keys!=std::vector<std::string>{"comment", "id", "namedSub", "sub"})
106  throw "irep JSON representation is missing one of needed keys: "
107  "'id', 'sub', 'namedSub', 'comment'";
108 
109  irept out(in["id"].value);
110 
111  for(const auto &sub : in["sub"].array)
112  out.get_sub().push_back(convert_from_json(sub));
113 
114  for(const auto &named_sub : in["namedSub"].object)
115  out.add(named_sub.first)=convert_from_json(named_sub.second);
116 
117  for(const auto &comment : in["comment"].object)
118  out.add(comment.first)=convert_from_json(comment.second);
119 
120  return out;
121 }
const std::string & id2string(const irep_idt &d)
Definition: irep.h:43
bool include_comments
Definition: json_irep.h:38
std::vector< irept > subt
Definition: irep.h:90
std::string comment(const rw_set_baset::entryt &entry, bool write)
Definition: race_check.cpp:107
Definition: json.h:23
subt & get_sub()
Definition: irep.h:245
jsont & push_back(const jsont &json)
Definition: json.h:163
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:79
named_subt & get_comments()
Definition: irep.h:249
Base class for tree-like data structures with sharing.
Definition: irep.h:86
std::map< irep_namet, irept > named_subt
Definition: irep.h:99
objectt object
Definition: json.h:132
named_subt & get_named_sub()
Definition: irep.h:247
json_objectt convert_from_irep(const irept &) const
To convert to JSON from an irep structure by recursively generating JSON for the different sub trees...
Definition: json_irep.cpp:32
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:55
irept & add(const irep_namet &name)
Definition: irep.cpp:306
const std::string & id_string() const
Definition: irep.h:192
irept convert_from_json(const jsont &) const
Deserialize a JSON irep representation.
Definition: json_irep.cpp:99