Simple JSON Implementation: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
[[Category:Snippets]]<br />[toc align_right="yes" depth="3"]
[[Category:Snippets]]
[toc align_right="yes" depth="3"]


= Simple JSON Implementation =
= Simple JSON Implementation =
Line 7: Line 8:
== JsonObject.hpp ==
== JsonObject.hpp ==


<code>#ifndef JSONOBJECT_HPP<br />#define JSONOBJECT_HPP
<code>#ifndef JSONOBJECT_HPP
#define JSONOBJECT_HPP


#include <QVariant>
#include <QVariant>
Line 13: Line 15:
typedef QVariantList JsonArray;
typedef QVariantList JsonArray;


class JsonObject<br />{<br />public:<br /> static const QString null;<br /> static const QString True;<br /> static const QString False;
class JsonObject
{
public:
static const QString null;
static const QString True;
static const QString False;


JsonObject(QString jsonData = "");
JsonObject(QString jsonData = "");
Line 19: Line 26:
QString toString() const;
QString toString() const;


void put(const QString&amp;amp; id,JsonObject obj);<br /> void put(const QString&amp;amp; id,QString value);<br /> void put(const QString&amp;amp; id,int value);<br /> void put(const QString&amp;amp; id,double value);<br /> void put(const QString&amp;amp; id,qlonglong value);<br /> void put(const QString&amp;amp; id,JsonArray value);
void put(const QString&amp;amp; id,JsonObject obj);
void put(const QString&amp;amp; id,QString value);
void put(const QString&amp;amp; id,int value);
void put(const QString&amp;amp; id,double value);
void put(const QString&amp;amp; id,qlonglong value);
void put(const QString&amp;amp; id,JsonArray value);


JsonObject getObject(const QString&amp;amp; id, bool *ok = NULL) const;<br /> QString getString(const QString&amp;amp; id, bool *ok = NULL) const;<br /> int getInt(const QString&amp;amp; id, bool *ok = NULL) const;<br /> double getDouble(const QString&amp;amp; id, bool *ok = NULL) const;<br /> qlonglong getLong(const QString&amp;amp; id, bool *ok = NULL) const;<br /> JsonArray getArray(const QString&amp;amp; id, bool *ok = NULL) const;
JsonObject getObject(const QString&amp;amp; id, bool *ok = NULL) const;
QString getString(const QString&amp;amp; id, bool *ok = NULL) const;
int getInt(const QString&amp;amp; id, bool *ok = NULL) const;
double getDouble(const QString&amp;amp; id, bool *ok = NULL) const;
qlonglong getLong(const QString&amp;amp; id, bool *ok = NULL) const;
JsonArray getArray(const QString&amp;amp; id, bool *ok = NULL) const;


bool isNull(const QString&amp;amp; id) const;
bool isNull(const QString&amp;amp; id) const;


QVariantMap getRawData() const;<br />protected:<br /> void setRawData(QVariantMap data);
QVariantMap getRawData() const;
protected:
void setRawData(QVariantMap data);


private:<br /> QString stringFromList(const JsonArray&amp;amp; array) const;<br /> QString getKey(QString&amp;amp; data);<br /> QString getValue(QString&amp;amp; data);<br /> JsonArray parseArray(QString data);
private:
QString stringFromList(const JsonArray&amp;amp; array) const;
QString getKey(QString&amp;amp; data);
QString getValue(QString&amp;amp; data);
JsonArray parseArray(QString data);


QVariantMap objectData;<br />};
QVariantMap objectData;
};


#endif // JSONOBJECT_HPP<br /></code>
#endif // JSONOBJECT_HPP
</code>


Here's the cpp code:
Here's the cpp code:
Line 37: Line 62:
== JsonObject.cpp ==
== JsonObject.cpp ==


<code>#include "JsonObject.hpp"<br />#include <QStringList>
<code>#include "JsonObject.hpp"
#include <QStringList>


QString JsonObject::toString() const<br />{<br /> QString result = "{";<br /> QStringList keys = objectData.keys();<br /> for(int i = 0; i < keys.count(); +''i)<br /> {<br /> QVariant data = objectData.value(keys.at(i));<br /> switch(data.type())<br /> {<br /> case QVariant::Map:<br /> {<br /> result.append('"');<br /> result.append(keys.at(i));<br /> result.append('"');<br /> result.append(':');<br /> JsonObject obj;<br /> obj.setRawData(data.toMap());<br /> result.append(obj.toString());<br /> result.append(',');<br /> }<br /> break;<br /> case QVariant::String:<br /> {<br /> result.append('"');<br /> result.append(keys.at(i));<br /> result.append('"');<br /> result.append(':');<br /> if(data.toString()  null || data.toString()  True<br /> || data.toString() == False)<br /> result.append(data.toString());<br /> else<br /> {<br /> result.append('"');<br /> result.append(data.toString());<br /> result.append('"');<br /> }<br /> result.append(',');<br /> }<br /> break;<br /> case QVariant::Int:<br /> {<br /> result.append('"');<br /> result.append(keys.at(i));<br /> result.append('"');<br /> result.append(':');<br /> result.append(QString("%1").arg(data.toInt()));<br /> result.append(',');<br /> }<br /> break;<br /> case QVariant::Double:<br /> {<br /> result.append('"');<br /> result.append(keys.at(i));<br /> result.append('"');<br /> result.append(':');<br /> result.append(QString("%1").arg(data.toDouble()));<br /> result.append(',');<br /> }<br /> break;<br /> case QVariant::LongLong:<br /> {<br /> result.append('"');<br /> result.append(keys.at(i));<br /> result.append('"');<br /> result.append(':');<br /> result.append(QString("%1").arg(data.toLongLong()));<br /> result.append(',');<br /> }<br /> break;<br /> case QVariant::List:<br /> {<br /> result.append('"');<br /> result.append(keys.at(i));<br /> result.append('"');<br /> result.append(':');<br /> result.append(stringFromList(data.toList()));<br /> result.append(',');<br /> }<br /> break;<br /> default:<br /> break;<br /> }<br /> }<br /> if(result.endsWith(','))<br /> result.remove(result.length() - 1,1);<br /> result.append("}");<br /> return result;<br />}
QString JsonObject::toString() const
<br />void JsonObject::put(const QString&amp;amp; id,JsonObject obj)<br />{<br /> objectData.insert(id,QVariant(obj.getRawData()));<br />}
{
<br />void JsonObject::put(const QString&amp;amp; id,QString value)<br />{<br /> if(value.startsWith('"'))<br /> value.remove(0,1);<br /> if(value.endsWith('"'))<br /> value.remove(value.length() - 1,1);<br /> objectData.insert(id,QVariant(value));<br />}
QString result = "{";
<br />void JsonObject::put(const QString&amp;amp; id,int value)<br />{<br /> objectData.insert(id,QVariant(value));<br />}
QStringList keys = objectData.keys();
<br />void JsonObject::put(const QString&amp;amp; id,double value)<br />{<br /> objectData.insert(id,QVariant(value));<br />}
for(int i = 0; i < keys.count(); +''i)
<br />void JsonObject::put(const QString&amp;amp; id,qlonglong value)<br />{<br /> objectData.insert(id,QVariant(value));<br />}
{
<br />void JsonObject::put(const QString&amp;amp; id,JsonArray value)<br />{<br /> objectData.insert(id,QVariant(value));<br />}
QVariant data = objectData.value(keys.at(i));
<br />JsonObject JsonObject::getObject(const QString&amp;amp; id, bool *ok) const<br />{<br /> JsonObject result;<br /> bool isOk = false;<br /> if(objectData.contains(id))<br /> {<br /> QVariant data = objectData.value(id);<br /> if(data.type() == QVariant::Map)<br /> {<br /> result.setRawData(data.toMap());<br /> isOk = true;<br /> }<br /> }<br /> if(ok != NULL)<br /> *ok = isOk;<br /> return result;<br />}
switch(data.type())
<br />QString JsonObject::getString(const QString&amp;amp; id, bool *ok) const<br />{<br /> QString result = "";<br /> bool isOk = false;<br /> if(objectData.contains(id))<br /> {<br /> QVariant data = objectData.value(id);<br /> if(data.type() == QVariant::String)<br /> {<br /> result = data.toString();<br /> isOk = true;<br /> }<br /> }<br /> if(ok != NULL)<br /> *ok = isOk;<br /> return result;<br />}
{
<br />int JsonObject::getInt(const QString&amp;amp; id, bool *ok) const<br />{<br /> int result = 0;<br /> bool isOk = false;<br /> if(objectData.contains(id))<br /> {<br /> QVariant data = objectData.value(id);<br /> if(data.type() == QVariant::Int)<br /> {<br /> result = data.toInt();<br /> isOk = true;<br /> }<br /> }<br /> if(ok != NULL)<br /> *ok = isOk;<br /> return result;<br />}
case QVariant::Map:
<br />double JsonObject::getDouble(const QString&amp;amp; id, bool *ok) const<br />{<br /> double result = 0.0;<br /> bool isOk = false;<br /> if(objectData.contains(id))<br /> {<br /> QVariant data = objectData.value(id);<br /> if(data.type() == QVariant::Double)<br /> {<br /> result = data.toDouble();<br /> isOk = true;<br /> }<br /> }<br /> if(ok != NULL)<br /> *ok = isOk;<br /> return result;<br />}
{
<br />qlonglong JsonObject::getLong(const QString&amp;amp; id, bool *ok) const<br />{<br /> qlonglong result = 0;<br /> bool isOk = false;<br /> if(objectData.contains(id))<br /> {<br /> QVariant data = objectData.value(id);<br /> if(data.type() == QVariant::LongLong)<br /> {<br /> result = data.toLongLong();<br /> isOk = true;<br /> }<br /> }<br /> if(ok != NULL)<br /> *ok = isOk;<br /> return result;<br />}
result.append('"');
<br />JsonArray JsonObject::getArray(const QString&amp;amp; id, bool *ok) const<br />{<br /> JsonArray result;<br /> bool isOk = false;<br /> if(objectData.contains(id))<br /> {<br /> QVariant data = objectData.value(id);<br /> if(data.type() == QVariant::List)<br /> {<br /> result = data.toList();<br /> isOk = true;<br /> }<br /> }<br /> if(ok != NULL)<br /> *ok = isOk;<br /> return result;<br />}
result.append(keys.at(i));
<br />bool JsonObject::isNull(const QString&amp;amp; id) const<br />{<br /> if(objectData.contains(id))<br /> {<br /> if(objectData.value(id).type()  QVariant::String)
result.append('"');
            return objectData.value(id).toString()  null;<br /> return false;<br /> }<br /> return true;<br />}
result.append(':');
<br />QVariantMap JsonObject::getRawData() const<br />{<br /> return objectData;<br />}
JsonObject obj;
<br />void JsonObject::setRawData(QVariantMap data)<br />{<br /> objectData = data;<br />}
obj.setRawData(data.toMap());
<br />QString JsonObject::stringFromList(const JsonArray&amp;amp; array) const<br />{<br /> QString result = "[";<br /> for(int i = 0; i < array.count();i)<br /> {<br /> QVariant data = array.at(i);<br /> switch(data.type())<br /> {<br /> case QVariant::Map:<br /> {<br /> JsonObject obj;<br /> obj.setRawData(data.toMap());<br /> result.append(obj.toString());<br /> result.append(',');<br /> }<br /> break;<br /> case QVariant::String:<br /> {<br /> if(data.toString()  null || data.toString()  False || data.toString() == True)<br /> result.append(data.toString());<br /> else<br /> {<br /> result.append('"');<br /> result.append(data.toString());<br /> result.append('"');<br /> }<br /> result.append(',');<br /> }<br /> break;<br /> case QVariant::Int:<br /> {<br /> result.append(QString("%1").arg(data.toInt()));<br /> result.append(',');<br /> }<br /> break;<br /> case QVariant::Double:<br /> {<br /> result.append(QString("%1").arg(data.toDouble()));<br /> result.append(',');<br /> }<br /> break;<br /> case QVariant::LongLong:<br /> {<br /> result.append(QString("%1").arg(data.toLongLong()));<br /> result.append(',');<br /> }<br /> break;<br /> case QVariant::List:<br /> {<br /> result.append(stringFromList(data.toList()));<br /> result.append(',');<br /> }<br /> break;<br /> default:<br /> break;<br /> }<br /> }<br /> if(result.endsWith(','))<br /> result.remove(result.length() - 1,1);<br /> result.append("]");<br /> return result;<br />}
result.append(obj.toString());
<br />QString JsonObject::getKey(QString&amp;amp; data)<br />{<br /> QString key = "";<br /> int index = data.indexOf(':');<br /> if(index >= 0)<br /> {<br /> key = data.mid(0,index);<br /> data.remove(0,index);<br /> if(data.startsWith(':'))<br /> data.remove(0,1);<br /> }<br /> if(key.startsWith('"'))<br /> key.remove(0,1);<br /> if(key.endsWith('"'))<br /> key.remove(key.length() - 1,1);<br /> return key;<br />}
result.append(',');
<br />QString JsonObject::getValue(QString&amp;amp; data)<br />{<br /> QString value = "";<br /> if(data.startsWith('{'))//object<br /> {<br /> int index = data.indexOf('}');<br /> if(index >= 0)<br /> {<br /> value.append(data.mid(0,index'' 1));<br /> data.remove(0,index + 1);<br /> while(value.count('{') > value.count('}'))<br /> {<br /> index = data.indexOf('}');<br /> if(index >= 0)<br /> {<br /> value.append(data.mid(0,index + 1));<br /> data.remove(0,index + 1);<br /> }<br /> else<br /> {<br /> value = "";<br /> break;<br /> }<br /> }<br /> if(data.startsWith(','))<br /> data.remove(0,1);<br /> }<br /> }<br /> else if(data.startsWith('['))//Array<br /> {<br /> int index = data.indexOf(']');<br /> if(index >= 0)<br /> {<br /> value.append(data.mid(0,index + 1));<br /> data.remove(0,index + 1);<br /> while(value.count('[') > value.count(']'))<br /> {<br /> index = data.indexOf(']');<br /> if(index >= 0)<br /> {<br /> value.append(data.mid(0,index + 1));<br /> data.remove(0,index + 1);<br /> }<br /> else<br /> {<br /> value = "";<br /> break;<br /> }<br /> }<br /> if(data.startsWith(','))<br /> data.remove(0,1);<br /> }<br /> }<br /> else<br /> {<br /> int index = data.indexOf(',');<br /> if(index >= 0)<br /> {<br /> value = data.mid(0,index);<br /> data.remove(0,index);<br /> if(data.startsWith(','))<br /> data.remove(0,1);<br /> }<br /> else if(!data.contains(','))<br /> {<br /> value = data;<br /> data.clear();<br /> }<br /> }<br /> return value;<br />}
}
break;
case QVariant::String:
{
result.append('"');
result.append(keys.at(i));
result.append('"');
result.append(':');
if(data.toString()  null || data.toString()  True
|| data.toString() == False)
result.append(data.toString());
else
{
result.append('"');
result.append(data.toString());
result.append('"');
}
result.append(',');
}
break;
case QVariant::Int:
{
result.append('"');
result.append(keys.at(i));
result.append('"');
result.append(':');
result.append(QString("%1").arg(data.toInt()));
result.append(',');
}
break;
case QVariant::Double:
{
result.append('"');
result.append(keys.at(i));
result.append('"');
result.append(':');
result.append(QString("%1").arg(data.toDouble()));
result.append(',');
}
break;
case QVariant::LongLong:
{
result.append('"');
result.append(keys.at(i));
result.append('"');
result.append(':');
result.append(QString("%1").arg(data.toLongLong()));
result.append(',');
}
break;
case QVariant::List:
{
result.append('"');
result.append(keys.at(i));
result.append('"');
result.append(':');
result.append(stringFromList(data.toList()));
result.append(',');
}
break;
default:
break;
}
}
if(result.endsWith(','))
result.remove(result.length() - 1,1);
result.append("}");
return result;
}


JsonArray JsonObject::parseArray(QString data)<br />{<br /> JsonArray result;<br /> if(data.startsWith('[') &amp;&amp; data.endsWith(']'))<br /> {<br /> data.remove(0,1);<br /> data.remove(data.length() - 1,1);<br /> while(data.length() > 0)<br /> {<br /> QString value = getValue(data);<br /> if(value.startsWith('{'))//Object<br /> {<br /> JsonObject obj(value);<br /> result.append(QVariant(obj.getRawData()));<br /> }<br /> else if(value.startsWith('['))//Array<br /> {<br /> JsonArray array = parseArray(value);<br /> result.append(QVariant(array));<br /> }<br /> else if(value.startsWith('"'))//String<br /> {<br /> value.remove(0,1);<br /> if(value.endsWith('"'))<br /> value.remove(value.length() - 1,1);<br /> result.append(QVariant(value));<br /> }<br /> else if(value  null)
void JsonObject::put(const QString&amp;amp; id,JsonObject obj)
{
objectData.insert(id,QVariant(obj.getRawData()));
}
 
void JsonObject::put(const QString&amp;amp; id,QString value)
{
if(value.startsWith('"'))
value.remove(0,1);
if(value.endsWith('"'))
value.remove(value.length() - 1,1);
objectData.insert(id,QVariant(value));
}
 
void JsonObject::put(const QString&amp;amp; id,int value)
{
objectData.insert(id,QVariant(value));
}
 
void JsonObject::put(const QString&amp;amp; id,double value)
{
objectData.insert(id,QVariant(value));
}
 
void JsonObject::put(const QString&amp;amp; id,qlonglong value)
{
objectData.insert(id,QVariant(value));
}
 
void JsonObject::put(const QString&amp;amp; id,JsonArray value)
{
objectData.insert(id,QVariant(value));
}
 
JsonObject JsonObject::getObject(const QString&amp;amp; id, bool *ok) const
{
JsonObject result;
bool isOk = false;
if(objectData.contains(id))
{
QVariant data = objectData.value(id);
if(data.type() == QVariant::Map)
{
result.setRawData(data.toMap());
isOk = true;
}
}
if(ok != NULL)
*ok = isOk;
return result;
}
 
QString JsonObject::getString(const QString&amp;amp; id, bool *ok) const
{
QString result = "";
bool isOk = false;
if(objectData.contains(id))
{
QVariant data = objectData.value(id);
if(data.type() == QVariant::String)
{
result = data.toString();
isOk = true;
}
}
if(ok != NULL)
*ok = isOk;
return result;
}
 
int JsonObject::getInt(const QString&amp;amp; id, bool *ok) const
{
int result = 0;
bool isOk = false;
if(objectData.contains(id))
{
QVariant data = objectData.value(id);
if(data.type() == QVariant::Int)
{
result = data.toInt();
isOk = true;
}
}
if(ok != NULL)
*ok = isOk;
return result;
}
 
double JsonObject::getDouble(const QString&amp;amp; id, bool *ok) const
{
double result = 0.0;
bool isOk = false;
if(objectData.contains(id))
{
QVariant data = objectData.value(id);
if(data.type() == QVariant::Double)
{
result = data.toDouble();
isOk = true;
}
}
if(ok != NULL)
*ok = isOk;
return result;
}
 
qlonglong JsonObject::getLong(const QString&amp;amp; id, bool *ok) const
{
qlonglong result = 0;
bool isOk = false;
if(objectData.contains(id))
{
QVariant data = objectData.value(id);
if(data.type() == QVariant::LongLong)
{
result = data.toLongLong();
isOk = true;
}
}
if(ok != NULL)
*ok = isOk;
return result;
}
 
JsonArray JsonObject::getArray(const QString&amp;amp; id, bool *ok) const
{
JsonArray result;
bool isOk = false;
if(objectData.contains(id))
{
QVariant data = objectData.value(id);
if(data.type() == QVariant::List)
{
result = data.toList();
isOk = true;
}
}
if(ok != NULL)
*ok = isOk;
return result;
}
 
bool JsonObject::isNull(const QString&amp;amp; id) const
{
if(objectData.contains(id))
{
if(objectData.value(id).type()  QVariant::String)
            return objectData.value(id).toString()  null;
return false;
}
return true;
}
 
QVariantMap JsonObject::getRawData() const
{
return objectData;
}
 
void JsonObject::setRawData(QVariantMap data)
{
objectData = data;
}
 
QString JsonObject::stringFromList(const JsonArray&amp;amp; array) const
{
QString result = "[";
for(int i = 0; i < array.count();i)
{
QVariant data = array.at(i);
switch(data.type())
{
case QVariant::Map:
{
JsonObject obj;
obj.setRawData(data.toMap());
result.append(obj.toString());
result.append(',');
}
break;
case QVariant::String:
{
if(data.toString()  null || data.toString()  False || data.toString() == True)
result.append(data.toString());
else
{
result.append('"');
result.append(data.toString());
result.append('"');
}
result.append(',');
}
break;
case QVariant::Int:
{
result.append(QString("%1").arg(data.toInt()));
result.append(',');
}
break;
case QVariant::Double:
{
result.append(QString("%1").arg(data.toDouble()));
result.append(',');
}
break;
case QVariant::LongLong:
{
result.append(QString("%1").arg(data.toLongLong()));
result.append(',');
}
break;
case QVariant::List:
{
result.append(stringFromList(data.toList()));
result.append(',');
}
break;
default:
break;
}
}
if(result.endsWith(','))
result.remove(result.length() - 1,1);
result.append("]");
return result;
}
 
QString JsonObject::getKey(QString&amp;amp; data)
{
QString key = "";
int index = data.indexOf(':');
if(index >= 0)
{
key = data.mid(0,index);
data.remove(0,index);
if(data.startsWith(':'))
data.remove(0,1);
}
if(key.startsWith('"'))
key.remove(0,1);
if(key.endsWith('"'))
key.remove(key.length() - 1,1);
return key;
}
 
QString JsonObject::getValue(QString&amp;amp; data)
{
QString value = "";
if(data.startsWith('{'))//object
{
int index = data.indexOf('}');
if(index >= 0)
{
value.append(data.mid(0,index'' 1));
data.remove(0,index + 1);
while(value.count('{') > value.count('}'))
{
index = data.indexOf('}');
if(index >= 0)
{
value.append(data.mid(0,index + 1));
data.remove(0,index + 1);
}
else
{
value = "";
break;
}
}
if(data.startsWith(','))
data.remove(0,1);
}
}
else if(data.startsWith('['))//Array
{
int index = data.indexOf(']');
if(index >= 0)
{
value.append(data.mid(0,index + 1));
data.remove(0,index + 1);
while(value.count('[') > value.count(']'))
{
index = data.indexOf(']');
if(index >= 0)
{
value.append(data.mid(0,index + 1));
data.remove(0,index + 1);
}
else
{
value = "";
break;
}
}
if(data.startsWith(','))
data.remove(0,1);
}
}
else
{
int index = data.indexOf(',');
if(index >= 0)
{
value = data.mid(0,index);
data.remove(0,index);
if(data.startsWith(','))
data.remove(0,1);
}
else if(!data.contains(','))
{
value = data;
data.clear();
}
}
return value;
}
 
JsonArray JsonObject::parseArray(QString data)
{
JsonArray result;
if(data.startsWith('[') &amp;&amp; data.endsWith(']'))
{
data.remove(0,1);
data.remove(data.length() - 1,1);
while(data.length() > 0)
{
QString value = getValue(data);
if(value.startsWith('{'))//Object
{
JsonObject obj(value);
result.append(QVariant(obj.getRawData()));
}
else if(value.startsWith('['))//Array
{
JsonArray array = parseArray(value);
result.append(QVariant(array));
}
else if(value.startsWith('"'))//String
{
value.remove(0,1);
if(value.endsWith('"'))
value.remove(value.length() - 1,1);
result.append(QVariant(value));
}
else if(value  null)
                 result.append(QVariant(null));
                 result.append(QVariant(null));
             else if(value  True)<br /> result.append(QVariant(True));<br /> else if(value == False)<br /> result.append(QVariant(False));<br /> else//Number<br /> {<br /> if(value.contains('.'))//double<br /> {<br /> bool ok;<br /> double doubleValue = value.toDouble(&amp;ok);<br /> if(ok)<br /> result.append(QVariant(doubleValue));<br /> else<br /> result.append(QVariant(0.0));<br /> }<br /> else//int || long<br /> {<br /> bool ok;<br /> int intValue = value.toInt(&amp;ok);<br /> if(ok)<br /> result.append(QVariant(intValue));<br /> else<br /> {<br /> qlonglong longValue = value.toLongLong(&amp;ok);<br /> if(ok)<br /> result.append(QVariant(longValue));<br /> else<br /> result.append(QVariant(0));<br /> }<br /> }<br /> }<br /> }<br /> }<br /> return result;<br />}
             else if(value  True)
result.append(QVariant(True));
else if(value == False)
result.append(QVariant(False));
else//Number
{
if(value.contains('.'))//double
{
bool ok;
double doubleValue = value.toDouble(&amp;ok);
if(ok)
result.append(QVariant(doubleValue));
else
result.append(QVariant(0.0));
}
else//int || long
{
bool ok;
int intValue = value.toInt(&amp;ok);
if(ok)
result.append(QVariant(intValue));
else
{
qlonglong longValue = value.toLongLong(&amp;ok);
if(ok)
result.append(QVariant(longValue));
else
result.append(QVariant(0));
}
}
}
}
}
return result;
}


JsonObject::JsonObject(QString jsonData)<br />{<br /> if(jsonData.startsWith('{') &amp;&amp; jsonData.endsWith('}'))<br /> {<br /> jsonData.remove(0,1);<br /> jsonData.remove(jsonData.length() - 1,1);<br /> while(jsonData.length() > 0)<br /> {<br /> QString key = getKey(jsonData);<br /> QString value = getValue(jsonData);<br /> if(value.startsWith('{'))//Object<br /> this->put(key,JsonObject(value));<br /> else if(value.startsWith('['))//Array<br /> {<br /> JsonArray array = parseArray(value);<br /> this->put(key,array);<br /> }<br /> else if(value.startsWith('"'))//String<br /> this->put(key,value);<br /> else if(value  null)
JsonObject::JsonObject(QString jsonData)
{
if(jsonData.startsWith('{') &amp;&amp; jsonData.endsWith('}'))
{
jsonData.remove(0,1);
jsonData.remove(jsonData.length() - 1,1);
while(jsonData.length() > 0)
{
QString key = getKey(jsonData);
QString value = getValue(jsonData);
if(value.startsWith('{'))//Object
this->put(key,JsonObject(value));
else if(value.startsWith('['))//Array
{
JsonArray array = parseArray(value);
this->put(key,array);
}
else if(value.startsWith('"'))//String
this->put(key,value);
else if(value  null)
                 this->put(key,null);
                 this->put(key,null);
             else if(value  True)<br /> this->put(key,True);<br /> else if(value == False)<br /> this->put(key,False);<br /> else//Number<br /> {<br /> if(value.contains('.'))//double<br /> {<br /> bool ok;<br /> double doubleValue = value.toDouble(&amp;ok);<br /> if(ok)<br /> this->put(key,doubleValue);<br /> else<br /> this->put(key,0.0);<br /> }<br /> else//int || long<br /> {<br /> bool ok;<br /> int intValue = value.toInt(&amp;ok);<br /> if(ok)<br /> this->put(key,intValue);<br /> else<br /> {<br /> qlonglong longValue = value.toLongLong(&amp;ok);<br /> if(ok)<br /> this->put(key,longValue);<br /> else<br /> this->put(key,0);<br /> }<br /> }<br /> }<br /> }<br /> }<br />}
             else if(value  True)
this->put(key,True);
else if(value == False)
this->put(key,False);
else//Number
{
if(value.contains('.'))//double
{
bool ok;
double doubleValue = value.toDouble(&amp;ok);
if(ok)
this->put(key,doubleValue);
else
this->put(key,0.0);
}
else//int || long
{
bool ok;
int intValue = value.toInt(&amp;ok);
if(ok)
this->put(key,intValue);
else
{
qlonglong longValue = value.toLongLong(&amp;ok);
if(ok)
this->put(key,longValue);
else
this->put(key,0);
}
}
}
}
}
}


const QString JsonObject::null = "null";<br />const QString JsonObject::True = "true";<br />const QString JsonObject::False = "false";
const QString JsonObject::null = "null";
const QString JsonObject::True = "true";
const QString JsonObject::False = "false";

Revision as of 12:07, 25 February 2015

[toc align_right="yes" depth="3"]

Simple JSON Implementation

The reason I made this (except for the fun of it) is that I found a couple of people had issues with incorporating "QJson":http://qjson.sourceforge.net/ in their projects (be it what it may). I reserve no copyrights to this implementation so you are all welcome to ctrl+c ctrl+v and do as you see fit with it.

JsonObject.hpp

#ifndef JSONOBJECT_HPP
#define JSONOBJECT_HPP

#include <QVariant>

typedef QVariantList JsonArray;

class JsonObject
{
public:
 static const QString null;
 static const QString True;
 static const QString False;

JsonObject(QString jsonData = "");

QString toString() const;

void put(const QString&amp;amp; id,JsonObject obj);
 void put(const QString&amp;amp; id,QString value);
 void put(const QString&amp;amp; id,int value);
 void put(const QString&amp;amp; id,double value);
 void put(const QString&amp;amp; id,qlonglong value);
 void put(const QString&amp;amp; id,JsonArray value);

JsonObject getObject(const QString&amp;amp; id, bool *ok = NULL) const;
 QString getString(const QString&amp;amp; id, bool *ok = NULL) const;
 int getInt(const QString&amp;amp; id, bool *ok = NULL) const;
 double getDouble(const QString&amp;amp; id, bool *ok = NULL) const;
 qlonglong getLong(const QString&amp;amp; id, bool *ok = NULL) const;
 JsonArray getArray(const QString&amp;amp; id, bool *ok = NULL) const;

bool isNull(const QString&amp;amp; id) const;

QVariantMap getRawData() const;
protected:
 void setRawData(QVariantMap data);

private:
 QString stringFromList(const JsonArray&amp;amp; array) const;
 QString getKey(QString&amp;amp; data);
 QString getValue(QString&amp;amp; data);
 JsonArray parseArray(QString data);

QVariantMap objectData;
};

#endif // JSONOBJECT_HPP

Here's the cpp code:

JsonObject.cpp

#include "JsonObject.hpp"

  1. include <QStringList>

QString JsonObject::toString() const {

QString result = "{";
QStringList keys = objectData.keys();
for(int i = 0; i < keys.count(); +i)
{
QVariant data = objectData.value(keys.at(i));
switch(data.type())
{
case QVariant::Map:
{
result.append('"');
result.append(keys.at(i));
result.append('"');
result.append(':');
JsonObject obj;
obj.setRawData(data.toMap());
result.append(obj.toString());
result.append(',');
}
break;
case QVariant::String:
{
result.append('"');
result.append(keys.at(i));
result.append('"');
result.append(':');
if(data.toString()  null || data.toString()  True
|| data.toString() == False)
result.append(data.toString());
else
{
result.append('"');
result.append(data.toString());
result.append('"');
}
result.append(',');
}
break;
case QVariant::Int:
{
result.append('"');
result.append(keys.at(i));
result.append('"');
result.append(':');
result.append(QString("%1").arg(data.toInt()));
result.append(',');
}
break;
case QVariant::Double:
{
result.append('"');
result.append(keys.at(i));
result.append('"');
result.append(':');
result.append(QString("%1").arg(data.toDouble()));
result.append(',');
}
break;
case QVariant::LongLong:
{
result.append('"');
result.append(keys.at(i));
result.append('"');
result.append(':');
result.append(QString("%1").arg(data.toLongLong()));
result.append(',');
}
break;
case QVariant::List:
{
result.append('"');
result.append(keys.at(i));
result.append('"');
result.append(':');
result.append(stringFromList(data.toList()));
result.append(',');
}
break;
default:
break;
}
}
if(result.endsWith(','))
result.remove(result.length() - 1,1);
result.append("}");
return result;

}

void JsonObject::put(const QString&amp; id,JsonObject obj) {

objectData.insert(id,QVariant(obj.getRawData()));

}

void JsonObject::put(const QString&amp; id,QString value) {

if(value.startsWith('"'))
value.remove(0,1);
if(value.endsWith('"'))
value.remove(value.length() - 1,1);
objectData.insert(id,QVariant(value));

}

void JsonObject::put(const QString&amp; id,int value) {

objectData.insert(id,QVariant(value));

}

void JsonObject::put(const QString&amp; id,double value) {

objectData.insert(id,QVariant(value));

}

void JsonObject::put(const QString&amp; id,qlonglong value) {

objectData.insert(id,QVariant(value));

}

void JsonObject::put(const QString&amp; id,JsonArray value) {

objectData.insert(id,QVariant(value));

}

JsonObject JsonObject::getObject(const QString&amp; id, bool *ok) const {

JsonObject result;
bool isOk = false;
if(objectData.contains(id))
{
QVariant data = objectData.value(id);
if(data.type() == QVariant::Map)
{
result.setRawData(data.toMap());
isOk = true;
}
}
if(ok != NULL)
*ok = isOk;
return result;

}

QString JsonObject::getString(const QString&amp; id, bool *ok) const {

QString result = "";
bool isOk = false;
if(objectData.contains(id))
{
QVariant data = objectData.value(id);
if(data.type() == QVariant::String)
{
result = data.toString();
isOk = true;
}
}
if(ok != NULL)
*ok = isOk;
return result;

}

int JsonObject::getInt(const QString&amp; id, bool *ok) const {

int result = 0;
bool isOk = false;
if(objectData.contains(id))
{
QVariant data = objectData.value(id);
if(data.type() == QVariant::Int)
{
result = data.toInt();
isOk = true;
}
}
if(ok != NULL)
*ok = isOk;
return result;

}

double JsonObject::getDouble(const QString&amp; id, bool *ok) const {

double result = 0.0;
bool isOk = false;
if(objectData.contains(id))
{
QVariant data = objectData.value(id);
if(data.type() == QVariant::Double)
{
result = data.toDouble();
isOk = true;
}
}
if(ok != NULL)
*ok = isOk;
return result;

}

qlonglong JsonObject::getLong(const QString&amp; id, bool *ok) const {

qlonglong result = 0;
bool isOk = false;
if(objectData.contains(id))
{
QVariant data = objectData.value(id);
if(data.type() == QVariant::LongLong)
{
result = data.toLongLong();
isOk = true;
}
}
if(ok != NULL)
*ok = isOk;
return result;

}

JsonArray JsonObject::getArray(const QString&amp; id, bool *ok) const {

JsonArray result;
bool isOk = false;
if(objectData.contains(id))
{
QVariant data = objectData.value(id);
if(data.type() == QVariant::List)
{
result = data.toList();
isOk = true;
}
}
if(ok != NULL)
*ok = isOk;
return result;

}

bool JsonObject::isNull(const QString&amp; id) const {

if(objectData.contains(id))
{
if(objectData.value(id).type()  QVariant::String)
           return objectData.value(id).toString()  null;
return false;
}
return true;

}

QVariantMap JsonObject::getRawData() const {

return objectData;

}

void JsonObject::setRawData(QVariantMap data) {

objectData = data;

}

QString JsonObject::stringFromList(const JsonArray&amp; array) const {

QString result = "[";
for(int i = 0; i < array.count();i)
{
QVariant data = array.at(i);
switch(data.type())
{
case QVariant::Map:
{
JsonObject obj;
obj.setRawData(data.toMap());
result.append(obj.toString());
result.append(',');
}
break;
case QVariant::String:
{
if(data.toString()  null || data.toString()  False || data.toString() == True)
result.append(data.toString());
else
{
result.append('"');
result.append(data.toString());
result.append('"');
}
result.append(',');
}
break;
case QVariant::Int:
{
result.append(QString("%1").arg(data.toInt()));
result.append(',');
}
break;
case QVariant::Double:
{
result.append(QString("%1").arg(data.toDouble()));
result.append(',');
}
break;
case QVariant::LongLong:
{
result.append(QString("%1").arg(data.toLongLong()));
result.append(',');
}
break;
case QVariant::List:
{
result.append(stringFromList(data.toList()));
result.append(',');
}
break;
default:
break;
}
}
if(result.endsWith(','))
result.remove(result.length() - 1,1);
result.append("]");
return result;

}

QString JsonObject::getKey(QString&amp; data) {

QString key = "";
int index = data.indexOf(':');
if(index >= 0)
{
key = data.mid(0,index);
data.remove(0,index);
if(data.startsWith(':'))
data.remove(0,1);
}
if(key.startsWith('"'))
key.remove(0,1);
if(key.endsWith('"'))
key.remove(key.length() - 1,1);
return key;

}

QString JsonObject::getValue(QString&amp; data) {

QString value = "";
if(data.startsWith('{'))//object
{
int index = data.indexOf('}');
if(index >= 0)
{
value.append(data.mid(0,index 1));
data.remove(0,index + 1);
while(value.count('{') > value.count('}'))
{
index = data.indexOf('}');
if(index >= 0)
{
value.append(data.mid(0,index + 1));
data.remove(0,index + 1);
}
else
{
value = "";
break;
}
}
if(data.startsWith(','))
data.remove(0,1);
}
}
else if(data.startsWith('['))//Array
{
int index = data.indexOf(']');
if(index >= 0)
{
value.append(data.mid(0,index + 1));
data.remove(0,index + 1);
while(value.count('[') > value.count(']'))
{
index = data.indexOf(']');
if(index >= 0)
{
value.append(data.mid(0,index + 1));
data.remove(0,index + 1);
}
else
{
value = "";
break;
}
}
if(data.startsWith(','))
data.remove(0,1);
}
}
else
{
int index = data.indexOf(',');
if(index >= 0)
{
value = data.mid(0,index);
data.remove(0,index);
if(data.startsWith(','))
data.remove(0,1);
}
else if(!data.contains(','))
{
value = data;
data.clear();
}
}
return value;

}

JsonArray JsonObject::parseArray(QString data) {

JsonArray result;
if(data.startsWith('[') && data.endsWith(']'))
{
data.remove(0,1);
data.remove(data.length() - 1,1);
while(data.length() > 0)
{
QString value = getValue(data);
if(value.startsWith('{'))//Object
{
JsonObject obj(value);
result.append(QVariant(obj.getRawData()));
}
else if(value.startsWith('['))//Array
{
JsonArray array = parseArray(value);
result.append(QVariant(array));
}
else if(value.startsWith('"'))//String
{
value.remove(0,1);
if(value.endsWith('"'))
value.remove(value.length() - 1,1);
result.append(QVariant(value));
}
else if(value  null)
               result.append(QVariant(null));
           else if(value  True)
result.append(QVariant(True));
else if(value == False)
result.append(QVariant(False));
else//Number
{
if(value.contains('.'))//double
{
bool ok;
double doubleValue = value.toDouble(&ok);
if(ok)
result.append(QVariant(doubleValue));
else
result.append(QVariant(0.0));
}
else//int || long
{
bool ok;
int intValue = value.toInt(&ok);
if(ok)
result.append(QVariant(intValue));
else
{
qlonglong longValue = value.toLongLong(&ok);
if(ok)
result.append(QVariant(longValue));
else
result.append(QVariant(0));
}
}
}
}
}
return result;

}

JsonObject::JsonObject(QString jsonData) {

if(jsonData.startsWith('{') && jsonData.endsWith('}'))
{
jsonData.remove(0,1);
jsonData.remove(jsonData.length() - 1,1);
while(jsonData.length() > 0)
{
QString key = getKey(jsonData);
QString value = getValue(jsonData);
if(value.startsWith('{'))//Object
this->put(key,JsonObject(value));
else if(value.startsWith('['))//Array
{
JsonArray array = parseArray(value);
this->put(key,array);
}
else if(value.startsWith('"'))//String
this->put(key,value);
else if(value  null)
               this->put(key,null);
           else if(value  True)
this->put(key,True);
else if(value == False)
this->put(key,False);
else//Number
{
if(value.contains('.'))//double
{
bool ok;
double doubleValue = value.toDouble(&ok);
if(ok)
this->put(key,doubleValue);
else
this->put(key,0.0);
}
else//int || long
{
bool ok;
int intValue = value.toInt(&ok);
if(ok)
this->put(key,intValue);
else
{
qlonglong longValue = value.toLongLong(&ok);
if(ok)
this->put(key,longValue);
else
this->put(key,0);
}
}
}
}
}

}

const QString JsonObject::null = "null"; const QString JsonObject::True = "true"; const QString JsonObject::False = "false";