Unverified Commit 854a7961 authored by acpaquette's avatar acpaquette Committed by GitHub
Browse files

PvlKeyword from JSON (#5030)

* Json reading fixes

* Addressed PR feedback

* Converted PvlKeyword unittest to gtest

* Added new tests for addJsonValue and setJsonValue
parent c970b543
Loading
Loading
Loading
Loading
+60
Changes for isis/src/base/objs/PvlKeyword/PvlKeyword.cpp: 60 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ find files of those names at the top level of this repository. **/
#include "PvlSequence.h"

using namespace std;
using json = nlohmann::json;
namespace Isis {
  //! Constructs a blank PvlKeyword object.
  PvlKeyword::PvlKeyword() {
@@ -157,6 +158,24 @@ namespace Isis {
    addValue(value, unit);
  }

  /**
   * Sets new value from Json.
   *
   * If no current value exists, this method sets the given json value
   * to the PvlKeyword.  Otherwise, it clears any existing values
   * and resets to the value given using addJsonValue(). Defaults to
   * unit = "" (empty QString).
   *
   * @param jsonobj New jsobobj to be parsed and assigned.
   * @param unit Units of measurement corresponding to the value.
   *
   * @see addJsonValue()
   */
  void PvlKeyword::setJsonValue(json jsonobj, QString unit)
  {
    clear();
    addJsonValue(jsonobj, unit);
  }

  /**
   * Sets the unit of measure for all current values if any exist
@@ -267,6 +286,47 @@ namespace Isis {
    }
  }

  /**
   * Adds a value with units.
   *
   * If no current value exists, this method sets the given json value.
   * Otherwise, it retains any current values and adds the json value
   * given to the array of values for this PvlKeyword object using addValue.
   * Defaults to unit = "" (empty QString).
   *
   * @param jsonobj New jsonobj to be parsed and assigned.
   * @param unit Units of measurement corresponding to the value.
   *
   * @see setJsonValue()
   * @see addValue()
   *
   * @throws Isis::iException::Unknown - jsonobj cannot be an array of values
   */
  void PvlKeyword::addJsonValue(json jsonobj, QString unit) {
    QString value;
    if (jsonobj.is_array()) {
      QString msg = "Unable to convert " + name() + " with nested json array value into PvlKeyword";
      throw IException(IException::Unknown, msg, _FILEINFO_);
    }
    else if (jsonobj.is_number())
    {
      value = QString::number(jsonobj.get<double>(), 'g', 16);
    }
    else if (jsonobj.is_boolean())
    {
      value = QString(jsonobj.get<bool>() ? "true" : "false");
    }
    else if (jsonobj.is_null())
    {
      value = QString("Null");
    }
    else
    {
      value = QString::fromStdString(jsonobj);
    }
    addValue(value, unit);
  }

  /**
   * Adds a value.
   *
+8
Changes for isis/src/base/objs/PvlKeyword/PvlKeyword.h: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@ find files of those names at the top level of this repository. **/
#include "Constants.h"
#include "IString.h"

#include <nlohmann/json.hpp>

namespace Isis {
  class PvlSequence;
  class PvlFormat;
@@ -78,6 +80,9 @@ namespace Isis {
   *                          the QString "Yes" instead of the keyword name. Added padding on
   *                          control statements to bring the code closer to ISIS Coding Standards.
   *                          References #1659.
   * 
   *  @history 2022-08-15 Adam Paquette - Added the ability to add and set PvlKeyword values from JSON
   *                          keyword value pairs.
   */
  class PvlKeyword {
    public:
@@ -112,6 +117,7 @@ namespace Isis {
      };

      void setValue(QString value, QString unit = "");
      void setJsonValue(nlohmann::json jsonobj, QString unit = "");

      void setUnits(QString units);
      void setUnits(QString value, QString units);
@@ -119,6 +125,8 @@ namespace Isis {
      PvlKeyword &operator=(QString value);

      void addValue(QString value, QString unit = "");
      void addJsonValue(nlohmann::json jsonobj, QString unit = "");

      PvlKeyword &operator+=(QString value);

      //! Returns the number of values stored in this keyword
−632

File deleted.

−400

File deleted.

Preview size limit exceeded, changes collapsed.

+7 −14
Changes for isis/src/base/objs/PvlObject/PvlObject.cpp: 7 added lines, 14 removed lines.
Original line number Diff line number Diff line
@@ -63,27 +63,20 @@ namespace Isis {

    for(auto it = jsonobj.begin(); it != jsonobj.end(); it++) {
        PvlKeyword keyword;
        if (it.value().is_array()) {
        keyword.setName(QString::fromStdString(it.key()));
        if (it.value().is_array()) {
          for(auto ar = it.value().begin(); ar!=it.value().end(); ar++) {
            keyword += QString::number(ar->get<double>(), 'g', 16);
            try {
              keyword.addJsonValue(*ar);
            }
            catch (IException &e) {
              QString msg = "While attempting to parse " + name + " the following occured";
              throw IException(e, IException::Unknown, msg, _FILEINFO_);
            }
        else if(it.value().is_number()) {
          keyword.setName(QString::fromStdString(it.key()));
          keyword.setValue(QString::number(it->get<double>()));
          }
        else if(it.value().is_boolean()) {
          keyword.setName(QString::fromStdString(it.key()));
          keyword.setValue(QString(it->get<bool>() ? "true" : "false"));
        }
        else if(it.value().is_null()) {
          keyword.setName(QString::fromStdString(it.key()));
          keyword.setValue(QString("Null"));
        }
        else {
          keyword.setName(QString::fromStdString(it.key()));
          keyword.setValue(QString::fromStdString(it.value()));
          keyword.setJsonValue(*it);
        }
        addKeyword(keyword);
    }
Loading