Version: 0.6.0
jsonreader.h
1 // Name: jsonreader.h
3 // Purpose: the parser of JSON text
4 // Author: Luciano Cattani
5 // Created: 2007/09/15
6 // RCS-ID: $Id: jsonreader.h,v 1.3 2008/03/03 19:05:45 luccat Exp $
7 // Copyright: (c) 2007 Luciano Cattani
8 // Licence: wxWidgets licence
10 
11 #if !defined( _WX_JSONREADER_H )
12 #define _WX_JSONREADER_H
13 
14 #ifdef __GNUG__
15  #pragma interface "jsonreader.h"
16 #endif
17 
18 // For compilers that support precompilation, includes "wx/wx.h".
19 #include "wx/wxprec.h"
20 
21 #ifdef __BORLANDC__
22  #pragma hdrstop
23 #endif
24 
25 // for all others, include the necessary headers (this file is usually all you
26 // need because it includes almost all "standard" wxWidgets headers)
27 #ifndef WX_PRECOMP
28  #include <wx/stream.h>
29  #include <wx/string.h>
30  #include <wx/arrstr.h>
31 #endif
32 
33 
34 #include "json_defs.h"
35 #include "jsonval.h"
36 
37 // The flags of the parser
38 enum {
39  wxJSONREADER_STRICT = 0,
40  wxJSONREADER_ALLOW_COMMENTS = 1,
41  wxJSONREADER_STORE_COMMENTS = 2,
42  wxJSONREADER_CASE = 4,
43  wxJSONREADER_MISSING = 8,
44  wxJSONREADER_MULTISTRING = 16,
45  wxJSONREADER_COMMENTS_AFTER = 32,
46  wxJSONREADER_NOUTF8_STREAM = 64,
47  wxJSONREADER_MEMORYBUFF = 128,
48 
49  wxJSONREADER_TOLERANT = wxJSONREADER_ALLOW_COMMENTS | wxJSONREADER_CASE |
50  wxJSONREADER_MISSING | wxJSONREADER_MULTISTRING,
51  wxJSONREADER_COMMENTS_BEFORE = wxJSONREADER_ALLOW_COMMENTS | wxJSONREADER_STORE_COMMENTS
52 };
53 
54 
55 class WXDLLIMPEXP_JSON wxJSONReader
56 {
57 public:
58  wxJSONReader( int flags = wxJSONREADER_TOLERANT, int maxErrors = 30 );
59  virtual ~wxJSONReader();
60 
61  int Parse( const wxString& doc, wxJSONValue* val );
62  int Parse( wxInputStream& doc, wxJSONValue* val );
63 
64  int GetDepth() const;
65  int GetErrorCount() const;
66  int GetWarningCount() const;
67  const wxArrayString& GetErrors() const;
68  const wxArrayString& GetWarnings() const;
69 
70  static int UTF8NumBytes( char ch );
71 
72 #if defined( wxJSON_64BIT_INT )
73  static bool Strtoll( const wxString& str, wxInt64* i64 );
74  static bool Strtoull( const wxString& str, wxUint64* ui64 );
75  static bool DoStrto_ll( const wxString& str, wxUint64* ui64, wxChar* sign );
76 #endif
77 
78 protected:
79 
80  int DoRead( wxInputStream& doc, wxJSONValue& val );
81  void AddError( const wxString& descr );
82  void AddError( const wxString& fmt, const wxString& str );
83  void AddError( const wxString& fmt, wxChar ch );
84  void AddWarning( int type, const wxString& descr );
85  int GetStart( wxInputStream& is );
86  int ReadChar( wxInputStream& is );
87  int PeekChar( wxInputStream& is );
88  void StoreValue( int ch, const wxString& key, wxJSONValue& value, wxJSONValue& parent );
89  int SkipWhiteSpace( wxInputStream& is );
90  int SkipComment( wxInputStream& is );
91  void StoreComment( const wxJSONValue* parent );
92  int ReadString( wxInputStream& is, wxJSONValue& val );
93  int ReadToken( wxInputStream& is, int ch, wxString& s );
94  int ReadValue( wxInputStream& is, int ch, wxJSONValue& val );
95  int ReadUES( wxInputStream& is, char* uesBuffer );
96  int AppendUES( wxMemoryBuffer& utf8Buff, const char* uesBuffer );
97  int NumBytes( char ch );
98  int ConvertCharByChar( wxString& s, const wxMemoryBuffer& utf8Buffer );
99  int ReadMemoryBuff( wxInputStream& is, wxJSONValue& val );
100 
102  int m_flags;
103 
106 
108  int m_lineNo;
109 
111  int m_colNo;
112 
114  int m_level;
115 
117  int m_depth;
118 
121 
124 
127 
129  wxString m_comment;
130 
133 
135  wxArrayString m_errors;
136 
138  wxArrayString m_warnings;
139 
142 
144  bool m_noUtf8;
145 };
146 
147 
148 #endif // not defined _WX_JSONREADER_H
149 
150 
wxJSONValue * m_lastStored
The pointer to the value object that was last stored.
Definition: jsonreader.h:123
int m_peekChar
The character read by the PeekChar() function (-1 none)
Definition: jsonreader.h:141
wxArrayString m_warnings
The array of warning messages.
Definition: jsonreader.h:138
int m_commentLine
The starting line of the comment string.
Definition: jsonreader.h:132
The JSON value class implementation.
Definition: jsonval.h:91
wxJSONValue * m_current
The pointer to the value object that is being read.
Definition: jsonreader.h:120
int m_lineNo
The current line number (start at 1).
Definition: jsonreader.h:108
bool m_noUtf8
ANSI: do not convert UTF-8 strings.
Definition: jsonreader.h:144
wxJSONValue * m_next
The pointer to the value object that will be read.
Definition: jsonreader.h:126
wxArrayString m_errors
The array of error messages.
Definition: jsonreader.h:135
int m_colNo
The current column number (start at 1).
Definition: jsonreader.h:111
int m_depth
The depth level of the read JSON text.
Definition: jsonreader.h:117
int m_flags
Flag that control the parser behaviour,.
Definition: jsonreader.h:102
int m_level
The current level of object/array annidation (start at ZERO).
Definition: jsonreader.h:114
The JSON parser.
Definition: jsonreader.h:55
int m_maxErrors
Maximum number of errors stored in the error's array.
Definition: jsonreader.h:105
wxString m_comment
The comment string read by SkipComment().
Definition: jsonreader.h:129