Simple Json implementation: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Redirect to identical page)
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
[[Category:Snippets]]<br />[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]
#Redirect [[Simple JSON Implementation]]
 
= 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 &quot;QJson&amp;quot;: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 ==
 
<code>#ifndef JSONOBJECT_HPP<br />#define JSONOBJECT_HPP
 
#include &lt;QVariant&amp;gt;
 
typedef QVariantList JsonArray;
 
class JsonObject<br />{<br />public:<br /> static const QString null;<br /> static const QString True;<br /> static const QString False;
 
JsonObject(QString jsonData = &quot;&quot;);
 
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);
 
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;
 
bool isNull(const QString&amp;amp; id) const;
 
QVariantMap getRawData() const;<br />protected:<br /> 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);
 
QVariantMap objectData;<br />};
 
#endif // JSONOBJECT_HPP<br /></code>
 
Here's the cpp code:
 
== JsonObject.cpp ==
 
<code>#include &quot;JsonObject.hpp&amp;quot;<br />#include &lt;QStringList&amp;gt;
 
QString JsonObject::toString() const<br />{<br /> QString result = &quot;{&quot;;<br /> QStringList keys = objectData.keys();<br /> for(int i = 0; i &lt; 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('&quot;');<br /> result.append(keys.at(i));<br /> result.append('&quot;');<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('&quot;');<br /> result.append(keys.at(i));<br /> result.append('&quot;');<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('&quot;');<br /> result.append(data.toString());<br /> result.append('&quot;');<br /> }<br /> result.append(',');<br /> }<br /> break;<br /> case QVariant::Int:<br /> {<br /> result.append('&quot;');<br /> result.append(keys.at(i));<br /> result.append('&quot;');<br /> result.append(':');<br /> result.append(QString(&quot;%1&amp;quot;).arg(data.toInt()));<br /> result.append(',');<br /> }<br /> break;<br /> case QVariant::Double:<br /> {<br /> result.append('&quot;');<br /> result.append(keys.at(i));<br /> result.append('&quot;');<br /> result.append(':');<br /> result.append(QString(&quot;%1&amp;quot;).arg(data.toDouble()));<br /> result.append(',');<br /> }<br /> break;<br /> case QVariant::LongLong:<br /> {<br /> result.append('&quot;');<br /> result.append(keys.at(i));<br /> result.append('&quot;');<br /> result.append(':');<br /> result.append(QString(&quot;%1&amp;quot;).arg(data.toLongLong()));<br /> result.append(',');<br /> }<br /> break;<br /> case QVariant::List:<br /> {<br /> result.append('&quot;');<br /> result.append(keys.at(i));<br /> result.append('&quot;');<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(&quot;}&quot;);<br /> return result;<br />}
<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('&quot;'))<br /> value.remove(0,1);<br /> if(value.endsWith('&quot;'))<br /> value.remove(value.length() - 1,1);<br /> objectData.insert(id,QVariant(value));<br />}
<br />void JsonObject::put(const QString&amp;amp; id,int value)<br />{<br /> objectData.insert(id,QVariant(value));<br />}
<br />void JsonObject::put(const QString&amp;amp; id,double value)<br />{<br /> objectData.insert(id,QVariant(value));<br />}
<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 />}
<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 />}
<br />QString JsonObject::getString(const QString&amp;amp; id, bool *ok) const<br />{<br /> QString result = &quot;&quot;;<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 />}
<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 />}
<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 />}
<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)
            return objectData.value(id).toString()  null;<br /> return false;<br /> }<br /> return true;<br />}
<br />QVariantMap JsonObject::getRawData() const<br />{<br /> return objectData;<br />}
<br />void JsonObject::setRawData(QVariantMap data)<br />{<br /> objectData = data;<br />}
<br />QString JsonObject::stringFromList(const JsonArray&amp;amp; array) const<br />{<br /> QString result = &quot;[&quot;;<br /> for(int i = 0; i &lt; 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('&quot;');<br /> result.append(data.toString());<br /> result.append('&quot;');<br /> }<br /> result.append(',');<br /> }<br /> break;<br /> case QVariant::Int:<br /> {<br /> result.append(QString(&quot;%1&amp;quot;).arg(data.toInt()));<br /> result.append(',');<br /> }<br /> break;<br /> case QVariant::Double:<br /> {<br /> result.append(QString(&quot;%1&amp;quot;).arg(data.toDouble()));<br /> result.append(',');<br /> }<br /> break;<br /> case QVariant::LongLong:<br /> {<br /> result.append(QString(&quot;%1&amp;quot;).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(&quot;]&quot;);<br /> return result;<br />}
<br />QString JsonObject::getKey(QString&amp;amp; data)<br />{<br /> QString key = &quot;&quot;;<br /> int index = data.indexOf(':');<br /> if(index &gt;= 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('&quot;'))<br /> key.remove(0,1);<br /> if(key.endsWith('&quot;'))<br /> key.remove(key.length() - 1,1);<br /> return key;<br />}
<br />QString JsonObject::getValue(QString&amp;amp; data)<br />{<br /> QString value = &quot;&quot;;<br /> if(data.startsWith('{'))//object<br /> {<br /> int index = data.indexOf('}');<br /> if(index &gt;= 0)<br /> {<br /> value.append(data.mid(0,index'' 1));<br /> data.remove(0,index + 1);<br /> while(value.count('{') &gt; value.count('}'))<br /> {<br /> index = data.indexOf('}');<br /> if(index &gt;= 0)<br /> {<br /> value.append(data.mid(0,index + 1));<br /> data.remove(0,index + 1);<br /> }<br /> else<br /> {<br /> value = &quot;&quot;;<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 &gt;= 0)<br /> {<br /> value.append(data.mid(0,index + 1));<br /> data.remove(0,index + 1);<br /> while(value.count('[') &gt; value.count(']'))<br /> {<br /> index = data.indexOf(']');<br /> if(index &gt;= 0)<br /> {<br /> value.append(data.mid(0,index + 1));<br /> data.remove(0,index + 1);<br /> }<br /> else<br /> {<br /> value = &quot;&quot;;<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 &gt;= 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 />}
 
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() &gt; 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('&quot;'))//String<br /> {<br /> value.remove(0,1);<br /> if(value.endsWith('&quot;'))<br /> value.remove(value.length() - 1,1);<br /> result.append(QVariant(value));<br /> }<br /> else if(value  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 />}
 
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() &gt; 0)<br /> {<br /> QString key = getKey(jsonData);<br /> QString value = getValue(jsonData);<br /> if(value.startsWith('{'))//Object<br /> this-&gt;put(key,JsonObject(value));<br /> else if(value.startsWith('['))//Array<br /> {<br /> JsonArray array = parseArray(value);<br /> this-&gt;put(key,array);<br /> }<br /> else if(value.startsWith('&quot;'))//String<br /> this-&gt;put(key,value);<br /> else if(value  null)
                this-&amp;gt;put(key,null);
            else if(value  True)<br /> this-&gt;put(key,True);<br /> else if(value == False)<br /> this-&gt;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-&gt;put(key,doubleValue);<br /> else<br /> this-&gt;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-&gt;put(key,intValue);<br /> else<br /> {<br /> qlonglong longValue = value.toLongLong(&amp;ok);<br /> if(ok)<br /> this-&gt;put(key,longValue);<br /> else<br /> this-&gt;put(key,0);<br /> }<br /> }<br /> }<br /> }<br /> }<br />}
 
const QString JsonObject::null = &quot;null&amp;quot;;<br />const QString JsonObject::True = &quot;true&amp;quot;;<br />const QString JsonObject::False = &quot;false&amp;quot;;

Latest revision as of 12:18, 28 November 2016