Advertisement
VRonin

Qt Json to XML

Jul 24th, 2018
2,688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class JsonToXml{
  2.     static int guessPrecision(double val){
  3.         double junk;
  4.         int precision = 0;
  5.         for(;!qFuzzyIsNull(std::modf(val,&junk));++precision)
  6.             val*=10.0;
  7.         return precision;
  8.     }
  9.     static void jsonValueToXml(const QJsonValue& val, const QString& name, QXmlStreamWriter& writer){
  10.         if(val.isNull() || val.isUndefined())
  11.             return;
  12.         if(val.isObject())
  13.             return jsonObjectToXml(val.toObject(),name,writer);
  14.         if(val.isArray())
  15.             return jsonArrayToXml(val.toArray(),name,writer);
  16.          writer.writeStartElement(name);
  17.          writer.writeAttribute(QStringLiteral("type"),QStringLiteral("value"));
  18.          if(val.isBool()){
  19.              writer.writeAttribute(QStringLiteral("valueType"),QStringLiteral("boolean"));
  20.              writer.writeCharacters(val.toBool() ? QStringLiteral("true") : QStringLiteral("false"));
  21.          }
  22.          else if(val.isDouble()){
  23.              writer.writeAttribute(QStringLiteral("valueType"),QStringLiteral("double"));
  24.              const double doubleVal = val.toDouble();
  25.              writer.writeCharacters(QString::number(doubleVal,'f',guessPrecision(doubleVal)));
  26.          }
  27.          else{
  28.              writer.writeAttribute(QStringLiteral("valueType"),QStringLiteral("string"));
  29.              writer.writeCharacters(val.toString());
  30.          }
  31.          writer.writeEndElement();
  32.     }
  33.     static void jsonObjectToXml(const QJsonObject& obj, const QString& name, QXmlStreamWriter& writer ){
  34.         if(obj.isEmpty())
  35.             return;
  36.         writer.writeStartElement(name);
  37.         writer.writeAttribute(QStringLiteral("type"),QStringLiteral("object"));
  38.         for(auto i=obj.constBegin(), objEnd = obj.constEnd();i!=objEnd;++i)
  39.             jsonValueToXml(*i,i.key(),writer);
  40.         writer.writeEndElement();
  41.     }
  42.     static void jsonArrayToXml(const QJsonArray& arr, const QString& name, QXmlStreamWriter& writer ){
  43.         writer.writeStartElement(name);
  44.         writer.writeAttribute(QStringLiteral("type"),QStringLiteral("array"));
  45.         for(int i=0, arrSize= arr.size();i<arrSize;++i)
  46.             jsonValueToXml(arr.at(i),name + QString::number(i),writer);
  47.         writer.writeEndElement();
  48.     }
  49. public:
  50.     static QString jsonToXml(const QJsonDocument& doc, const QString& rootElementName = "root"){
  51.         QString result;
  52.         if(doc.isEmpty() || doc.isNull())
  53.             return result;
  54.         QXmlStreamWriter writer(&result);
  55.         writer.writeStartDocument();
  56.         if(doc.isArray())
  57.             jsonArrayToXml(doc.array(),rootElementName,writer);
  58.         else if(doc.isObject())
  59.             jsonObjectToXml(doc.object(),rootElementName,writer);
  60.         return result;
  61.     }
  62. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement