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
24QT_BEGIN_NAMESPACE
25
26namespace QV4 {
27
28namespace Heap {
29
30struct JsonObject : Object {
31 void init();
32};
33
34}
35
36struct ObjectItem {
37 const QV4::Object *o;
38 ObjectItem(const QV4::Object *o) : o(o) {}
39};
40
41inline bool operator ==(const ObjectItem &a, const ObjectItem &b)
42{ return a.o->d() == b.o->d(); }
43
44inline size_t qHash(const ObjectItem &i, size_t seed = 0)
45{ return ::qHash(t: (void *)i.o->d(), seed); }
46
47struct JsonObject : Object {
48 Q_MANAGED_TYPE(JsonObject)
49 V4_OBJECT2(JsonObject, Object)
50private:
51
52 typedef QSet<ObjectItem> V4ObjectSet;
53public:
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::ArrayObject *a)
67 { V4ObjectSet visitedObjects; return toJsonArray(a, visitedObjects); }
68
69private:
70 static QJsonValue toJsonValue(const QV4::Value &value, V4ObjectSet &visitedObjects);
71 static QJsonObject toJsonObject(const Object *o, V4ObjectSet &visitedObjects);
72 static QJsonArray toJsonArray(const ArrayObject *a, V4ObjectSet &visitedObjects);
73
74};
75
76class JsonParser
77{
78public:
79 JsonParser(ExecutionEngine *engine, const QChar *json, int length);
80
81 ReturnedValue parse(QJsonParseError *error);
82
83private:
84 inline bool eatSpace();
85 inline QChar nextToken();
86
87 ReturnedValue parseObject();
88 ReturnedValue parseArray();
89 bool parseMember(Object *o);
90 bool parseString(QString *string);
91 bool parseValue(Value *val);
92 bool parseNumber(Value *val);
93
94 ExecutionEngine *engine;
95 const QChar *head;
96 const QChar *json;
97 const QChar *end;
98
99 int nestingLevel;
100 QJsonParseError::ParseError lastError;
101};
102
103}
104
105QT_END_NAMESPACE
106
107#endif
108
109

source code of qtdeclarative/src/qml/jsruntime/qv4jsonobject_p.h