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  if(irep.id()!=ID_nil)
38  irep_object["id"]=json_stringt(irep.id_string());
39 
40  convert_sub_tree("sub", irep.get_sub(), irep_object);
41  convert_named_sub_tree("namedSub", irep.get_named_sub(), irep_object);
42 
44  convert_named_sub_tree("comment", irep.get_comments(), irep_object);
45 
46  return irep_object;
47 }
48 
55 
57  const std::string &sub_tree_id,
58  const irept::subt &sub_trees,
59  json_objectt &parent) const
60 {
61  if(!sub_trees.empty())
62  {
63  json_arrayt sub_objects;
64  for(const irept &sub_tree : sub_trees)
65  {
66  json_objectt sub_object=convert_from_irep(sub_tree);
67  sub_objects.push_back(sub_object);
68  }
69  parent[sub_tree_id]=sub_objects;
70  }
71 }
72 
81  const std::string &sub_tree_id,
82  const irept::named_subt &sub_trees,
83  json_objectt &parent) const
84 {
85  if(!sub_trees.empty())
86  {
87  json_objectt sub_objects;
88  for(const auto &sub_tree : sub_trees)
89  {
90  json_objectt sub_object=convert_from_irep(sub_tree.second);
91  sub_objects[id2string(sub_tree.first)]=sub_object;
92  }
93  parent[sub_tree_id]=sub_objects;
94  }
95 }
96 
101 {
102  std::vector<std::string> have_keys;
103  for(const auto &keyval : in.object)
104  have_keys.push_back(keyval.first);
105  std::sort(have_keys.begin(), have_keys.end());
106  if(have_keys!=std::vector<std::string>{"comment", "id", "namedSub", "sub"})
107  {
109  "irep JSON representation is missing one of needed keys: "
110  "'id', 'sub', 'namedSub', 'comment'");
111  }
112 
113  irept out(in["id"].value);
114 
115  for(const auto &sub : in["sub"].array)
116  out.get_sub().push_back(convert_from_json(sub));
117 
118  for(const auto &named_sub : in["namedSub"].object)
119  out.add(named_sub.first)=convert_from_json(named_sub.second);
120 
121  for(const auto &comment : in["comment"].object)
122  out.add(comment.first)=convert_from_json(comment.second);
123 
124  return out;
125 }
Thrown when failing to deserialize a value from some low level format, like JSON or raw bytes.
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
bool include_comments
Definition: json_irep.h:38
std::vector< irept > subt
Definition: irep.h:160
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:317
jsont & push_back(const jsont &json)
Definition: json.h:163
const irep_idt & id() const
Definition: irep.h:259
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:80
named_subt & get_comments()
Definition: irep.h:321
Base class for tree-like data structures with sharing.
Definition: irep.h:156
std::map< irep_namet, irept > named_subt
Definition: irep.h:169
objectt object
Definition: json.h:132
named_subt & get_named_sub()
Definition: irep.h:319
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
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
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:56
irept & add(const irep_namet &name)
Definition: irep.cpp:305
const std::string & id_string() const
Definition: irep.h:262
irept convert_from_json(const jsont &) const
Deserialize a JSON irep representation.
Definition: json_irep.cpp:100