]> git.cworth.org Git - apitrace/blob - thirdparty/qjson/serializer.h
Update bundled QJson
[apitrace] / thirdparty / qjson / serializer.h
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 class QIODevice;
27 class QString;
28 class QVariant;
29
30 namespace QJson {
31   /**
32   * @brief How the indentation should work.
33   *
34   * none (default) : { "foo" : 0, "foo1" : 1, "foo2" : [ { "foo3" : 3, "foo4" : 4 } ] }
35   *
36   * compact : {"foo":0,"foo1":1,"foo2":[{"foo3":3,"foo4":4}]}
37   *
38   * minimum : { "foo" : 0, "foo1" : 1, "foo2" : [
39   *             { "foo3" : 3, "foo4" : 4 }
40   *           ] }
41   *
42   * medium : {
43   *           "foo" : 0, "foo1" : 1, "foo2" : [
44   *            {
45   *             "foo3" : 3, "foo4" : 4
46   *            }
47   *           ]
48   *          }
49   * full : {
50   *         "foo" : 0,
51   *         "foo1" : 1,
52   *         "foo2" : [
53   *          {
54   *           "foo3" : 3,
55   *           "foo4" : 4
56   *          }
57   *         ]
58   *        }
59   */
60   enum IndentMode {
61     IndentNone,
62     IndentCompact,
63     IndentMinimum,
64     IndentMedium,
65     IndentFull
66   };
67   /**
68   * @brief Main class used to convert QVariant objects to JSON data.
69   *
70   * QVariant objects are converted to a string containing the JSON data.
71   * If QVariant object is empty or not valid a <em>null</em> json object is returned.
72   */
73   class QJSON_EXPORT Serializer {
74   public:
75     Serializer();
76     ~Serializer();
77
78      /**
79       * This method generates a textual JSON representation and outputs it to the
80       * passed in I/O Device.
81       * @param variant The JSON document in its in-memory representation as generated by the
82       * parser.
83       * @param out Input output device
84       * @param ok if a conversion error occurs, *ok is set to false; otherwise *ok is set to true
85       */
86     void serialize( const QVariant& variant, QIODevice* out, bool* ok = 0);
87
88     /**
89       * This is a method provided for convenience. It turns the passed in in-memory
90       * representation of the JSON document into a textual one, which is returned.
91       * If the returned string is empty, the document was empty. If it was null, there
92       * was a parsing error.
93       *
94       * @param variant The JSON document in its in-memory representation as generated by the
95       * parser.
96       */
97
98     QByteArray serialize( const QVariant& variant);
99
100     /**
101      * Allow or disallow writing of NaN and/or Infinity (as an extension to QJson)
102      */
103     void allowSpecialNumbers(bool allow);
104
105     /**
106      * Is Nan and/or Infinity allowed?
107      */
108     bool specialNumbersAllowed() const;
109
110     /**
111      * set output indentation mode as defined in QJson::IndentMode
112      */
113     void setIndentMode(IndentMode mode = QJson::IndentNone);
114
115
116     /**
117     * set double precision used while converting Double
118     * \sa QByteArray::number
119     */
120     void setDoublePrecision(int precision);
121
122     /**
123      * Returns one of the indentation modes defined in QJson::IndentMode
124      */
125     IndentMode indentMode() const;
126
127   private:
128     Q_DISABLE_COPY(Serializer)
129     class SerializerPrivate;
130     SerializerPrivate* const d;
131   };
132 }
133
134 #endif // QJSON_SERIALIZER_H