summaryrefslogtreecommitdiffstats
path: root/src/jsonbuffer.cpp
blob: 4382713018ee769510f461f85d2d21d730289c0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtAddOn.JsonStream module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QDebug>
#include <QtEndian>
#include <QJsonDocument>

#include "jsonbuffer_p.h"
#include "qjsondocument.h"
#include "qjsonobject.h"
#include "bson/qt-bson_p.h"

QT_BEGIN_NAMESPACE_JSONSTREAM

/*!
  \class JsonBuffer
  \brief The JsonBuffer class parses data received into appropriate Json messages

  The JsonBuffer class wraps an internal buffer.  As you append
  data, the JsonBuffer object parses for received Json objects and
  raises an appropriate signal for each received object.

  \warning This class does not support JSON arrays.  Arrays that are received will be ignored.
*/

/*!
  Construct a JsonBuffer object with the given \a parent
*/

JsonBuffer::JsonBuffer(QObject *parent)
    : QObject(parent)
    , mFormat(FormatUndefined)
    , mParserState(ParseNormal)
    , mParserDepth(0)
    , mParserOffset(0)
    , mParserStartOffset(-1)
{
}

/*!
  Append the contents of a byte array \a data onto the buffer.
  During the execution of this function, the objectReceived
  signal may be raised.
*/

void JsonBuffer::append(const QByteArray& data)
{
    mBuffer.append(data.data(), data.size());
    processMessages();
}

/*!
  Append the \a data pointer with length \a len onto the JsonBuffer.
  During the execution of this function, objectReceived
  signal may be raised.
*/

void JsonBuffer::append(const char *data, int len)
{
    mBuffer.append(data, len);
    processMessages();
}

/*!
  Copy data from a file descriptor \a fd into the buffer.
  This function tries to eliminate extra data copy operations.
  It assumes that the file descriptor is ready to read and
  it does not try to read all of the data.

  Returns the number of bytes read or -1 for an error condition.
 */

int JsonBuffer::copyFromFd(int fd)
{
    const int maxcopy = 1024;
    uint oldSize = mBuffer.size();
    mBuffer.resize(oldSize + maxcopy);
    int n = ::read(fd, mBuffer.data()+oldSize, maxcopy);
    if (n > 0) {
        mBuffer.resize(oldSize+n);
        processMessages();
    }
    else
        mBuffer.resize(oldSize);
    return n;
}

/*!
    Clear the contents of the buffer.
 */

void JsonBuffer::clear()
{
    mBuffer.clear();
}

/*!
  \internal
*/

void JsonBuffer::processMessages()
{
    if (mFormat == FormatUndefined && mBuffer.size() >= 4) {
        if (strncmp("bson", mBuffer.data(), 4) == 0)
            mFormat = FormatBSON;
        else if (QJsonDocument::BinaryFormatTag == *((uint *) mBuffer.data()))
            mFormat = FormatQBJS;
        else
            mFormat = FormatUTF8;
    }

    switch (mFormat) {
    case FormatUndefined:
        break;
    case FormatUTF8:
        for (  ; mParserOffset < mBuffer.size() ; mParserOffset++ ) {
            char c = mBuffer.at(mParserOffset);
            // qDebug() << "Parsing: " << (int) c << c;
            switch (mParserState) {
            case ParseNormal:
                if ( c == '{' ) {
                    if ( mParserDepth == 0 )
                        mParserStartOffset = mParserOffset;
                    mParserDepth += 1;
                }
                else if ( c == '}' && mParserDepth > 0 ) {
                    mParserDepth -= 1;
                    if ( mParserDepth == 0 ) {
                        mParserOffset++;
                        QByteArray msg = mBuffer.mid(mParserStartOffset, mParserOffset - mParserStartOffset);
                        QJsonObject obj = QJsonDocument::fromJson(msg).object();
                        if (!obj.isEmpty())
                            emit objectReceived(obj);
                        mBuffer = mBuffer.mid(mParserOffset);
                        mParserState  = ParseNormal;
                        mParserDepth  = 0;
                        mParserOffset = 0;
                        mParserStartOffset = -1;
                    }
                }
                else if ( c == '"' ) {
                    mParserState = ParseInString;
                }
                break;
            case ParseInString:
                if ( c == '"' ) {
                    mParserState = ParseNormal;
                } else if ( c == '\\' ) {
                    mParserState = ParseInBackslash;
                }
                break;
            case ParseInBackslash:
                mParserState = ParseInString;
                break;
            }
        }
        break;
    case FormatBSON:
        while (mBuffer.size() >= 8) {
            qint32 message_size = qFromLittleEndian(((qint32 *)mBuffer.data())[1]);
            if (mBuffer.size() < message_size + 4)
                break;
            QByteArray msg = mBuffer.mid(4, message_size);
            QJsonObject obj = QJsonDocument::fromVariant(BsonObject(msg).toMap()).object();
            if (!obj.isEmpty())
                emit objectReceived(obj);
            mBuffer = mBuffer.mid(message_size+4);
        }
        break;
    case FormatQBJS:
        while (mBuffer.size() >= 12) {
            // ### TODO: Should use 'sizeof(Header)'
            qint32 message_size = qFromLittleEndian(((qint32 *)mBuffer.data())[2]) + 8;
            if (mBuffer.size() < message_size)
                break;
            QByteArray msg = mBuffer.left(message_size);
            QJsonObject obj = QJsonDocument::fromBinaryData(msg).object();
            if (!obj.isEmpty())
                emit objectReceived(obj);
            mBuffer = mBuffer.mid(message_size);
        }
        break;
    }
}

/*!
  Return the current encoding format used by the receive buffer
*/

EncodingFormat JsonBuffer::format() const
{
    return mFormat;
}

/*!
    \fn void JsonBuffer::objectReceived(const QJsonObject& object)
    This signal is emitted when a new Qt Binary Json \a object has been received on the
    stream.
*/

#include "moc_jsonbuffer_p.cpp"

QT_END_NAMESPACE_JSONSTREAM