//----------------------------------------------------------------------------
///
/// \file   helpers.cpp
///
/// \brief  helper functions
///
/// \date   20210902
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


//---Documentation------------------------------------------------------------


//---Includes -----------------------------------------------------------------


//---General--------------------------

#include <QJsonObject>
#include <QJsonArray>
#include <QMap>

//---Own------------------------------

#include <helpers.h>

//--- Implementation ----------------------------------------------------------

const QMap<QString, QString> jsonObjectToStringMap(const QJsonObject &object)
{
   QMap<QString, QString> attributes;
   for(const auto &key: object.keys())
   {
      if( object[key].isArray() )
      {
         attributes[key]=jsonArrayToString( object[key] );
      }
      else
      {
         attributes[key]=object[key].toString();
      }
   }
   return(attributes);
}

const QString jsonArrayToString( const QJsonValue &object )
{
   QString str;
   if( ! object.isArray() )
   {
      throw("JSON object is not an array");
   };

   for(const auto i: object.toArray() )
   {
      str+=i.toString()+"\n";
   };

   return( str );
}

const QStringList jsonArrayToStringList(const QJsonArray &array)
{
   QStringList list;
   for(const auto value: array)
   {
      if( value.isString() )
      {
         list << value.toString();
      }
      else if( value.isObject() )
      {
         QJsonObject subObject{value.toObject()};
         for(const auto &key: subObject.keys())
         {
            list << QString("%1 = %2").arg(key).arg(subObject[key].toString());
         }
      }
   }

   return(list);
}


//---fin----------------------------------------------------------------------
