Isis 3 Programmer Reference
PvlToJSON.cpp
1
6/* SPDX-License-Identifier: CC0-1.0 */
7#include "PvlToJSON.h"
8
9#include <nlohmann/json.hpp>
10
11#include "Pvl.h"
12#include "PvlKeyword.h"
13#include "IException.h"
14
15using json = nlohmann::json;
16using namespace std;
17
18namespace Isis {
19
79 json jsonKeyword;
80
81 // Convert values
82 if (keyword.size() == 1) {
83 jsonKeyword["Value"] = keyword[0].toStdString();
84 }
85 else if (keyword.size() > 1) {
86 json valueList;
87 for (int i = 0; i < keyword.size(); i++) {
88 valueList.push_back(keyword[i].toStdString());
89 }
90 jsonKeyword["Value"] = valueList;
91 }
92
93 // Optionally convert units
94 if (keyword.size() == 1 && !keyword.unit(0).isEmpty()) {
95 jsonKeyword["Units"] = keyword.unit(0).toStdString();
96 }
97 else if (keyword.size() > 1 && !keyword.unit(0).isEmpty()) {
98 json valueList;
99 for (int i = 0; i < keyword.size(); i++) {
100 valueList.push_back(keyword.unit(i).toStdString());
101 }
102 jsonKeyword["Units"] = valueList;
103 }
104
105 // Optionally convert comments
106 if (keyword.comments() == 1) {
107 jsonKeyword["Comment"] = keyword.comment(0).toStdString();
108 }
109 else if (keyword.comments() > 1) {
110 json commentList;
111 for (int i = 0; i < keyword.comments(); i++) {
112 commentList.push_back(keyword.comment(i).toStdString());
113 }
114 jsonKeyword["Comment"] = commentList;
115 }
116 return jsonKeyword;
117 }
118
119
134 json jsonContainer;
135
136 // Convert keywords
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
216 // PvlGroups are just PvlContainers with extra input/output options
217 // so we can just use the PvlContainer conversion directly.
218 return pvlContainerToJSON(group);;
219 }
220
221
297 // Convert keywords and comments
298 json jsonObject = pvlContainerToJSON(object);
299
300 // Convert groups
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
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
350 json pvlToJSON(Pvl &pvl) {
351 return pvlObjectToJSON(pvl);
352 }
353
362 json pvlToJSON(QString pvlFile) {
363 Pvl pvl;
364
365 try {
366 pvl.read(pvlFile);
367 }
368 catch (IException &e){
369 QString msg = QString("Failed to open file for PVL Input: [%1]").arg(pvlFile);
370 throw IException(e, IException::User, msg, _FILEINFO_);
371 }
372
373 return pvlToJSON(pvl);
374 }
375}
Isis exception class.
Definition IException.h:91
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
Definition IException.h:126
Contains more than one keyword-value pair.
QList< PvlKeyword >::iterator PvlKeywordIterator
The keyword iterator.
Contains multiple PvlContainers.
Definition PvlGroup.h:41
Container for cube-like labels.
Definition Pvl.h:119
void read(const QString &file)
Loads PVL information from a stream.
Definition Pvl.cpp:90
A single keyword-value pair.
Definition PvlKeyword.h:87
int size() const
Returns the number of values stored in this keyword.
Definition PvlKeyword.h:133
QString unit(const int index=0) const
Returns the units of measurement of the element of the array of values for the object at the specifie...
int comments() const
Returns the number of lines of comments associated with this keyword.
Definition PvlKeyword.h:168
QString comment(const int index) const
Return a comment at the specified index.
Contains Pvl Groups and Pvl Objects.
Definition PvlObject.h:61
QList< Isis::PvlGroup >::iterator PvlGroupIterator
The counter for groups.
Definition PvlObject.h:83
QList< PvlObject >::iterator PvlObjectIterator
The counter for objects.
Definition PvlObject.h:229
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
json pvlKeywordToJSON(PvlKeyword &keyword)
Convert the contents of a PvlKeyword to a JSON object.
Definition PvlToJSON.cpp:78
json pvlToJSON(Pvl &pvl)
Convert the contents of a Pvl to a JSON object.
json pvlGroupToJSON(PvlGroup &group)
Convert the contents of a PvlGroup to a JSON object.
json pvlContainerToJSON(PvlContainer &container)
Convert the contents of a PvlContainer to a JSON object.
json pvlObjectToJSON(PvlObject &object)
Convert the contents of a PvlObject to a JSON object.
Namespace for the standard library.