-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Qt JSON types support #6
Conversation
This adds support for Qt’s JSON types as proposed in the comment of PR cutelyst#5. The difficulty in using Qt’s JSON data types was that they can be casted to QVariantList/QVariantHash if they are inside a QVariant and e.g. `QVariant::canConvert<QVariantList>` returns true for a QJsonArray, but the next step by Cutelee is to cast them into QAssociativeIterable or QSequentialIterable. That only works for template based containers. So, my solution is to “intercept“ this types before the QVariant cast is tried and convert them directly into QVariantList or QVariantHash. If the JSON type is direct part of the context, this happens in `Cutelee::Variable::resolve()`, if the JSON type is stored in a context variable’s property or further down the road, the lookup will be done in `Cutelee::MetaType::lookup()`. Unit tests are currently written to test “normal“ usage as variable and to be used in a `{% for %}` loop tag. Maybe we should add some more tests to see if they play nicely with filters and the rest, even though I think that this implementations should make this possible without further changes.
Cool thanks, I think it would be best to remove the calls to make it a QVariantList/Hash, this would avoid creating a container each time a propriety is requested, you can use QJsonValue::toVariant() to return the result. |
Make the switch a bit simpler and convert directly to QVariant.
I now simplified the switch in Variable::resolve() and use QJsonValue::toVariant() there. For MetaType::lookup() it is imho currently not possible, as I rely on the exact type and would have to convert in the specialized lookup functions anyway. |
templates/lib/metatype.cpp
Outdated
@@ -106,12 +110,89 @@ static QVariant doQobjectLookUp(const QObject *const object, | |||
return object->property(property.toUtf8().constData()); | |||
} | |||
|
|||
static QVariant doVariantListLookUp(const QVariantList &list, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I meant was here (and on the hash lookup), change the type to a QJsonArray and return the QJsonValue::toVariant() of it
great thanks! |
This adds support for Qt’s JSON types (QJsonDocument, QJsonObject, QJsonArray, QJsonValue) as proposed in the comment of PR #5.
The difficulty in using Qt’s JSON data types was that they can be casted to QVariantList/QVariantHash if they are inside a QVariant and e.g.
QVariant::canConvert<QVariantList>
returnstrue
for a QJsonArray, but the next step by Cutelee is to cast them into QAssociativeIterable or QSequentialIterable. That only works for template based containers and ends with a segfault for the JSON types.So, my solution is to “intercept“ this types before the QVariant cast is tried and convert them directly into QVariantList or QVariantHash. If the JSON type is direct part of the context, this happens in
Cutelee::Variable::resolve()
, if the JSON type is stored in a context variable’s property or further down the road, the lookup will be done inCutelee::MetaType::lookup()
.Unit tests are currently written to test “normal“ usage as variable and to be used in a
{% for %}
loop tag. Maybe we should add some more tests to see if they play nicely with filters and the rest, even though I think that this implementations should make this possible without further changes.