1 /* This file is part of qjson 2 * 3 * Copyright (C) 2009 Till Adam <adam@kde.org> 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License version 2.1, as published by the Free Software Foundation. 8 * 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with this library; see the file COPYING.LIB. If not, write to 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 * Boston, MA 02110-1301, USA. 19 */ 20 21 #ifndef QJSON_SERIALIZER_H 22 #define QJSON_SERIALIZER_H 23 24 #include "qjson_export.h" 25 26 QT_BEGIN_NAMESPACE 27 class QIODevice; 28 class QString; 29 class QVariant; 30 QT_END_NAMESPACE 31 32 namespace QJson { 33 /** 34 @brief How the indentation should work. 35 \verbatim 36 none (default) : 37 { "foo" : 0, "foo1" : 1, "foo2" : [ { "bar" : 1, "foo" : 0, "foobar" : 0 }, { "bar" : 1, "foo" : 1, "foobar" : 1 } ], "foo3" : [ 1, 2, 3, 4, 5, 6 ] } 38 39 compact : 40 {"foo":0,"foo1":1,"foo2":[{"bar":1,"foo":0,"foobar":0},{"bar":1,"foo":1,"foobar":1}],"foo3":[1,2,3,4,5,6]} 41 42 minimum : 43 { "foo" : 0, "foo1" : 1, "foo2" : [ 44 { "bar" : 1, "foo" : 0, "foobar" : 0 }, 45 { "bar" : 1, "foo" : 1, "foobar" : 1 } 46 ], "foo3" : [ 47 1, 48 2, 49 3, 50 4, 51 5, 52 6 53 ] } 54 55 medium : 56 { 57 "foo" : 0, "foo1" : 1, "foo2" : [ 58 { 59 "bar" : 1, "foo" : 0, "foobar" : 0 60 }, 61 { 62 "bar" : 1, "foo" : 1, "foobar" : 1 63 } 64 ], "foo3" : [ 65 1, 66 2, 67 3, 68 4, 69 5, 70 6 71 ] 72 } 73 74 full : 75 { 76 "foo" : 0, 77 "foo1" : 1, 78 "foo2" : [ 79 { 80 "bar" : 1, 81 "foo" : 0, 82 "foobar" : 0 83 }, 84 { 85 "bar" : 1, 86 "foo" : 1, 87 "foobar" : 1 88 } 89 ], 90 "foo3" : [ 91 1, 92 2, 93 3, 94 4, 95 5, 96 6 97 ] 98 } 99 100 101 \endverbatim 102 */ 103 enum IndentMode { 104 IndentNone, 105 IndentCompact, 106 IndentMinimum, 107 IndentMedium, 108 IndentFull 109 }; 110 /** 111 * @brief Main class used to convert QVariant objects to JSON data. 112 * 113 * QVariant objects are converted to a string containing the JSON data. 114 * 115 * 116 * Usage: 117 * 118 * \code 119 * QVariantList people; 120 * 121 * QVariantMap bob; 122 * bob.insert("Name", "Bob"); 123 * bob.insert("Phonenumber", 123); 124 * 125 * QVariantMap alice; 126 * alice.insert("Name", "Alice"); 127 * alice.insert("Phonenumber", 321); 128 * 129 * people << bob << alice; 130 * 131 * QJson::Serializer serializer; 132 * bool ok; 133 * QByteArray json = serializer.serialize(people, &ok); 134 * 135 * if (ok) { 136 * qDebug() << json; 137 * } else { 138 * qCritical() << "Something went wrong:" << serializer.errorMessage(); 139 * } 140 * \endcode 141 * 142 * The output will be: 143 * 144 * \code 145 * "[ { "Name" : "Bob", "Phonenumber" : 123 }, 146 * { "Name" : "Alice", "Phonenumber" : 321 } ]" 147 * \endcode 148 * 149 * It's possible to tune the indentation level of the resulting string. \sa setIndentMode 150 */ 151 class QJSON_EXPORT Serializer { 152 public: 153 Serializer(); 154 ~Serializer(); 155 156 /** 157 * This method generates a textual JSON representation and outputs it to the 158 * passed in I/O Device. 159 * @param variant The JSON document in its in-memory representation as generated by the 160 * parser. 161 * @param out Input output device 162 * @param ok if a conversion error occurs, *ok is set to false; otherwise *ok is set to true 163 */ 164 void serialize( const QVariant& variant, QIODevice* out, bool* ok); 165 166 /** 167 * This is a method provided for convenience. It turns the passed in in-memory 168 * representation of the JSON document into a textual one, which is returned. 169 * If the returned string is empty, the document was empty. If it was null, there 170 * was a parsing error. 171 * 172 * @param variant The JSON document in its in-memory representation as generated by the 173 * parser. 174 * 175 * \deprecated This method is going to be removed with the next major release of QJson. 176 */ 177 QByteArray serialize( const QVariant& variant); 178 179 /** 180 * This is a method provided for convenience. It turns the passed in in-memory 181 * representation of the JSON document into a textual one, which is returned. 182 * If the returned string is empty, the document was empty. If it was null, there 183 * was a parsing error. 184 * 185 * @param variant The JSON document in its in-memory representation as generated by the 186 * parser. 187 * @param ok if a conversion error occurs, *ok is set to false; otherwise *ok is set to true 188 */ 189 QByteArray serialize( const QVariant& variant, bool *ok); 190 191 /** 192 * Allow or disallow writing of NaN and/or Infinity (as an extension to QJson) 193 */ 194 void allowSpecialNumbers(bool allow); 195 196 /** 197 * Is Nan and/or Infinity allowed? 198 */ 199 bool specialNumbersAllowed() const; 200 201 /** 202 * set output indentation mode as defined in QJson::IndentMode 203 */ 204 void setIndentMode(IndentMode mode = QJson::IndentNone); 205 206 207 /** 208 * set double precision used while converting Double 209 * \sa QByteArray::number 210 */ 211 void setDoublePrecision(int precision); 212 213 /** 214 * Returns one of the indentation modes defined in QJson::IndentMode 215 */ 216 IndentMode indentMode() const; 217 218 /** 219 * Returns the error message 220 */ 221 QString errorMessage() const; 222 223 private: 224 Q_DISABLE_COPY(Serializer) 225 class SerializerPrivate; 226 SerializerPrivate* const d; 227 }; 228 } 229 230 #endif // QJSON_SERIALIZER_H 231