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 
14 using json = nlohmann::json;
15 using namespace std;
16 
17 namespace Isis {
18 
77  json pvlKeywordToJSON(PvlKeyword &keyword) {
78  json jsonKeyword;
79 
80  // Convert values
81  if (keyword.size() == 1) {
82  jsonKeyword["Value"] = keyword[0].toStdString();
83  }
84  else if (keyword.size() > 1) {
85  json valueList;
86  for (int i = 0; i < keyword.size(); i++) {
87  valueList.push_back(keyword[i].toStdString());
88  }
89  jsonKeyword["Value"] = valueList;
90  }
91 
92  // Optionally convert units
93  if (keyword.size() == 1 && !keyword.unit(0).isEmpty()) {
94  jsonKeyword["Units"] = keyword.unit(0).toStdString();
95  }
96  else if (keyword.size() > 1 && !keyword.unit(0).isEmpty()) {
97  json valueList;
98  for (int i = 0; i < keyword.size(); i++) {
99  valueList.push_back(keyword.unit(i).toStdString());
100  }
101  jsonKeyword["Units"] = valueList;
102  }
103 
104  // Optionally convert comments
105  if (keyword.comments() == 1) {
106  jsonKeyword["Comment"] = keyword.comment(0).toStdString();
107  }
108  else if (keyword.comments() > 1) {
109  json commentList;
110  for (int i = 0; i < keyword.comments(); i++) {
111  commentList.push_back(keyword.comment(i).toStdString());
112  }
113  jsonKeyword["Comment"] = commentList;
114  }
115  return jsonKeyword;
116  }
117 
118 
132  json pvlContainerToJSON(PvlContainer &container) {
133  json jsonContainer;
134 
135  // Convert keywords
137  for (keywordIt = container.begin(); keywordIt != container.end(); keywordIt++) {
138  // Handle repeated keywords by packing them into an array
139  if ( jsonContainer.contains(keywordIt->name().toStdString()) ) {
140  if (!jsonContainer[keywordIt->name().toStdString()].is_array()) {
141  json repeatedArray;
142  repeatedArray.push_back(jsonContainer[keywordIt->name().toStdString()]);
143  jsonContainer[keywordIt->name().toStdString()] = repeatedArray;
144  }
145  jsonContainer[keywordIt->name().toStdString()].push_back(pvlKeywordToJSON(*keywordIt));
146  }
147  else {
148  jsonContainer[keywordIt->name().toStdString()] = pvlKeywordToJSON(*keywordIt);
149  }
150  }
151 
152  // Optionally convert comments
153  if (container.comments() == 1) {
154  jsonContainer["Comment"] = container.comment(0).toStdString();
155  }
156  else if (container.comments() > 1) {
157  json commentList;
158  for (int i = 0; i < container.comments(); i++) {
159  commentList.push_back(container.comment(i).toStdString());
160  }
161  jsonContainer["Comment"] = commentList;
162  }
163  return jsonContainer;
164 
165  }
166 
167 
213  json pvlGroupToJSON(PvlGroup &group) {
214  // PvlGroups are just PvlContainers with extra input/output options
215  // so we can just use the PvlContainer conversion directly.
216  return pvlContainerToJSON(group);;
217  }
218 
219 
294  json pvlObjectToJSON(PvlObject &object) {
295  // Convert keywords and comments
296  json jsonObject = pvlContainerToJSON(object);
297 
298  // Convert groups
300  for (groupIt = object.beginGroup(); groupIt != object.endGroup(); groupIt++) {
301  // Handle repeated elements by packing them into an array
302  if ( jsonObject.contains(groupIt->name().toStdString()) ) {
303  if (!jsonObject[groupIt->name().toStdString()].is_array()) {
304  json repeatedArray;
305  repeatedArray.push_back(jsonObject[groupIt->name().toStdString()]);
306  jsonObject[groupIt->name().toStdString()] = repeatedArray;
307  }
308  jsonObject[groupIt->name().toStdString()].push_back(pvlGroupToJSON(*groupIt));
309  }
310  else {
311  jsonObject[groupIt->name().toStdString()] = pvlGroupToJSON(*groupIt);
312  }
313  }
314 
315  // Convert nested objects
317  for (objectIt = object.beginObject(); objectIt != object.endObject(); objectIt++) {
318  // Handle repeated elements by packing them into an array
319  if ( jsonObject.contains(objectIt->name().toStdString()) ) {
320  if (!jsonObject[objectIt->name().toStdString()].is_array()) {
321  json repeatedArray;
322  repeatedArray.push_back(jsonObject[objectIt->name().toStdString()]);
323  jsonObject[objectIt->name().toStdString()] = repeatedArray;
324  }
325  jsonObject[objectIt->name().toStdString()].push_back(pvlObjectToJSON(*objectIt));
326  }
327  else {
328  jsonObject[objectIt->name().toStdString()] = pvlObjectToJSON(*objectIt);
329  }
330  }
331  return jsonObject;
332  }
333 
334 
348  json pvlToJSON(Pvl &pvl) {
349  return pvlObjectToJSON(pvl);
350  }
351 
352 }
Isis::PvlObject::PvlGroupIterator
QList< Isis::PvlGroup >::iterator PvlGroupIterator
The counter for groups.
Definition: PvlObject.h:83
Isis::PvlObject
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:61
Isis::PvlKeyword
A single keyword-value pair.
Definition: PvlKeyword.h:82
Isis::Pvl
Container for cube-like labels.
Definition: Pvl.h:119
Isis::pvlContainerToJSON
json pvlContainerToJSON(PvlContainer &container)
Convert the contents of a PvlContainer to a JSON object.
Definition: PvlToJSON.cpp:132
Isis::pvlKeywordToJSON
json pvlKeywordToJSON(PvlKeyword &keyword)
Convert the contents of a PvlKeyword to a JSON object.
Definition: PvlToJSON.cpp:77
Isis::pvlObjectToJSON
json pvlObjectToJSON(PvlObject &object)
Convert the contents of a PvlObject to a JSON object.
Definition: PvlToJSON.cpp:294
Isis::PvlKeyword::comments
int comments() const
Returns the number of lines of comments associated with this keyword.
Definition: PvlKeyword.h:160
Isis::PvlObject::PvlObjectIterator
QList< PvlObject >::iterator PvlObjectIterator
The counter for objects.
Definition: PvlObject.h:227
Isis::pvlToJSON
json pvlToJSON(Pvl &pvl)
Convert the contents of a Pvl to a JSON object.
Definition: PvlToJSON.cpp:348
Isis::PvlGroup
Contains multiple PvlContainers.
Definition: PvlGroup.h:41
Isis::pvlGroupToJSON
json pvlGroupToJSON(PvlGroup &group)
Convert the contents of a PvlGroup to a JSON object.
Definition: PvlToJSON.cpp:213
std
Namespace for the standard library.
Isis::PvlKeyword::size
int size() const
Returns the number of values stored in this keyword.
Definition: PvlKeyword.h:125
Isis::PvlContainer::PvlKeywordIterator
QList< PvlKeyword >::iterator PvlKeywordIterator
The keyword iterator.
Definition: PvlContainer.h:157
Isis::PvlKeyword::unit
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...
Definition: PvlKeyword.cpp:357
Isis::PvlContainer::end
PvlKeywordIterator end()
Return the ending iterator.
Definition: PvlContainer.h:194
Isis::PvlKeyword::comment
QString comment(const int index) const
Return a comment at the specified index.
Definition: PvlKeyword.cpp:441
Isis::PvlContainer
Contains more than one keyword-value pair.
Definition: PvlContainer.h:49
Isis::PvlContainer::begin
PvlKeywordIterator begin()
Return the beginning iterator.
Definition: PvlContainer.h:178
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16