* Convert the contents of a PvlObject to a JSON object. * Any comments in the base object will be stored in "Comment". * Comments associated with keywords, groups, or nested objects will be * stored inside their associated JSON object. * * An example demonstrating how nested objects and groups are converted * * PvlObject: * * Object = TestObject2 * TestKey3 =
| 294 | * @return @b json The contents of the object as a JSON object |
| 295 | */ |
| 296 | json pvlObjectToJSON(PvlObject &object) { |
| 297 | // Convert keywords and comments |
| 298 | json jsonObject = pvlContainerToJSON(object); |
| 299 | |
| 300 | // Convert groups |
| 301 | PvlObject::PvlGroupIterator groupIt; |
| 302 | for (groupIt = object.beginGroup(); groupIt != object.endGroup(); groupIt++) { |
| 303 | // Handle repeated elements by packing them into an array |
| 304 | if ( jsonObject.contains(groupIt->name().toStdString()) ) { |
| 305 | if (!jsonObject[groupIt->name().toStdString()].is_array()) { |
| 306 | json repeatedArray; |
| 307 | repeatedArray.push_back(jsonObject[groupIt->name().toStdString()]); |
| 308 | jsonObject[groupIt->name().toStdString()] = repeatedArray; |
| 309 | } |
| 310 | jsonObject[groupIt->name().toStdString()].push_back(pvlGroupToJSON(*groupIt)); |
| 311 | } |
| 312 | else { |
| 313 | jsonObject[groupIt->name().toStdString()] = pvlGroupToJSON(*groupIt); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | // Convert nested objects |
| 318 | PvlObject::PvlObjectIterator objectIt; |
| 319 | for (objectIt = object.beginObject(); objectIt != object.endObject(); objectIt++) { |
| 320 | // Handle repeated elements by packing them into an array |
| 321 | if ( jsonObject.contains(objectIt->name().toStdString()) ) { |
| 322 | if (!jsonObject[objectIt->name().toStdString()].is_array()) { |
| 323 | json repeatedArray; |
| 324 | repeatedArray.push_back(jsonObject[objectIt->name().toStdString()]); |
| 325 | jsonObject[objectIt->name().toStdString()] = repeatedArray; |
| 326 | } |
| 327 | jsonObject[objectIt->name().toStdString()].push_back(pvlObjectToJSON(*objectIt)); |
| 328 | } |
| 329 | else { |
| 330 | jsonObject[objectIt->name().toStdString()] = pvlObjectToJSON(*objectIt); |
| 331 | } |
| 332 | } |
| 333 | return jsonObject; |
| 334 | } |
| 335 | |
| 336 | |
| 337 | /** |