1 /* Copyright 2014, Uwe L. Korn <uwelk@xhochy.com>
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in all
11 * copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 * SOFTWARE.
20 */
21
22 #include "Json.h"
23
24 // Qt version specific includes
25 #if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
26 #include <QJsonDocument>
27 #include <QMetaProperty>
28 #else
29 #include <qjson/parser.h>
30 #include <qjson/qobjecthelper.h>
31 #include <qjson/serializer.h>
32 #endif
33
34 namespace QJsonWrapper
35 {
36
37 QVariantMap
qobject2qvariant(const QObject * object)38 qobject2qvariant( const QObject* object )
39 {
40 #if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
41 QVariantMap map;
42 if ( object == NULL )
43 {
44 return map;
45 }
46
47 const QMetaObject* metaObject = object->metaObject();
48 for ( int i = 0; i < metaObject->propertyCount(); ++i )
49 {
50 QMetaProperty metaproperty = metaObject->property( i );
51 if ( metaproperty.isReadable() )
52 {
53 map[ QLatin1String( metaproperty.name() ) ] = object->property( metaproperty.name() );
54 }
55 }
56 return map;
57 #else
58 return QJson::QObjectHelper::qobject2qvariant( object );
59 #endif
60 }
61
62
63 void
qvariant2qobject(const QVariantMap & variant,QObject * object)64 qvariant2qobject( const QVariantMap& variant, QObject* object )
65 {
66 #if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
67 for ( QVariantMap::const_iterator iter = variant.begin(); iter != variant.end(); ++iter )
68 {
69 QVariant property = object->property( iter.key().toLatin1() );
70 Q_ASSERT( property.isValid() );
71 if ( property.isValid() )
72 {
73 QVariant value = iter.value();
74 if ( value.canConvert( property.type() ) )
75 {
76 value.convert( property.type() );
77 object->setProperty( iter.key().toLatin1(), value );
78 } else if ( QString( QLatin1String("QVariant") ).compare( QLatin1String( property.typeName() ) ) == 0 ) {
79 object->setProperty( iter.key().toLatin1(), value );
80 }
81 }
82 }
83 #else
84 QJson::QObjectHelper::qvariant2qobject( variant, object );
85 #endif
86 }
87
88
89 QVariant
parseJson(const QByteArray & jsonData,bool * ok)90 parseJson( const QByteArray& jsonData, bool* ok )
91 {
92 #if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
93 QJsonParseError error;
94 QJsonDocument doc = QJsonDocument::fromJson( jsonData, &error );
95 if ( ok != NULL )
96 {
97 *ok = ( error.error == QJsonParseError::NoError );
98 }
99 return doc.toVariant();
100 #else
101 QJson::Parser p;
102 return p.parse( jsonData, ok );
103 #endif
104 }
105
106
107 QByteArray
toJson(const QVariant & variant,bool * ok)108 toJson( const QVariant &variant, bool* ok )
109 {
110 #if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
111 QJsonDocument doc = QJsonDocument::fromVariant( variant );
112 if ( ok != NULL )
113 {
114 *ok = !doc.isNull();
115 }
116 return doc.toJson( QJsonDocument::Compact );
117 #else
118 QJson::Serializer serializer;
119 QByteArray ret = serializer.serialize(variant);
120 if ( ok != NULL )
121 {
122 *ok = !ret.isNull();
123 }
124 return ret;
125 #endif
126 }
127
128 }
129