~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Mark Atwood
  • Date: 2011-08-11 03:05:03 UTC
  • mfrom: (2385.1.12 refactor4)
  • Revision ID: me@mark.atwood.name-20110811030503-rp9xjihc5x3y0x4q
mergeĀ lp:~olafvdspek/drizzle/refactor4

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
      virtual ~FastWriter(){}
 
70
 
 
71
      void enableYAMLCompatibility();
 
72
 
 
73
   public: // overridden from Writer
 
74
      virtual std::string write( const Value &root );
 
75
 
 
76
   private:
 
77
      void writeValue( const Value &value );
 
78
 
 
79
      std::string document_;
 
80
      bool yamlCompatiblityEnabled_;
 
81
   };
 
82
 
 
83
   /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way.
 
84
    *
 
85
    * The rules for line break and indent are as follow:
 
86
    * - Object value:
 
87
    *     - if empty then print {} without indent and line break
 
88
    *     - if not empty the print '{', line break & indent, print one value per line
 
89
    *       and then unindent and line break and print '}'.
 
90
    * - Array value:
 
91
    *     - if empty then print [] without indent and line break
 
92
    *     - if the array contains no object value, empty array or some other value types,
 
93
    *       and all the values fit on one lines, then print the array on a single line.
 
94
    *     - otherwise, it the values do not fit on one line, or the array contains
 
95
    *       object or non empty array, then print one value per line.
 
96
    *
 
97
    * If the Value have comments then they are outputed according to their #CommentPlacement.
 
98
    *
 
99
    * \sa Reader, Value, Value::setComment()
 
100
    */
 
101
   class JSON_API StyledWriter: public Writer
 
102
   {
 
103
   public:
 
104
      StyledWriter();
 
105
      virtual ~StyledWriter(){}
 
106
 
 
107
   public: // overridden from Writer
 
108
      /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
 
109
       * \param root Value to serialize.
 
110
       * \return String containing the JSON document that represents the root value.
 
111
       */
 
112
      virtual std::string write( const Value &root );
 
113
 
 
114
   private:
 
115
      void writeValue( const Value &value );
 
116
      void writeArrayValue( const Value &value );
 
117
      bool isMultineArray( const Value &value );
 
118
      void pushValue( const std::string &value );
 
119
      void writeIndent();
 
120
      void writeWithIndent( const std::string &value );
 
121
      void indent();
 
122
      void unindent();
 
123
      void writeCommentBeforeValue( const Value &root );
 
124
      void writeCommentAfterValueOnSameLine( const Value &root );
 
125
      bool hasCommentForValue( const Value &value );
 
126
      static std::string normalizeEOL( const std::string &text );
 
127
 
 
128
      typedef std::vector<std::string> ChildValues;
 
129
 
 
130
      ChildValues childValues_;
 
131
      std::string document_;
 
132
      std::string indentString_;
 
133
      int rightMargin_;
 
134
      int indentSize_;
 
135
      bool addChildValues_;
 
136
   };
 
137
 
 
138
   /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way,
 
139
        to a stream rather than to a string.
 
140
    *
 
141
    * The rules for line break and indent are as follow:
 
142
    * - Object value:
 
143
    *     - if empty then print {} without indent and line break
 
144
    *     - if not empty the print '{', line break & indent, print one value per line
 
145
    *       and then unindent and line break and print '}'.
 
146
    * - Array value:
 
147
    *     - if empty then print [] without indent and line break
 
148
    *     - if the array contains no object value, empty array or some other value types,
 
149
    *       and all the values fit on one lines, then print the array on a single line.
 
150
    *     - otherwise, it the values do not fit on one line, or the array contains
 
151
    *       object or non empty array, then print one value per line.
 
152
    *
 
153
    * If the Value have comments then they are outputed according to their #CommentPlacement.
 
154
    *
 
155
    * \param indentation Each level will be indented by this amount extra.
 
156
    * \sa Reader, Value, Value::setComment()
 
157
    */
 
158
   class JSON_API StyledStreamWriter
 
159
   {
 
160
   public:
 
161
      StyledStreamWriter( std::string indentation="\t" );
 
162
      ~StyledStreamWriter(){}
 
163
 
 
164
   public:
 
165
      /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
 
166
       * \param out Stream to write to. (Can be ostringstream, e.g.)
 
167
       * \param root Value to serialize.
 
168
       * \note There is no point in deriving from Writer, since write() should not return a value.
 
169
       */
 
170
      void write( std::ostream &out, const Value &root );
 
171
 
 
172
   private:
 
173
      void writeValue( const Value &value );
 
174
      void writeArrayValue( const Value &value );
 
175
      bool isMultineArray( const Value &value );
 
176
      void pushValue( const std::string &value );
 
177
      void writeIndent();
 
178
      void writeWithIndent( const std::string &value );
 
179
      void indent();
 
180
      void unindent();
 
181
      void writeCommentBeforeValue( const Value &root );
 
182
      void writeCommentAfterValueOnSameLine( const Value &root );
 
183
      bool hasCommentForValue( const Value &value );
 
184
      static std::string normalizeEOL( const std::string &text );
 
185
 
 
186
      typedef std::vector<std::string> ChildValues;
 
187
 
 
188
      ChildValues childValues_;
 
189
      std::ostream* document_;
 
190
      std::string indentString_;
 
191
      int rightMargin_;
 
192
      std::string indentation_;
 
193
      bool addChildValues_;
 
194
   };
 
195
 
 
196
   std::string JSON_API valueToString( Int value );
 
197
   std::string JSON_API valueToString( UInt value );
 
198
   std::string JSON_API valueToString( double value );
 
199
   std::string JSON_API valueToString( bool value );
 
200
   std::string JSON_API valueToQuotedString( const char *value );
 
201
 
 
202
   /// \brief Output using the StyledStreamWriter.
 
203
   /// \see Json::operator>>()
 
204
   std::ostream& operator<<( std::ostream&, const Value &root );
 
205
 
 
206
} // namespace Json