~drizzle-trunk/drizzle/development

« back to all changes in this revision

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