MCPcopy Create free account
hub / github.com/DOI-USGS/ISIS3 / pvlObjectToJSON

Function pvlObjectToJSON

isis/src/base/objs/PvlToJSON/PvlToJSON.cpp:296–334  ·  view source on GitHub ↗

* 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 =

Source from the content-addressed store, hash-verified

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 /**

Callers 4

jigsawFunction · 0.85
fitsToJsonFunction · 0.85
pvlToJSONFunction · 0.85
TESTFunction · 0.85

Calls 9

pvlContainerToJSONFunction · 0.85
pvlGroupToJSONFunction · 0.85
beginGroupMethod · 0.80
endGroupMethod · 0.80
beginObjectMethod · 0.80
endObjectMethod · 0.80
containsMethod · 0.45
nameMethod · 0.45
push_backMethod · 0.45

Tested by 1

TESTFunction · 0.68