* Convert the contents of a PvlContainer to a JSON object. * Any comments in the container will be stored in "Comment". * Comments associated with keywords will be stored inside their json object. * If a keyword is repeated in the container, then the instances will be * packed into an array in the order that they occur. * * This function is used by the PvlGroup, PvlObject, and Pv
| 131 | * @return @b json The contents of the container as a JSON object |
| 132 | */ |
| 133 | json pvlContainerToJSON(PvlContainer &container) { |
| 134 | json jsonContainer; |
| 135 | |
| 136 | // Convert keywords |
| 137 | PvlContainer::PvlKeywordIterator keywordIt; |
| 138 | for (keywordIt = container.begin(); keywordIt != container.end(); keywordIt++) { |
| 139 | string keywordName = keywordIt->name().replace("^", "ptr").replace(":", "_").toStdString(); |
| 140 | // Handle repeated keywords by packing them into an array |
| 141 | if ( jsonContainer.contains(keywordName) ) { |
| 142 | if (!jsonContainer[keywordName].is_array()) { |
| 143 | json repeatedArray; |
| 144 | repeatedArray.push_back(jsonContainer[keywordName]); |
| 145 | jsonContainer[keywordName] = repeatedArray; |
| 146 | } |
| 147 | jsonContainer[keywordName].push_back(pvlKeywordToJSON(*keywordIt)); |
| 148 | } |
| 149 | else { |
| 150 | jsonContainer[keywordName] = pvlKeywordToJSON(*keywordIt); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // Optionally convert comments |
| 155 | if (container.comments() == 1) { |
| 156 | jsonContainer["Comment"] = container.comment(0).toStdString(); |
| 157 | } |
| 158 | else if (container.comments() > 1) { |
| 159 | json commentList; |
| 160 | for (int i = 0; i < container.comments(); i++) { |
| 161 | commentList.push_back(container.comment(i).toStdString()); |
| 162 | } |
| 163 | jsonContainer["Comment"] = commentList; |
| 164 | } |
| 165 | return jsonContainer; |
| 166 | |
| 167 | } |
| 168 | |
| 169 | |
| 170 | /** |