Пример #1
0
/**
 * @internal
 * @brief QBsonConverter::mapValue converts alls simple values and redirects to
 * mapObject and mapArray methods. If a value can not converted an
 * empty bsonvalue is returned.
 *
 * @param jsValue jsValue to convert
 *
 * @return QBsonValue
 */
QBsonValue QBsonConverter::mapValue(const QJSValue &jsValue)
{
    if (jsValue.isBool()) {
        return QBsonValue(jsValue.toBool());
    }
    if (jsValue.isDate()) {
        return QBsonValue(jsValue.toDateTime());
    }
    if (jsValue.isString()) {
        return QBsonValue(jsValue.toString());
    }
    if (jsValue.isNumber()) {
        int vType = jsValue.toVariant().type();
        switch (vType) {
            case QVariant::Double:
                return QBsonValue(jsValue.toVariant().toDouble());
                break;
            case QVariant::Int:
                return QBsonValue(jsValue.toVariant().toInt());
                break;
        }
    }
    if (jsValue.isArray()) {
        return mapArray(jsValue);
    }
    if (jsValue.isObject()) {
        return mapObject(jsValue);
    }

    return QBsonValue();
}
Пример #2
0
QJsonValue QJsonConvert::jsonFromScriptValue(const QJSValue& jsval){
    if ( jsval.isArray() ){
        QJSValueIterator it(jsval);

        QJsonArray arr;
        while( it.hasNext() ){
            it.next();

            QJsonValue itJsonValue = jsonFromScriptValue(it.value());
            if( !itJsonValue.isNull() )
                arr.append(itJsonValue);
        }

        return QJsonValue(arr);
    } else if ( jsval.isObject() ){
        QJSValueIterator it(jsval);

        QJsonObject obj;
        while( it.hasNext() ){
            it.next();
            QJsonValue itJsonValue = jsonFromScriptValue(it.value());
            if( !itJsonValue.isNull() )
                obj[it.name()] = itJsonValue;
        }
        return QJsonValue(obj);
    } else  if ( jsval.isString() ){
        return QJsonValue(jsval.toString());
    } else if ( jsval.isBool() ){
        return QJsonValue(jsval.toString());
    } else if ( jsval.isNumber() ){
        return QJsonValue(jsval.toNumber() );
    }
    return QJsonValue();
}
static QString qjsValueToString(const QJSValue &v)
{
  if (v.isArray()) {
    return QStringLiteral("<array>");
  } else if (v.isBool()) {
    return v.toBool() ? QStringLiteral("true") : QStringLiteral("false");
  } else if (v.isCallable()) {
    return QStringLiteral("<callable>");
  } else if (v.isDate()) {
    return v.toDateTime().toString();
  } else if (v.isError()) {
    return QStringLiteral("<error>");
  } else if (v.isNull()) {
    return QStringLiteral("<null>");
  } else if (v.isNumber()) {
    return QString::number(v.toNumber());
  } else if (v.isObject()) {
    return QStringLiteral("<object>");
  } else if (v.isQObject()) {
    return Util::displayString(v.toQObject());
  } else if (v.isRegExp()) {
    return QStringLiteral("<regexp>");
  } else if (v.isString()) {
    return v.toString();
  } else if (v.isUndefined()) {
    return QStringLiteral("<undefined>");
  } else if (v.isVariant()) {
    return VariantHandler::displayString(v.toVariant());
  }
  return QStringLiteral("<unknown QJSValue>");
}
Пример #4
0
Option<Error> jsToJson(QJSValue value, JsonValue::Builder builder) {
    if (value.isArray()) {
        std::vector<QJSValue> arr;
        QJSValueIterator iter(value);
        while (iter.hasNext()) {
            iter.next();
            // dafuq!? why is this required
            if (!iter.hasNext()) {
                break;
            }
            arr.push_back(iter.value());
        }
        auto lst = builder.initArray(static_cast<unsigned int>(arr.size()));
        for (auto k = 0; k < arr.size(); ++k) {
            auto subvalue = lst[k];
            auto opt_err = jsToJson(arr[k], subvalue);
            if (opt_err.has()) {
                return opt_err;
            }
        }
        return Option<Error>();
    } if (value.isObject()) {
        std::vector<std::tuple<QString, QJSValue>> arr;
        QJSValueIterator iter(value);
        while (iter.hasNext()) {
            iter.next();
            arr.push_back(std::make_tuple(iter.name(), iter.value()));
        }
        auto lst = builder.initObject(static_cast<unsigned int>(arr.size()));
        for (auto k = 0; k < arr.size(); ++k) {
            auto value_builder = lst[k].initValue();
            const char *name = std::get<0>(arr[k]).toUtf8().constData();
            lst[k].setName(name);
            auto opt_err = jsToJson(std::get<1>(arr[k]), value_builder);
            if (opt_err.has()) {
                return opt_err;
            }
        }
        return Option<Error>();
    } if (value.isBool()) {
        builder.setBoolean(value.toBool());
        return Option<Error>();
    } if (value.isNumber()) {
        builder.setNumber(value.toNumber());
        return Option<Error>();
    } if (value.isString()) {
        const char *str = value.toString().toUtf8().constData();
        builder.setString(str);
        return Option<Error>();
    } if (value.isNull()) {
        builder.setNone();
        return Option<Error>();
    }
    auto tpname = toStdString(value.toVariant().typeName());
    Error err(Error::Kind::ClientSideError, "Invalid type: " + tpname);
    return err;
}
Пример #5
0
bool TemplateEngine::evaluateBooleanJavaScriptExpression(QJSEngine &engine,
                                                         const QString &expression, bool *result,
                                                         QString *errorMessage)
{
    if (errorMessage)
        errorMessage->clear();
    if (result)
        *result = false;
    const QJSValue value = engine.evaluate(expression);
    if (value.isError()) {
        if (errorMessage)
            *errorMessage = QString::fromLatin1("Error in \"%1\": %2")
                .arg(expression, value.toString());
        return false;
    }
    // Try to convert to bool, be that an int or whatever.
    if (value.isBool()) {
        if (result)
            *result = value.toBool();
        return true;
    }
    if (value.isNumber()) {
        if (result)
            *result = !qFuzzyCompare(value.toNumber(), 0);
        return true;
    }
    if (value.isString()) {
        if (result)
            *result = !value.toString().isEmpty();
        return true;
    }
    if (errorMessage)
        *errorMessage = QString::fromLatin1("Cannot convert result of \"%1\" (\"%2\"to bool.")
            .arg(expression, value.toString());

    return false;
}
Пример #6
0
extern "C" int hsqml_is_jval_bool(HsQMLJValHandle* hndl)
{
    QJSValue* value = reinterpret_cast<QJSValue*>(hndl);
    return value->isBool();
}