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