Exemple #1
0
void mafPipe::setParametersHash(const QVariantHash hash) {
    // Fuse parameters coming from the given hash with that one already present.
    m_ParametersHash.unite(hash);
    QVariantHash::iterator iter = m_ParametersHash.begin();
    while (iter != m_ParametersHash.end()) {
        // Cycle over all hash items and set parameters present as properties.
        QByteArray ba = iter.key().toAscii();
        QVariant val(iter.value());
        setProperty(ba.constData(), val);
        ++iter;
    }
}
Exemple #2
0
QString QJson::encodeData(const QVariant &data, EncodeOptions options, QString *errorMessage,
	int indentation, QString currentLinePrefix)
{
	QString indentedLinePrefix = options.testFlag(Compact) ?
		QString::fromLatin1("") :
		(currentLinePrefix + QString::fromLatin1(" ").repeated(indentation));

	QString optionalNewLine = options.testFlag(Compact) ?
		QString::fromLatin1("") :
		(QString::fromLatin1("\n") + currentLinePrefix);

	QString optionalIndentedNewLine = options.testFlag(Compact) ?
		QString::fromLatin1("") :
		(QString::fromLatin1("\n") + indentedLinePrefix);

	QString encoded;

	switch (data.type())
	{
	case(QVariant::Bool) :
		encoded += QString::fromLatin1(data.toBool() ? "true" : "false");
		break;

	case(QVariant::Int) :
	case(QVariant::UInt) :
	case(QVariant::LongLong) :
	case(QVariant::ULongLong) :
							  Q_ASSERT(data.canConvert(QVariant::String));
		encoded = data.toString();
		break;

	case(QVariant::Double) :
		encoded = QString::number(data.toDouble(), 'g', 16);
		if (!encoded.contains(QString::fromLatin1(".")) && !encoded.contains(QString::fromLatin1("e")))
			encoded += ".0";
		break;

	case(QVariant::String) :
		encoded = encodeString(data.toString());
		break;

	case(QVariant::ByteArray) :
		encoded = encodeByteArray(data.toByteArray());
		break;

	case(QVariant::List) :
	{
		encoded = QString::fromLatin1("[") + optionalIndentedNewLine;
		QVariantList list = data.toList();
		for (int i = 0; i < list.count(); ++i)
		{
			if (i) encoded += QString::fromLatin1(",") + optionalIndentedNewLine;
			encoded += encodeData(list.at(i), options, errorMessage, indentation, indentedLinePrefix);
			if (errorMessage && !errorMessage->isNull())
				return QString();
		}
		encoded += optionalNewLine + QString::fromLatin1("]");
	}
						 break;

	case(QVariant::StringList) :
	{
		encoded = QString::fromLatin1("[") + optionalIndentedNewLine;
		QStringList list = data.toStringList();
		for (int i = 0; i < list.count(); ++i)
		{
			if (i) encoded += QString::fromLatin1(",") + optionalIndentedNewLine;
			encoded += encodeData(list.at(i), options, errorMessage, indentation, indentedLinePrefix);
			if (errorMessage && !errorMessage->isNull())
				return QString();
		}
		encoded += optionalNewLine + QString::fromLatin1("]");
	}
							   break;

	case(QVariant::Map) :
	{
		encoded = QString::fromLatin1("{") + optionalIndentedNewLine;
		QVariantMap map = data.toMap();
		QVariantMap::iterator i;
		bool first = true;
		for (i = map.begin(); i != map.end(); ++i)
		{
			if (!first)
				encoded += QString::fromLatin1(",") + optionalIndentedNewLine;
			first = false;
			encoded += encodeString(i.key());
			encoded += options.testFlag(Compact) ? QString::fromLatin1(":") : QString::fromLatin1(" : ");
			encoded += encodeData(i.value(), options, errorMessage, indentation, indentedLinePrefix);
			if (errorMessage && !errorMessage->isNull())
				return QString();
		}
		encoded += optionalNewLine + QString::fromLatin1("}");
	}
						break;

	case(QVariant::Hash) :
	{
		encoded = QString::fromLatin1("{") + optionalIndentedNewLine;
		QVariantHash hash = data.toHash();
		QVariantHash::iterator i;
		bool first = true;
		for (i = hash.begin(); i != hash.end(); ++i)
		{
			if (!first)
				encoded += QString::fromLatin1(",") + optionalIndentedNewLine;
			first = false;
			encoded += encodeString(i.key());
			encoded += options.testFlag(Compact) ? QString::fromLatin1(":") : QString::fromLatin1(" : ");
			encoded += encodeData(i.value(), options, errorMessage, indentation, indentedLinePrefix);
			if (errorMessage && !errorMessage->isNull())
				return QString();
		}
		encoded += optionalNewLine + QString::fromLatin1("}");
	}
						 break;

	case(QVariant::Invalid) :
		encoded = QString::fromLatin1("null");
		break;

	default:
		if (!options.testFlag(EncodeUnknownTypesAsNull))
		{
			if (errorMessage)
				*errorMessage = QString::fromLatin1("Can't encode this type of data to JSON: %1")
				.arg(data.typeName());
			return QString();
		}
		encoded = QString::fromLatin1("null");
		break;
	}

	return encoded;
}