~ubuntu-branches/ubuntu/quantal/drizzle/quantal

« back to all changes in this revision

Viewing changes to plugin/json_server/json/writer.h

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-06-19 10:46:49 UTC
  • mfrom: (1.2.11) (2.1.16 sid)
  • Revision ID: package-import@ubuntu.com-20120619104649-9ij634mxm4x8pp4l
Tags: 1:7.1.36-stable-1ubuntu1
* Merge from Debian unstable. (LP: #987575)
  Remaining changes:
  - Added upstart script.
* debian/drizzle.upstart: dropped logger since upstart logs job
  output now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
2
 * 
 
3
 *  JSON Library, originally from http://jsoncpp.sourceforge.net/
 
4
 *
 
5
 *  Copyright (C) 2011 Stewart Smith
 
6
 *  All rights reserved.
 
7
 *
 
8
 *  Redistribution and use in source and binary forms, with or without
 
9
 *  modification, are permitted provided that the following conditions are
 
10
 *  met:
 
11
 *
 
12
 *      * Redistributions of source code must retain the above copyright
 
13
 *  notice, this list of conditions and the following disclaimer.
 
14
 *
 
15
 *      * Redistributions in binary form must reproduce the above
 
16
 *  copyright notice, this list of conditions and the following disclaimer
 
17
 *  in the documentation and/or other materials provided with the
 
18
 *  distribution.
 
19
 *
 
20
 *      * The names of its contributors may not be used to endorse or
 
21
 *  promote products derived from this software without specific prior
 
22
 *  written permission.
 
23
 *
 
24
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
25
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
26
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
27
 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
28
 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
29
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
30
 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
31
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
32
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
33
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
34
 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
35
 *
 
36
 */
 
37
 
 
38
#pragma once
 
39
 
 
40
# include "value.h"
 
41
# include <vector>
 
42
# include <string>
 
43
# include <iosfwd>
 
44
 
 
45
namespace Json {
 
46
 
 
47
   class Value;
 
48
 
 
49
   /** \brief Abstract class for writers.
 
50
    */
 
51
   class JSON_API Writer
 
52
   {
 
53
   public:
 
54
      virtual ~Writer();
 
55
 
 
56
      virtual std::string write( const Value &root ) = 0;
 
57
   };
 
58
 
 
59
   /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format without formatting (not human friendly).
 
60
    *
 
61
    * The JSON document is written in a single line. It is not intended for 'human' consumption,
 
62
    * but may be usefull to support feature such as RPC where bandwith is limited.
 
63
    * \sa Reader, Value
 
64
    */
 
65
   class JSON_API FastWriter : public Writer
 
66
   {
 
67
   public:
 
68
      FastWriter();
 
69
 
 
70
      void enableYAMLCompatibility();
 
71
 
 
72
   public: // overridden from Writer
 
73
      virtual std::string write( const Value &root );
 
74
 
 
75
   private:
 
76
      void writeValue( const Value &value );
 
77
 
 
78
      std::string document_;
 
79
      bool yamlCompatiblityEnabled_;
 
80
   };
 
81
 
 
82
   /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way.
 
83
    *
 
84
    * The rules for line break and indent are as follow:
 
85
    * - Object value:
 
86
    *     - if empty then print {} without indent and line break
 
87
    *     - if not empty the print '{', line break & indent, print one value per line
 
88
    *       and then unindent and line break and print '}'.
 
89
    * - Array value:
 
90
    *     - if empty then print [] without indent and line break
 
91
    *     - if the array contains no object value, empty array or some other value types,
 
92
    *       and all the values fit on one lines, then print the array on a single line.
 
93
    *     - otherwise, it the values do not fit on one line, or the array contains
 
94
    *       object or non empty array, then print one value per line.
 
95
    *
 
96
    * If the Value have comments then they are outputed according to their #CommentPlacement.
 
97
    *
 
98
    * \sa Reader, Value, Value::setComment()
 
99
    */
 
100
   class JSON_API StyledWriter: public Writer
 
101
   {
 
102
   public:
 
103
      StyledWriter();
 
104
 
 
105
   public: // overridden from Writer
 
106
      /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
 
107
       * \param root Value to serialize.
 
108
       * \return String containing the JSON document that represents the root value.
 
109
       */
 
110
      virtual std::string write( const Value &root );
 
111
 
 
112
   private:
 
113
      void writeValue( const Value &value );
 
114
      void writeArrayValue( const Value &value );
 
115
      bool isMultineArray( const Value &value );
 
116
      void pushValue( const std::string &value );
 
117
      void writeIndent();
 
118
      void writeWithIndent( const std::string &value );
 
119
      void indent();
 
120
      void unindent();
 
121
      void writeCommentBeforeValue( const Value &root );
 
122
      void writeCommentAfterValueOnSameLine( const Value &root );
 
123
      bool hasCommentForValue( const Value &value );
 
124
      static std::string normalizeEOL( const std::string &text );
 
125
 
 
126
      typedef std::vector<std::string> ChildValues;
 
127
 
 
128
      ChildValues childValues_;
 
129
      std::string document_;
 
130
      std::string indentString_;
 
131
      int rightMargin_;
 
132
      int indentSize_;
 
133
      bool addChildValues_;
 
134
   };
 
135
 
 
136
   /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way,
 
137
        to a stream rather than to a string.
 
138
    *
 
139
    * The rules for line break and indent are as follow:
 
140
    * - Object value:
 
141
    *     - if empty then print {} without indent and line break
 
142
    *     - if not empty the print '{', line break & indent, print one value per line
 
143
    *       and then unindent and line break and print '}'.
 
144
    * - Array value:
 
145
    *     - if empty then print [] without indent and line break
 
146
    *     - if the array contains no object value, empty array or some other value types,
 
147
    *       and all the values fit on one lines, then print the array on a single line.
 
148
    *     - otherwise, it the values do not fit on one line, or the array contains
 
149
    *       object or non empty array, then print one value per line.
 
150
    *
 
151
    * If the Value have comments then they are outputed according to their #CommentPlacement.
 
152
    *
 
153
    * \param indentation Each level will be indented by this amount extra.
 
154
    * \sa Reader, Value, Value::setComment()
 
155
    */
 
156
   class JSON_API StyledStreamWriter
 
157
   {
 
158
   public:
 
159
      StyledStreamWriter( std::string indentation="\t" );
 
160
      ~StyledStreamWriter(){}
 
161
 
 
162
   public:
 
163
      /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
 
164
       * \param out Stream to write to. (Can be ostringstream, e.g.)
 
165
       * \param root Value to serialize.
 
166
       * \note There is no point in deriving from Writer, since write() should not return a value.
 
167
       */
 
168
      void write( std::ostream &out, const Value &root );
 
169
 
 
170
   private:
 
171
      void writeValue( const Value &value );
 
172
      void writeArrayValue( const Value &value );
 
173
      bool isMultineArray( const Value &value );
 
174
      void pushValue( const std::string &value );
 
175
      void writeIndent();
 
176
      void writeWithIndent( const std::string &value );
 
177
      void indent();
 
178
      void unindent();
 
179
      void writeCommentBeforeValue( const Value &root );
 
180
      void writeCommentAfterValueOnSameLine( const Value &root );
 
181
      bool hasCommentForValue( const Value &value );
 
182
      static std::string normalizeEOL( const std::string &text );
 
183
 
 
184
      typedef std::vector<std::string> ChildValues;
 
185
 
 
186
      ChildValues childValues_;
 
187
      std::ostream* document_;
 
188
      std::string indentString_;
 
189
      int rightMargin_;
 
190
      std::string indentation_;
 
191
      bool addChildValues_;
 
192
   };
 
193
 
 
194
   std::string JSON_API valueToString( Int value );
 
195
   std::string JSON_API valueToString( UInt value );
 
196
   std::string JSON_API valueToString( double value );
 
197
   std::string JSON_API valueToString( bool value );
 
198
   std::string JSON_API valueToQuotedString( const char *value );
 
199
 
 
200
   /// \brief Output using the StyledStreamWriter.
 
201
   /// \see Json::operator>>()
 
202
   std::ostream& operator<<( std::ostream&, const Value &root );
 
203
 
 
204
} // namespace Json