<commit_before>#include "jsonforqt.h"

#include <QJsonValue>
#include <QJsonObject>
#include <QJsonArray>
#include <QPointF>
#include <QPoint>
#include <QSet>
#include <QHash>
#include <QMap>
#include <QString>

QJsonValue QPointF_toJson(QPointF point) {
    QJsonObject nodeObj;
    nodeObj["x"] = point.x();
    nodeObj["y"] = point.y();
    return nodeObj;
}

QPointF QPointF_fromJson(QJsonValue node) {
    QJsonObject nodeObj = node.toObject();
    qreal x = nodeObj["x"].toDouble();
    qreal y = nodeObj["y"].toDouble();
    return QPointF(x, y);
}

QJsonValue QPoint_toJson(QPoint point) {
    QJsonObject nodeObj;
    nodeObj["x"] = point.x();
    nodeObj["y"] = point.y();
    return nodeObj;
}

QPoint QPoint_fromJson(QJsonValue node) {
    QJsonObject nodeObj = node.toObject();
    int x = nodeObj["x"].toInt();
    int y = nodeObj["y"].toInt();
    return QPoint(x, y);
}

QJsonValue QHash_QString_QString_toJson(QHash<QString, QString> obj) {
    QJsonArray nodeArr;
    for (auto i = obj.constBegin(); i != obj.constEnd(); i++) {
        QJsonObject item;
        item["key"] = i.key();
        item["value"] = i.value();
        nodeArr.append(item);
    }
    return nodeArr;
}

QHash<QString, QString> QHash_QString_QString_fromJson(QJsonValue node) {
    QHash<QString, QString> obj;
    QJsonArray nodeArr = node.toArray();
    for (auto i = nodeArr.constBegin(); i != nodeArr.constEnd(); i++) {
        QJsonObject item = (*i).toObject();
        QString key = item["key"].toString();
        QString value = item["value"].toString();
        obj.insert(key, value);
    }
    return obj;
}

QJsonValue QMap_QString_QString_toJson(QMap<QString, QString> obj) {
    QJsonArray nodeArr;
    for (auto i = obj.constBegin(); i != obj.constEnd(); i++) {
        QJsonObject item;
        item["key"] = i.key();
        item["value"] = i.value();
        nodeArr.append(item);
    }
    return nodeArr;
}

QMap<QString, QString> QMap_QString_QString_fromJson(QJsonValue node) {
    QMap<QString, QString> obj;
    QJsonArray nodeArr = node.toArray();
    for (auto i = nodeArr.constBegin(); i != nodeArr.constEnd(); i++) {
        QJsonObject item = (*i).toObject();
        QString key = item["key"].toString();
        QString value = item["value"].toString();
        obj.insert(key, value);
    }
    return obj;
}<commit_msg>Add error checking to generic fromJson functions<commit_after>#include "jsonforqt.h"

#include <QJsonValue>
#include <QJsonObject>
#include <QJsonArray>
#include <QPointF>
#include <QPoint>
#include <QSet>
#include <QHash>
#include <QMap>
#include <QString>

QJsonValue QPointF_toJson(QPointF point) {
    QJsonObject nodeObj;
    nodeObj["x"] = point.x();
    nodeObj["y"] = point.y();
    return nodeObj;
}

QPointF QPointF_fromJson(QJsonValue node, bool *ok = NULL) {
    bool success;
    QJsonObject nodeObj = node.toObject();
    QJsonValue xVal = nodeObj["x"];
    QJsonValue yVal = nodeObj["y"];
    success = xVal.isDouble() && yVal.isDouble();
    if (ok) {
        *ok = success;
    }
    return QPointF(xVal.toDouble(), yVal.toDouble());
}

QJsonValue QPoint_toJson(QPoint point) {
    QJsonObject nodeObj;
    nodeObj["x"] = point.x();
    nodeObj["y"] = point.y();
    return nodeObj;
}

QPoint QPoint_fromJson(QJsonValue node, bool *ok = NULL) {
    bool success;
    QJsonObject nodeObj = node.toObject();
    QJsonValue xVal = nodeObj["x"];
    QJsonValue yVal = nodeObj["y"];
    success = xVal.isDouble() && xVal.toDouble() == xVal.toInt() && yVal.isDouble() && yVal.toDouble() == yVal.toInt();
    if (ok) {
        *ok = success;
    }
    return QPoint(xVal.toInt(), yVal.toInt());
}

QJsonValue QHash_QString_QString_toJson(QHash<QString, QString> obj) {
    QJsonArray nodeArr;
    for (auto i = obj.constBegin(); i != obj.constEnd(); i++) {
        QJsonObject item;
        item["key"] = i.key();
        item["value"] = i.value();
        nodeArr.append(item);
    }
    return nodeArr;
}

QHash<QString, QString> QHash_QString_QString_fromJson(QJsonValue node, bool *ok = NULL) {
    bool success = true;
    QHash<QString, QString> obj;
    if (!node.isArray()) {
        success = false;
    }
    QJsonArray nodeArr = node.toArray();
    for (auto i = nodeArr.constBegin(); i != nodeArr.constEnd(); i++) {
        QJsonObject item = (*i).toObject();
        QJsonValue keyVal = item["key"];
        QJsonValue valueVal = item["value"];
        if (!keyVal.isString() || !valueVal.isString()) {
            success = false;
        } else {
            obj.insert(keyVal.toString(), valueVal.toString());
        }
    }
    if (ok) {
        *ok = success;
    }
    return obj;
}

QJsonValue QMap_QString_QString_toJson(QMap<QString, QString> obj) {
    QJsonArray nodeArr;
    for (auto i = obj.constBegin(); i != obj.constEnd(); i++) {
        QJsonObject item;
        item["key"] = i.key();
        item["value"] = i.value();
        nodeArr.append(item);
    }
    return nodeArr;
}

QMap<QString, QString> QMap_QString_QString_fromJson(QJsonValue node, bool *ok = NULL) {
    bool success = true;
    QMap<QString, QString> obj;
    if (!node.isArray()) {
        success = false;
    }
    QJsonArray nodeArr = node.toArray();
    for (auto i = nodeArr.constBegin(); i != nodeArr.constEnd(); i++) {
        QJsonObject item = (*i).toObject();
        QJsonValue keyVal = item["key"];
        QJsonValue valueVal = item["value"];
        if (!keyVal.isString() || !valueVal.isString()) {
            success = false;
        } else {
            obj.insert(keyVal.toString(), valueVal.toString());
        }
    }
    if (ok) {
        *ok = success;
    }
    return obj;
}
{'fraction_non_alphanumeric': 0.15656476409259587, 'fraction_numerical': 0.0, 'mean_word_length': 3.0227434257285, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2093521', 'n_tokens_mistral': 1950, 'n_tokens_neox': 1954, 'n_words': 426}