CBMC
json_symbol_table.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: JSON symbol table deserialization
4 
5 Author: Chris Smowton, chris.smowton@diffblue.com
6 
7 \*******************************************************************/
8 
9 #include "json_symbol_table.h"
10 
11 #include <util/exception_utils.h>
12 #include <util/json.h>
13 #include <util/symbol_table_base.h>
14 
15 #include "json_symbol.h"
16 
17 void symbol_table_from_json(const jsont &in, symbol_table_baset &symbol_table)
18 {
19  if(!in.is_object())
20  {
22  "symbol_table_from_json: JSON input must be an object");
23  }
24 
25  const json_objectt &json_object = to_json_object(in);
26  const auto it = json_object.find("symbolTable");
27 
28  if(it == json_object.end())
29  {
31  "symbol_table_from_json: JSON object must have key `symbolTable`");
32  }
33 
34  if(!it->second.is_object())
35  {
37  "symbol_table_from_json: JSON symbol table must be an object");
38  }
39 
40  const json_objectt &json_symbol_table = to_json_object(it->second);
41 
42  for(const auto &pair : json_symbol_table)
43  {
44  const jsont &json_symbol = pair.second;
45 
46  symbolt symbol = symbol_from_json(json_symbol);
47 
48  if(symbol_table.add(symbol))
50  "symbol_table_from_json: duplicate symbol name `" +
51  id2string(symbol.name) + "`");
52  }
53 
55 }
Thrown when failing to deserialize a value from some low level format, like JSON or raw bytes.
iterator find(const std::string &key)
Definition: json.h:354
iterator end()
Definition: json.h:384
Definition: json.h:27
bool is_object() const
Definition: json.h:56
The symbol table base class interface.
virtual void validate(const validation_modet vm=validation_modet::INVARIANT) const =0
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
Symbol table entry.
Definition: symbol.h:28
irep_idt name
The unique identifier.
Definition: symbol.h:40
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
json_objectt & to_json_object(jsont &json)
Definition: json.h:442
symbolt symbol_from_json(const jsont &in)
Deserialise a json object to a symbolt.
Definition: json_symbol.cpp:45
void symbol_table_from_json(const jsont &in, symbol_table_baset &symbol_table)
Author: Diffblue Ltd.