Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXJSON.h
1 /********************************************************************************
2 * *
3 * J S O N R e a d e r & W r i t e r *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2013,2022 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXJSON_H
22 #define FXJSON_H
23 
24 #ifndef FXPARSEBUFFER_H
25 #include "FXParseBuffer.h"
26 #endif
27 
28 namespace FX {
29 
30 
80 class FXAPI FXJSON : public FXParseBuffer {
81 public:
82 
84  enum Error {
85  ErrOK,
97  ErrEnd
98  };
99 
101  enum Flow {
102  Stream,
104  Pretty
105  };
106 protected:
107  enum Token {
108  TK_ERROR,
109  TK_EOF,
110  TK_COMMA,
111  TK_COLON,
112  TK_IDENT,
113  TK_NAN,
114  TK_INF,
115  TK_NULL,
116  TK_FALSE,
117  TK_TRUE,
118  TK_STRING,
119  TK_PLUS,
120  TK_MINUS,
121  TK_INT,
122  TK_HEX,
123  TK_REAL,
124  TK_LBRACK,
125  TK_LBRACE,
126  TK_RBRACK,
127  TK_RBRACE
128  };
129 protected:
130  FXString value; // Token value
131  FXlong offset; // Position from start
132  Token token; // Token
133  FXint column; // Column number
134  FXint indent; // Indent level
135  FXint line; // Line number
136  FXint wrap; // Line wrap column
137  FXchar quote; // Quote type used
138  FXuchar flow; // Output flow
139  FXuchar prec; // Float precision
140  FXuchar fmt; // Float format
141  FXuchar esc; // Escape mode
142  FXuchar dent; // Indentation amount
143  FXuchar ver; // Version
144 protected:
145  static const FXchar *const errors[];
146 protected:
147  virtual Token next();
148  Token ident();
149  Token string();
150  static Token identoken(const FXString& str);
151  Error loadMap(FXVariant& var);
152  Error loadArray(FXVariant& var);
153  Error loadVariant(FXVariant& var);
154  Error saveString(const FXString& str);
155  Error saveIdent(const FXString& str);
156  Error saveMap(const FXVariant& var);
157  Error saveArray(const FXVariant& var);
158  Error saveVariant(const FXVariant& var);
159 private:
160  FXJSON(const FXJSON&);
161  FXJSON &operator=(const FXJSON&);
162 public:
163 
167  FXJSON();
168 
173  FXJSON(FXchar* buffer,FXuval sz=8192,Direction d=Load);
174 
179  FXbool open(FXchar* buffer=nullptr,FXuval sz=8192,Direction d=Load);
180 
184  FXint getLine() const { return line; }
185 
189  FXint getColumn() const { return column; }
190 
194  FXlong getOffset() const { return offset; }
195 
200  virtual Error load(FXVariant& variant);
201 
206  virtual Error save(const FXVariant& variant);
207 
211  static const FXchar* getError(Error err){ return errors[err]; }
212 
218  void setNumericPrecision(FXuchar p){ prec=p; }
219  FXuchar getNumericPrecision() const { return prec; }
220 
231  void setNumericFormat(FXuchar f){ fmt=f; }
232  FXuchar getNumericFormat() const { return fmt; }
233 
243  void setOutputFlow(FXuchar f){ flow=f; }
244  FXuchar getOutputFlow() const { return flow; }
245 
250  void setIndentation(FXuchar d){ dent=d; }
251  FXuchar getIndentation() const { return dent; }
252 
256  void setLineWrap(FXint w){ wrap=w; }
257  FXint getLineWrap() const { return wrap; }
258 
269  void setEscapeMode(FXuchar e){ esc=e; }
270  FXuchar getEscapeMode() const { return esc; }
271 
275  void setVersion(FXuchar v){ ver=v; }
276  FXuchar getVersion() const { return ver; }
277 
281  void setQuote(FXchar q){ quote=q; }
282  FXchar getQuote() const { return quote; }
283 
288  FXbool close();
289 
293  virtual ~FXJSON();
294  };
295 
296 }
297 
298 #endif
A Variant type can hold any kind of object, be it a boolean, integer, real, string, or even array of Variants or dictionaries of variants.
Definition: FXVariant.h:44
No errors.
Definition: FXJSON.h:86
Flow
JSON serializer flow modes.
Definition: FXJSON.h:101
void setVersion(FXuchar v)
Change json version.
Definition: FXJSON.h:275
void setNumericPrecision(FXuchar p)
Floating point output precision control.
Definition: FXJSON.h:218
Expected closing bracket.
Definition: FXJSON.h:92
Expected closing quotes.
Definition: FXJSON.h:94
void setNumericFormat(FXuchar f)
Floating point output format control.
Definition: FXJSON.h:231
void setLineWrap(FXint w)
Change column at which lines are wrapped.
Definition: FXJSON.h:256
Illegal token.
Definition: FXJSON.h:89
FXint getColumn() const
Return current column number.
Definition: FXJSON.h:189
void setQuote(FXchar q)
Change quote type, either (&#39;) or (").
Definition: FXJSON.h:281
Unable to save.
Definition: FXJSON.h:87
The FXJSON serializer loads or saves an FXVariant to a JSON text file.
Definition: FXJSON.h:80
FXint getLine() const
Return current line number.
Definition: FXJSON.h:184
static const FXchar * getError(Error err)
Returns error for given error code.
Definition: FXJSON.h:211
Expected closing brace.
Definition: FXJSON.h:93
void setEscapeMode(FXuchar e)
Change string escape mode.
Definition: FXJSON.h:269
Definition: FX4Splitter.h:28
Stream-of-consciousness output.
Definition: FXJSON.h:103
Unable to load.
Definition: FXJSON.h:88
Error
JSON deserializer error codes.
Definition: FXJSON.h:84
Expected colon &#39;:&#39;.
Definition: FXJSON.h:90
FXParseBuffer manages pointers to a buffer for various file format parsers.
Definition: FXParseBuffer.h:34
void setOutputFlow(FXuchar f)
Change output flow format (Stream, Compact, Pretty).
Definition: FXJSON.h:243
Numeric conversion.
Definition: FXJSON.h:96
FXlong getOffset() const
Return offset from begin of file.
Definition: FXJSON.h:194
void setIndentation(FXuchar d)
Change indentation level for pretty print flow, the amount of indentation applied for each level...
Definition: FXJSON.h:250
Expected comma &#39;,&#39;.
Definition: FXJSON.h:91
Expected closing quote.
Definition: FXJSON.h:95

Copyright © 1997-2022 Jeroen van der Zijp