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 "exception_utils.h"
15 #include "irep.h"
16 #include "json.h"
17 
18 #include <algorithm>
19 
24 json_irept::json_irept(bool _include_comments):
25  include_comments(_include_comments)
26 {
27 }
28 
34 {
35  json_objectt irep_object;
36 
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 
42  return irep_object;
43 }
44 
51 
53  const std::string &sub_tree_id,
54  const irept::subt &sub_trees,
55  json_objectt &parent) const
56 {
57  if(!sub_trees.empty())
58  {
59  json_arrayt sub_objects;
60  for(const irept &sub_tree : sub_trees)
61  {
62  json_objectt sub_object=convert_from_irep(sub_tree);
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  if(include_comments || !irept::is_comment(sub_tree.first))
86  {
87  json_objectt sub_object = convert_from_irep(sub_tree.second);
88  sub_objects[id2string(sub_tree.first)] = sub_object;
89  }
90  parent[sub_tree_id]=sub_objects;
91  }
92 }
93 
98 {
99  if(!in.is_object())
100  {
102  "irep JSON representation must be an object");
103  }
104 
105  const json_objectt &json_object = to_json_object(in);
106 
107  irept out;
108 
109  {
110  const auto it = json_object.find("id");
111 
112  if(it != json_object.end())
113  {
114  out.id(it->second.value);
115  }
116  }
117 
118  {
119  const auto it = json_object.find("sub");
120 
121  if(it != json_object.end())
122  {
123  for(const auto &sub : to_json_array(it->second))
124  out.get_sub().push_back(convert_from_json(sub));
125  }
126  }
127 
128  {
129  const auto it = json_object.find("namedSub");
130 
131  if(it != json_object.end())
132  {
133  for(const auto &named_sub : to_json_object(it->second))
134  out.add(named_sub.first) = convert_from_json(named_sub.second);
135  }
136  }
137 
138  return out;
139 }
140 
142 {
143  json_objectt result;
144 
145  if(!location.get_working_directory().empty())
146  result["workingDirectory"] = json_stringt(location.get_working_directory());
147 
148  if(!location.get_file().empty())
149  result["file"] = json_stringt(location.get_file());
150 
151  if(!location.get_line().empty())
152  result["line"] = json_stringt(location.get_line());
153 
154  if(!location.get_column().empty())
155  result["column"] = json_stringt(location.get_column());
156 
157  if(!location.get_function().empty())
158  result["function"] = json_stringt(location.get_function());
159 
160  if(!location.get_java_bytecode_index().empty())
161  result["bytecodeIndex"] = json_stringt(location.get_java_bytecode_index());
162 
163  return result;
164 }
exception_utils.h
source_locationt::get_function
const irep_idt & get_function() const
Definition: source_location.h:56
irept::is_comment
static bool is_comment(const irep_namet &name)
Definition: irep.h:489
json_irept::include_comments
bool include_comments
Definition: json_irep.h:38
deserialization_exceptiont
Thrown when failing to deserialize a value from some low level format, like JSON or raw bytes.
Definition: exception_utils.h:73
json_objectt::find
iterator find(const std::string &key)
Definition: json.h:356
irept::add
irept & add(const irep_namet &name)
Definition: irep.cpp:113
source_locationt::get_column
const irep_idt & get_column() const
Definition: source_location.h:51
sharing_treet< irept, std::map< irep_namet, irept > >::named_subt
typename dt::named_subt named_subt
Definition: irep.h:183
to_json_array
json_arrayt & to_json_array(jsont &json)
Definition: json.h:426
jsont
Definition: json.h:27
json_irep.h
Util.
source_locationt::get_line
const irep_idt & get_line() const
Definition: source_location.h:46
json_arrayt
Definition: json.h:165
json_objectt
Definition: json.h:300
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
irept::get_named_sub
named_subt & get_named_sub()
Definition: irep.h:479
irept::id_string
const std::string & id_string() const
Definition: irep.h:421
irept::id
const irep_idt & id() const
Definition: irep.h:418
dstringt::empty
bool empty() const
Definition: dstring.h:88
json_irept::convert_from_irep
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:33
sharing_treet< irept, std::map< irep_namet, irept > >::subt
typename dt::subt subt
Definition: irep.h:182
json_irept::convert_named_sub_tree
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
source_locationt
Definition: source_location.h:20
json_irept::convert_sub_tree
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:52
source_locationt::get_java_bytecode_index
const irep_idt & get_java_bytecode_index() const
Definition: source_location.h:81
json_objectt::end
iterator end()
Definition: json.h:386
to_json_object
json_objectt & to_json_object(jsont &json)
Definition: json.h:444
json_irept::json_irept
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:24
irept::get_sub
subt & get_sub()
Definition: irep.h:477
json.h
json
json_objectt json(const source_locationt &location)
Definition: json_irep.cpp:141
source_locationt::get_file
const irep_idt & get_file() const
Definition: source_location.h:36
irept
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition: irep.h:394
jsont::is_object
bool is_object() const
Definition: json.h:56
source_locationt::get_working_directory
const irep_idt & get_working_directory() const
Definition: source_location.h:41
json_arrayt::push_back
jsont & push_back(const jsont &json)
Definition: json.h:212
irep.h
json_irept::convert_from_json
irept convert_from_json(const jsont &) const
Deserialize a JSON irep representation.
Definition: json_irep.cpp:97
json_stringt
Definition: json.h:270