|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: Util |
| 4 | +
|
| 5 | +Author: Thomas Kiley, [email protected] |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +#include "irep.h" |
| 10 | +#include "json.h" |
| 11 | +#include "json_irep.h" |
| 12 | + |
| 13 | +/*******************************************************************\ |
| 14 | +
|
| 15 | +Function: json_irept::json_irept |
| 16 | +
|
| 17 | + Inputs: |
| 18 | + include_comments - when generating the JSON, should the comments |
| 19 | + sub tree be included. |
| 20 | +
|
| 21 | + Outputs: |
| 22 | +
|
| 23 | + Purpose: To convert to JSON from an irep structure by recurssively |
| 24 | + generating JSON for the different sub trees. |
| 25 | +
|
| 26 | +\*******************************************************************/ |
| 27 | + |
| 28 | +json_irept::json_irept(bool include_comments): |
| 29 | + include_comments(include_comments) |
| 30 | +{} |
| 31 | + |
| 32 | +/*******************************************************************\ |
| 33 | +
|
| 34 | +Function: json_irept::convert_from_irep |
| 35 | +
|
| 36 | + Inputs: |
| 37 | + irep - The irep structure to turn into json |
| 38 | + json - The json object to be filled up. |
| 39 | +
|
| 40 | + Outputs: |
| 41 | +
|
| 42 | + Purpose: To convert to JSON from an irep structure by recurssively |
| 43 | + generating JSON for the different sub trees. |
| 44 | +
|
| 45 | +\*******************************************************************/ |
| 46 | + |
| 47 | +void json_irept::convert_from_irep(const irept &irep, jsont &json) const |
| 48 | +{ |
| 49 | + json_objectt &irep_object=json.make_object(); |
| 50 | + if(irep.id()!=ID_nil) |
| 51 | + irep_object["id"]=json_stringt(irep.id_string()); |
| 52 | + |
| 53 | + convert_sub_tree("sub", irep.get_sub(), irep_object); |
| 54 | + convert_named_sub_tree("named_sub", irep.get_named_sub(), irep_object); |
| 55 | + if(include_comments) |
| 56 | + { |
| 57 | + convert_named_sub_tree("comment", irep.get_comments(), irep_object); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/*******************************************************************\ |
| 62 | +
|
| 63 | +Function: json_irept::convert_sub_tree |
| 64 | +
|
| 65 | + Inputs: |
| 66 | + sub_tree_id - the name to give the subtree in the parent object |
| 67 | + sub_trees - the list of subtrees to parse |
| 68 | + parent - the parent JSON object who should be added to |
| 69 | +
|
| 70 | + Outputs: |
| 71 | +
|
| 72 | + Purpose: To convert to JSON from a list of ireps that are in an |
| 73 | + unlabelled subtree. The parent JSON object will get a key |
| 74 | + called sub_tree_id and the value shall be an array of JSON |
| 75 | + objects generated from each of the sub trees |
| 76 | +
|
| 77 | +\*******************************************************************/ |
| 78 | + |
| 79 | +void json_irept::convert_sub_tree( |
| 80 | + const std::string &sub_tree_id, |
| 81 | + const irept::subt &sub_trees, |
| 82 | + json_objectt &parent) const |
| 83 | +{ |
| 84 | + if(sub_trees.size()>0) |
| 85 | + { |
| 86 | + json_arrayt sub_objects; |
| 87 | + for(const irept &sub_tree : sub_trees) |
| 88 | + { |
| 89 | + json_objectt sub_object; |
| 90 | + convert_from_irep(sub_tree, sub_object); |
| 91 | + sub_objects.push_back(sub_object); |
| 92 | + } |
| 93 | + parent[sub_tree_id]=sub_objects; |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/*******************************************************************\ |
| 98 | +
|
| 99 | +Function: json_irept::convert_named_sub_tree |
| 100 | +
|
| 101 | + Inputs: |
| 102 | + sub_tree_id - the name to give the subtree in the parent object |
| 103 | + sub_trees - the map of subtrees to parse |
| 104 | + parent - the parent JSON object who should be added to |
| 105 | +
|
| 106 | + Outputs: |
| 107 | +
|
| 108 | + Purpose: To convert to JSON from a map of ireps that are in a |
| 109 | + named subtree. The parent JSON object will get a key |
| 110 | + called sub_tree_id and the value shall be a JSON object |
| 111 | + whose keys shall be the name of the sub tree and the value |
| 112 | + will be the object generated from the sub tree. |
| 113 | +
|
| 114 | +\*******************************************************************/ |
| 115 | + |
| 116 | +void json_irept::convert_named_sub_tree( |
| 117 | + const std::string &sub_tree_id, |
| 118 | + const irept::named_subt &sub_trees, |
| 119 | + json_objectt &parent) const |
| 120 | +{ |
| 121 | + if(sub_trees.size()>0) |
| 122 | + { |
| 123 | + json_objectt sub_objects; |
| 124 | + for(const auto &sub_tree : sub_trees) |
| 125 | + { |
| 126 | + json_objectt sub_object; |
| 127 | + convert_from_irep(sub_tree.second, sub_object); |
| 128 | + sub_objects[id2string(sub_tree.first)]=sub_object; |
| 129 | + } |
| 130 | + parent[sub_tree_id]=sub_objects; |
| 131 | + } |
| 132 | +} |
| 133 | + |
0 commit comments