summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@nokia.com>2012-01-09 12:17:48 +0100
committerLars Knoll <lars.knoll@nokia.com>2012-01-09 13:29:08 +0100
commitbbf09d4de0413ab5ea173639511c95da00aca2ab (patch)
treeef06b4500261419e7f99abec29969750bd94f662 /src
parent12b74f21879ba9c13542640df8df59fd226356ee (diff)
Changes after API review round.
* Added: QJsonArray/Object::count() QJsonArray::prepend() * Renamed: QJsonDocument::Tag to BinaryFormatTag QJsonObject::numKeys() to size() QJsonValue::Boolean to Bool * Replaced QJsonDocument::type() with isArray() and isObject() * Made some constructors explicit. * Removed QJsonObject::toJson() * Added isType() methods to QJsonValue * added some ### for further changes Change-Id: Id88f24f2921b53d6781def3f808e146c9f61cb31 Sanity-Review: Lars Knoll <lars.knoll@nokia.com> Reviewed-by: Simon Hausmann <simon.hausmann@nokia.com>
Diffstat (limited to 'src')
-rw-r--r--src/qjson.cpp8
-rw-r--r--src/qjson_p.h6
-rw-r--r--src/qjsonarray.cpp5
-rw-r--r--src/qjsonarray.h8
-rw-r--r--src/qjsondocument.cpp17
-rw-r--r--src/qjsondocument.h10
-rw-r--r--src/qjsonobject.cpp10
-rw-r--r--src/qjsonobject.h8
-rw-r--r--src/qjsonparser.cpp6
-rw-r--r--src/qjsonvalue.cpp14
-rw-r--r--src/qjsonvalue.h16
-rw-r--r--src/qjsonwriter.cpp2
12 files changed, 68 insertions, 42 deletions
diff --git a/src/qjson.cpp b/src/qjson.cpp
index 1148db5..5936949 100644
--- a/src/qjson.cpp
+++ b/src/qjson.cpp
@@ -69,7 +69,7 @@ void Data::compact()
int size = sizeof(Base) + reserve + base->length*sizeof(offset);
int alloc = sizeof(Header) + size;
Header *h = (Header *) malloc(alloc);
- h->tag = QJsonDocument::Tag;
+ h->tag = QJsonDocument::BinaryFormatTag;
h->version = 1;
Base *b = h->root();
b->size = size;
@@ -126,7 +126,7 @@ void Data::validate()
if (valid != Unchecked)
return;
- if (header->tag != QJsonDocument::Tag || header->version != 1u) {
+ if (header->tag != QJsonDocument::BinaryFormatTag || header->version != 1u) {
valid = Invalid;
return;
}
@@ -242,7 +242,7 @@ int Value::usedStorage(const Base *b) const
s = objectOrArray(b)->size;
break;
case QJsonValue::Null:
- case QJsonValue::Boolean:
+ case QJsonValue::Bool:
default:
break;
}
@@ -263,7 +263,7 @@ bool Value::isValid(const Base *b) const
offset = val;
break;
case QJsonValue::Null:
- case QJsonValue::Boolean:
+ case QJsonValue::Bool:
default:
break;
}
diff --git a/src/qjson_p.h b/src/qjson_p.h
index 576ae41..4e88f1f 100644
--- a/src/qjson_p.h
+++ b/src/qjson_p.h
@@ -525,7 +525,7 @@ struct Header {
inline bool Value::toBoolean() const
{
- Q_ASSERT(type == QJsonValue::Boolean);
+ Q_ASSERT(type == QJsonValue::Bool);
return val != 0;
}
@@ -598,7 +598,7 @@ struct Data {
alloc = sizeof(Header) + sizeof(Base) + reserved + sizeof(offset);
header = (Header *)malloc(alloc);
- header->tag = QJsonDocument::Tag;
+ header->tag = QJsonDocument::BinaryFormatTag;
header->version = 1;
Base *b = header->root();
b->size = sizeof(Base);
@@ -642,7 +642,7 @@ struct Data {
char *raw = (char *)malloc(size);
memcpy(raw + sizeof(Header), b, b->size);
Header *h = (Header *)raw;
- h->tag = QJsonDocument::Tag;
+ h->tag = QJsonDocument::BinaryFormatTag;
h->version = 1;
Data *d = new Data(raw, size);
d->compactionCounter = compactionCounter;
diff --git a/src/qjsonarray.cpp b/src/qjsonarray.cpp
index ae1d68b..9e25107 100644
--- a/src/qjsonarray.cpp
+++ b/src/qjsonarray.cpp
@@ -151,6 +151,11 @@ QJsonValue QJsonArray::last() const
return at(a ? (a->length - 1) : 0);
}
+void QJsonArray::prepend(const QJsonValue &value)
+{
+ insert(0, value);
+}
+
void QJsonArray::append(const QJsonValue &value)
{
insert(a ? a->length : 0, value);
diff --git a/src/qjsonarray.h b/src/qjsonarray.h
index 931ed51..0ad2779 100644
--- a/src/qjsonarray.h
+++ b/src/qjsonarray.h
@@ -63,24 +63,30 @@ public:
QVariantList toVariantList() const;
int size() const;
+ inline int count() const { return size(); }
+
bool isEmpty() const;
QJsonValue at(int i) const;
QJsonValue first() const;
QJsonValue last() const;
+ void prepend(const QJsonValue &value);
void append(const QJsonValue &value);
void removeAt(int i);
QJsonValue takeAt(int i);
void insert(int i, const QJsonValue &value);
+ // ### void replace(int i, const QJsonValue &value);
+
bool contains(const QJsonValue &element) const;
-// JsonValue &operator[](int i);
+ // ### JsonValueRef &operator[](int i);
QJsonValue operator[](int i) const;
bool operator==(const QJsonArray &other) const;
bool operator!=(const QJsonArray &other) const;
+ // ### make private
void detach(uint reserve = 0);
private:
diff --git a/src/qjsondocument.cpp b/src/qjsondocument.cpp
index 312f71e..4702428 100644
--- a/src/qjsondocument.cpp
+++ b/src/qjsondocument.cpp
@@ -107,7 +107,7 @@ QJsonDocument QJsonDocument::fromBinaryData(const QByteArray &data)
Private::Header *h = (Private::Header *) data.constData();
if (data.size() < (int)(sizeof(Private::Header) + sizeof(Private::Base)) ||
- h->tag != QJsonDocument::Tag || h->version != 1u ||
+ h->tag != QJsonDocument::BinaryFormatTag || h->version != 1u ||
sizeof(Private::Header) + h->root()->size > (uint)data.size())
return QJsonDocument();
@@ -179,13 +179,22 @@ QByteArray QJsonDocument::toBinaryData() const
return QByteArray(d->rawData, d->header->root()->size + sizeof(Private::Header));
}
-QJsonValue::Type QJsonDocument::type() const
+bool QJsonDocument::isArray() const
{
if (!d)
- return QJsonValue::Null;
+ return false;
+
+ Private::Header *h = (Private::Header *)d->rawData;
+ return h->root()->isArray();
+}
+
+bool QJsonDocument::isObject() const
+{
+ if (!d)
+ return false;
Private::Header *h = (Private::Header *)d->rawData;
- return h->root()->isArray() ? QJsonValue::Array : QJsonValue::Object;
+ return h->root()->isObject();
}
QJsonObject QJsonDocument::object() const
diff --git a/src/qjsondocument.h b/src/qjsondocument.h
index 02da9f3..ba94729 100644
--- a/src/qjsondocument.h
+++ b/src/qjsondocument.h
@@ -51,11 +51,11 @@ namespace QtJson {
class Q_JSON_EXPORT QJsonDocument
{
public:
- static const uint Tag = ('q') | ('b' << 8) | ('j' << 16) | ('s' << 24);
+ static const uint BinaryFormatTag = ('q') | ('b' << 8) | ('j' << 16) | ('s' << 24);
QJsonDocument();
- QJsonDocument(const QJsonObject &object);
- QJsonDocument(const QJsonArray &array);
+ explicit QJsonDocument(const QJsonObject &object);
+ explicit QJsonDocument(const QJsonArray &array);
~QJsonDocument();
QJsonDocument(const QJsonDocument &other);
@@ -71,8 +71,8 @@ public:
static QJsonDocument fromJson(const QByteArray &json);
bool isEmpty() const;
-
- QJsonValue::Type type() const;
+ bool isArray() const;
+ bool isObject() const;
QJsonObject object() const;
QJsonArray array() const;
diff --git a/src/qjsonobject.cpp b/src/qjsonobject.cpp
index c2259d9..bbe9b80 100644
--- a/src/qjsonobject.cpp
+++ b/src/qjsonobject.cpp
@@ -111,14 +111,6 @@ QVariantMap QJsonObject::toVariantMap() const
return map;
}
-QByteArray QJsonObject::toJson() const
-{
- QByteArray json;
- QJsonWriter::objectToJson(o, json, 0);
- return json;
-}
-
-
QStringList QJsonObject::keys() const
{
if (!d)
@@ -134,7 +126,7 @@ QStringList QJsonObject::keys() const
return keys;
}
-int QJsonObject::numKeys() const
+int QJsonObject::size() const
{
if (!d)
return 0;
diff --git a/src/qjsonobject.h b/src/qjsonobject.h
index 397131d..de46cdd 100644
--- a/src/qjsonobject.h
+++ b/src/qjsonobject.h
@@ -59,17 +59,16 @@ public:
static QJsonObject fromVariantMap(const QVariantMap &map);
QVariantMap toVariantMap() const;
- QByteArray toJson() const;
-
QStringList keys() const;
- int numKeys() const;
+ int size() const;
+ inline int count() const { return size(); }
bool isEmpty() const;
// ### rather use an iterator?
// QString keyAt(int);
// QSonValue valueAt(int)
- QJsonValue value(const QString & key) const;
+ QJsonValue value(const QString &key) const;
void insert(const QString &key, const QJsonValue &value);
void remove(const QString &key);
@@ -79,6 +78,7 @@ public:
bool operator==(const QJsonObject &other) const;
bool operator!=(const QJsonObject &other) const;
+ // mkae private
void detach(uint reserve = 0);
private:
diff --git a/src/qjsonparser.cpp b/src/qjsonparser.cpp
index df23ae3..2e70f86 100644
--- a/src/qjsonparser.cpp
+++ b/src/qjsonparser.cpp
@@ -163,7 +163,7 @@ QtJson::QJsonDocument QJsonParser::parse()
// fill in Header data
Private::Header *h = (Private::Header *)data;
- h->tag = QJsonDocument::Tag;
+ h->tag = QJsonDocument::BinaryFormatTag;
h->version = 1u;
current = sizeof(Private::Header);
@@ -353,7 +353,7 @@ bool QJsonParser::parseValue(Private::Value *val, int baseOffset)
if (*json++ == 'r' &&
*json++ == 'u' &&
*json++ == 'e') {
- val->type = QJsonValue::Boolean;
+ val->type = QJsonValue::Bool;
val->val = true;
DEBUG << "value: true";
END;
@@ -367,7 +367,7 @@ bool QJsonParser::parseValue(Private::Value *val, int baseOffset)
*json++ == 'l' &&
*json++ == 's' &&
*json++ == 'e') {
- val->type = QJsonValue::Boolean;
+ val->type = QJsonValue::Bool;
val->val = false;
DEBUG << "value: false";
END;
diff --git a/src/qjsonvalue.cpp b/src/qjsonvalue.cpp
index f5b29f8..25dfebf 100644
--- a/src/qjsonvalue.cpp
+++ b/src/qjsonvalue.cpp
@@ -68,7 +68,7 @@ QJsonValue::QJsonValue(Private::Data *data, Private::Base *base, const Private::
case Null:
dbl = 0;
break;
- case Boolean:
+ case Bool:
b = v.toBoolean();
break;
case Number:
@@ -91,7 +91,7 @@ QJsonValue::QJsonValue(Private::Data *data, Private::Base *base, const Private::
}
QJsonValue::QJsonValue(bool b)
- : t(Boolean), d(0)
+ : t(Bool), d(0)
{
this->b = b;
}
@@ -220,7 +220,7 @@ QJsonValue QJsonValue::fromVariant(const QVariant &variant)
QVariant QJsonValue::toVariant() const
{
switch (t) {
- case Boolean:
+ case Bool:
return b;
case Number:
return dbl;
@@ -280,7 +280,7 @@ void QJsonValue::setValue(const QJsonObject &o)
bool QJsonValue::toBool() const
{
- if (t != Boolean)
+ if (t != Bool)
return false;
return b;
}
@@ -332,7 +332,7 @@ bool QJsonValue::operator==(const QJsonValue &other) const
case Undefined:
case Null:
break;
- case Boolean:
+ case Bool:
return b == other.b;
case Number:
return dbl == other.dbl;
@@ -384,7 +384,7 @@ int QJsonValue::requiredStorage(bool *compressed) const
return base ? base->size : sizeof(Private::Base);
case Undefined:
case Null:
- case Boolean:
+ case Bool:
break;
}
return 0;
@@ -396,7 +396,7 @@ uint QJsonValue::valueToStore(uint offset) const
case Undefined:
case Null:
break;
- case Boolean:
+ case Bool:
return b;
case Number: {
int c = Private::compressedNumber(dbl);
diff --git a/src/qjsonvalue.h b/src/qjsonvalue.h
index 9028c59..6b677b4 100644
--- a/src/qjsonvalue.h
+++ b/src/qjsonvalue.h
@@ -52,7 +52,7 @@ class Q_JSON_EXPORT QJsonValue {
public:
enum Type {
Null = 0x0,
- Boolean = 0x1,
+ Bool = 0x1,
Number = 0x2,
String = 0x3,
Array = 0x4,
@@ -78,8 +78,15 @@ public:
QVariant toVariant() const;
Type type() const;
+ inline bool isNull() const { return type() == Null; }
+ inline bool isBool() const { return type() == Bool; }
+ inline bool isNumber() const { return type() == Number; }
+ inline bool isString() const { return type() == String; }
+ inline bool isArray() const { return type() == Array; }
+ inline bool isObject() const { return type() == Object; }
inline bool isUndefined() const { return type() == Undefined; }
+ // ### remove!
void setValue(bool);
void setValue(double);
void setValue(int);
@@ -98,6 +105,7 @@ public:
bool operator==(const QJsonValue &other) const;
bool operator!=(const QJsonValue &other) const;
+ // ### make private
void detach();
private:
@@ -108,10 +116,16 @@ private:
friend class QJsonArray;
friend class QJsonObject;
QJsonValue(Private::Data *d, Private::Base *b, const Private::Value& v);
+
+ // ### These should move to Private::Value
int requiredStorage(bool *compressed) const;
uint valueToStore(uint offset) const;
void copyData(char *dest, bool compressed) const;
+ // ### This should be:
+ // Private::Data *d;
+ // Private::Base *b;
+ // Private::Value *v;
Type t;
Private::Data *d; // needed for Objects and Arrays
union {
diff --git a/src/qjsonwriter.cpp b/src/qjsonwriter.cpp
index cea366f..b678cf3 100644
--- a/src/qjsonwriter.cpp
+++ b/src/qjsonwriter.cpp
@@ -179,7 +179,7 @@ static void valueToJson(const Private::Base *b, const Private::Value &v, QByteAr
{
QJsonValue::Type type = (QJsonValue::Type)(uint)v.type;
switch (type) {
- case QJsonValue::Boolean:
+ case QJsonValue::Bool:
json += v.toBoolean() ? "true" : "false";
break;
case QJsonValue::Number: