| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | #ifndef QV4JSONOBJECT_H |
| 4 | #define QV4JSONOBJECT_H |
| 5 | |
| 6 | // |
| 7 | // W A R N I N G |
| 8 | // ------------- |
| 9 | // |
| 10 | // This file is not part of the Qt API. It exists purely as an |
| 11 | // implementation detail. This header file may change from version to |
| 12 | // version without notice, or even be removed. |
| 13 | // |
| 14 | // We mean it. |
| 15 | // |
| 16 | |
| 17 | #include "qv4object_p.h" |
| 18 | #include <qjsonarray.h> |
| 19 | #include <qjsonobject.h> |
| 20 | #include <qjsonvalue.h> |
| 21 | #include <qjsondocument.h> |
| 22 | #include <qhash.h> |
| 23 | |
| 24 | QT_BEGIN_NAMESPACE |
| 25 | |
| 26 | namespace QV4 { |
| 27 | |
| 28 | namespace Heap { |
| 29 | |
| 30 | struct JsonObject : Object { |
| 31 | void init(); |
| 32 | }; |
| 33 | |
| 34 | } |
| 35 | |
| 36 | struct ObjectItem { |
| 37 | const QV4::Object *o; |
| 38 | ObjectItem(const QV4::Object *o) : o(o) {} |
| 39 | }; |
| 40 | |
| 41 | inline bool operator ==(const ObjectItem &a, const ObjectItem &b) |
| 42 | { return a.o->d() == b.o->d(); } |
| 43 | |
| 44 | inline size_t qHash(const ObjectItem &i, size_t seed = 0) |
| 45 | { return ::qHash(t: (void *)i.o->d(), seed); } |
| 46 | |
| 47 | struct JsonObject : Object { |
| 48 | Q_MANAGED_TYPE(JsonObject) |
| 49 | V4_OBJECT2(JsonObject, Object) |
| 50 | private: |
| 51 | |
| 52 | typedef QSet<ObjectItem> V4ObjectSet; |
| 53 | public: |
| 54 | |
| 55 | static ReturnedValue method_parse(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 56 | static ReturnedValue method_stringify(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
| 57 | |
| 58 | static ReturnedValue fromJsonValue(ExecutionEngine *engine, const QJsonValue &value); |
| 59 | static ReturnedValue fromJsonObject(ExecutionEngine *engine, const QJsonObject &object); |
| 60 | static ReturnedValue fromJsonArray(ExecutionEngine *engine, const QJsonArray &array); |
| 61 | |
| 62 | static inline QJsonValue toJsonValue(const QV4::Value &value) |
| 63 | { V4ObjectSet visitedObjects; return toJsonValue(value, visitedObjects); } |
| 64 | static inline QJsonObject toJsonObject(const QV4::Object *o) |
| 65 | { V4ObjectSet visitedObjects; return toJsonObject(o, visitedObjects); } |
| 66 | static inline QJsonArray toJsonArray(const QV4::Object *o) |
| 67 | { V4ObjectSet visitedObjects; return toJsonArray(o, visitedObjects); } |
| 68 | |
| 69 | private: |
| 70 | static QJsonValue toJsonValue(const QV4::Value &value, V4ObjectSet &visitedObjects); |
| 71 | static QJsonObject toJsonObject(const Object *o, V4ObjectSet &visitedObjects); |
| 72 | static QJsonArray toJsonArray(const Object *o, V4ObjectSet &visitedObjects); |
| 73 | }; |
| 74 | |
| 75 | class JsonParser |
| 76 | { |
| 77 | public: |
| 78 | JsonParser(ExecutionEngine *engine, const QChar *json, int length); |
| 79 | |
| 80 | ReturnedValue parse(QJsonParseError *error); |
| 81 | |
| 82 | private: |
| 83 | inline bool eatSpace(); |
| 84 | inline QChar nextToken(); |
| 85 | |
| 86 | ReturnedValue parseObject(); |
| 87 | ReturnedValue parseArray(); |
| 88 | bool parseMember(Object *o); |
| 89 | bool parseString(QString *string); |
| 90 | bool parseValue(Value *val); |
| 91 | bool parseNumber(Value *val); |
| 92 | |
| 93 | ExecutionEngine *engine; |
| 94 | const QChar *head; |
| 95 | const QChar *json; |
| 96 | const QChar *end; |
| 97 | |
| 98 | int nestingLevel; |
| 99 | QJsonParseError::ParseError lastError; |
| 100 | }; |
| 101 | |
| 102 | } |
| 103 | |
| 104 | QT_END_NAMESPACE |
| 105 | |
| 106 | #endif |
| 107 | |
| 108 | |