~drizzle-trunk/drizzle/development

2295.1.1 by Brian Aker
Fix up stewart JSON patch.
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
2283.4.3 by Stewart Smith
add JSON library to json_server, fix up for building in our tree, and add simple /0.1/version JSON response as well as latest_api_version snippet to /
39
#ifndef CPPTL_JSON_READER_H_INCLUDED
40
# define CPPTL_JSON_READER_H_INCLUDED
41
42
# include "features.h"
43
# include "value.h"
44
# include <deque>
45
# include <stack>
46
# include <string>
47
# include <iostream>
48
49
namespace Json {
50
51
   /** \brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a Value.
52
    *
53
    */
54
   class JSON_API Reader
55
   {
56
   public:
57
      typedef char Char;
58
      typedef const Char *Location;
59
60
      /** \brief Constructs a Reader allowing all features
61
       * for parsing.
62
       */
63
      Reader();
64
65
      /** \brief Constructs a Reader allowing the specified feature set
66
       * for parsing.
67
       */
68
      Reader( const Features &features );
69
70
      /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document.
71
       * \param document UTF-8 encoded string containing the document to read.
72
       * \param root [out] Contains the root value of the document if it was
73
       *             successfully parsed.
74
       * \param collectComments \c true to collect comment and allow writing them back during
75
       *                        serialization, \c false to discard comments.
76
       *                        This parameter is ignored if Features::allowComments_
77
       *                        is \c false.
78
       * \return \c true if the document was successfully parsed, \c false if an error occurred.
79
       */
80
      bool parse( const std::string &document, 
81
                  Value &root,
82
                  bool collectComments = true );
83
84
      /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document.
85
       * \param document UTF-8 encoded string containing the document to read.
86
       * \param root [out] Contains the root value of the document if it was
87
       *             successfully parsed.
88
       * \param collectComments \c true to collect comment and allow writing them back during
89
       *                        serialization, \c false to discard comments.
90
       *                        This parameter is ignored if Features::allowComments_
91
       *                        is \c false.
92
       * \return \c true if the document was successfully parsed, \c false if an error occurred.
93
       */
94
      bool parse( const char *beginDoc, const char *endDoc, 
95
                  Value &root,
96
                  bool collectComments = true );
97
98
      /// \brief Parse from input stream.
99
      /// \see Json::operator>>(std::istream&, Json::Value&).
100
      bool parse( std::istream &is,
101
                  Value &root,
102
                  bool collectComments = true );
103
104
      /** \brief Returns a user friendly string that list errors in the parsed document.
105
       * \return Formatted error message with the list of errors with their location in 
106
       *         the parsed document. An empty string is returned if no error occurred
107
       *         during parsing.
108
       */
109
      std::string getFormatedErrorMessages() const;
110
111
   private:
112
      enum TokenType
113
      {
114
         tokenEndOfStream = 0,
115
         tokenObjectBegin,
116
         tokenObjectEnd,
117
         tokenArrayBegin,
118
         tokenArrayEnd,
119
         tokenString,
120
         tokenNumber,
121
         tokenTrue,
122
         tokenFalse,
123
         tokenNull,
124
         tokenArraySeparator,
125
         tokenMemberSeparator,
126
         tokenComment,
127
         tokenError
128
      };
129
130
      class Token
131
      {
132
      public:
133
         TokenType type_;
134
         Location start_;
135
         Location end_;
136
      };
137
138
      class ErrorInfo
139
      {
140
      public:
141
         Token token_;
142
         std::string message_;
143
         Location extra_;
144
      };
145
146
      typedef std::deque<ErrorInfo> Errors;
147
148
      bool expectToken( TokenType type, Token &token, const char *message );
149
      bool readToken( Token &token );
150
      void skipSpaces();
151
      bool match( Location pattern, 
152
                  int patternLength );
153
      bool readComment();
154
      bool readCStyleComment();
155
      bool readCppStyleComment();
156
      bool readString();
157
      void readNumber();
158
      bool readValue();
159
      bool readObject( Token &token );
160
      bool readArray( Token &token );
161
      bool decodeNumber( Token &token );
162
      bool decodeString( Token &token );
163
      bool decodeString( Token &token, std::string &decoded );
164
      bool decodeDouble( Token &token );
165
      bool decodeUnicodeCodePoint( Token &token, 
166
                                   Location &current, 
167
                                   Location end, 
168
                                   unsigned int &unicode );
169
      bool decodeUnicodeEscapeSequence( Token &token, 
170
                                        Location &current, 
171
                                        Location end, 
172
                                        unsigned int &unicode );
173
      bool addError( const std::string &message, 
174
                     Token &token,
175
                     Location extra = 0 );
176
      bool recoverFromError( TokenType skipUntilToken );
177
      bool addErrorAndRecover( const std::string &message, 
178
                               Token &token,
179
                               TokenType skipUntilToken );
180
      void skipUntilSpace();
181
      Value &currentValue();
182
      Char getNextChar();
183
      void getLocationLineAndColumn( Location location,
184
                                     int &line,
185
                                     int &column ) const;
186
      std::string getLocationLineAndColumn( Location location ) const;
187
      void addComment( Location begin, 
188
                       Location end, 
189
                       CommentPlacement placement );
190
      void skipCommentTokens( Token &token );
191
   
192
      typedef std::stack<Value *> Nodes;
193
      Nodes nodes_;
194
      Errors errors_;
195
      std::string document_;
196
      Location begin_;
197
      Location end_;
198
      Location current_;
199
      Location lastValueEnd_;
200
      Value *lastValue_;
201
      std::string commentsBefore_;
202
      Features features_;
203
      bool collectComments_;
204
   };
205
206
   /** \brief Read from 'sin' into 'root'.
207
208
    Always keep comments from the input JSON.
209
210
    This can be used to read a file into a particular sub-object.
211
    For example:
212
    \code
213
    Json::Value root;
214
    cin >> root["dir"]["file"];
215
    cout << root;
216
    \endcode
217
    Result:
218
    \verbatim
219
    {
220
	"dir": {
221
	    "file": {
222
		// The input stream JSON would be nested here.
223
	    }
224
	}
225
    }
226
    \endverbatim
227
    \throw std::exception on parse error.
228
    \see Json::operator<<()
229
   */
230
   std::istream& operator>>( std::istream&, Value& );
231
232
} // namespace Json
233
234
#endif // CPPTL_JSON_READER_H_INCLUDED