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,2024 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,
98  ErrEnd
99  };
100 
102  enum Flow {
103  Stream,
105  Pretty
106  };
107 protected:
108  enum Token {
109  TK_ERROR,
110  TK_EOF,
111  TK_COMMA,
112  TK_COLON,
113  TK_IDENT,
114  TK_NAN,
115  TK_INF,
116  TK_NULL,
117  TK_FALSE,
118  TK_TRUE,
119  TK_STRING,
120  TK_PLUS,
121  TK_MINUS,
122  TK_INT,
123  TK_HEX,
124  TK_REAL,
125  TK_LBRACK,
126  TK_LBRACE,
127  TK_RBRACK,
128  TK_RBRACE
129  };
130 protected:
131  FXString value; // Token value
132  FXlong offset; // Position from start
133  Token token; // Token
134  FXint column; // Column number
135  FXint indent; // Indent level
136  FXint line; // Line number
137  FXint wrap; // Line wrap column
138  FXchar quote; // Quote type used
139  FXuchar flow; // Output flow
140  FXuchar prec; // Float precision
141  FXuchar fmt; // Float format
142  FXuchar esc; // Escape mode
143  FXuchar dent; // Indentation amount
144  FXuchar ver; // Version
145 protected:
146  static const FXchar *const errors[];
147 protected:
148  virtual Token next();
149  Token ident();
150  Token string();
151  static Token identoken(const FXString& str);
152  Error loadMap(FXVariant& var);
153  Error loadArray(FXVariant& var);
154  Error loadVariant(FXVariant& var);
155  Error saveString(const FXString& str);
156  Error saveIdent(const FXString& str);
157  Error saveMap(const FXVariant& var);
158  Error saveArray(const FXVariant& var);
159  Error saveVariant(const FXVariant& var);
160 private:
161  FXJSON(const FXJSON&);
162  FXJSON &operator=(const FXJSON&);
163 public:
164 
168  FXJSON();
169 
174  FXJSON(FXchar* buffer,FXuval sz=8192,Direction d=Load);
175 
180  FXbool open(FXchar* buffer=nullptr,FXuval sz=8192,Direction d=Load);
181 
185  FXint getLine() const { return line; }
186 
190  FXint getColumn() const { return column; }
191 
195  FXlong getOffset() const { return offset; }
196 
201  virtual Error load(FXVariant& variant);
202 
207  virtual Error save(const FXVariant& variant);
208 
212  static const FXchar* getError(Error err){ return errors[err]; }
213 
219  void setNumericPrecision(FXuchar p){ prec=p; }
220  FXuchar getNumericPrecision() const { return prec; }
221 
232  void setNumericFormat(FXuchar f){ fmt=f; }
233  FXuchar getNumericFormat() const { return fmt; }
234 
244  void setOutputFlow(FXuchar f){ flow=f; }
245  FXuchar getOutputFlow() const { return flow; }
246 
251  void setIndentation(FXuchar d){ dent=d; }
252  FXuchar getIndentation() const { return dent; }
253 
257  void setLineWrap(FXint w){ wrap=w; }
258  FXint getLineWrap() const { return wrap; }
259 
270  void setEscapeMode(FXuchar e){ esc=e; }
271  FXuchar getEscapeMode() const { return esc; }
272 
276  void setVersion(FXuchar v){ ver=v; }
277  FXuchar getVersion() const { return ver; }
278 
282  void setQuote(FXchar q){ quote=q; }
283  FXchar getQuote() const { return quote; }
284 
289  FXbool close();
290 
294  virtual ~FXJSON();
295  };
296 
297 }
298 
299 #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:102
void setVersion(FXuchar v)
Change json version.
Definition: FXJSON.h:276
void setNumericPrecision(FXuchar p)
Floating point output precision control.
Definition: FXJSON.h:219
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:232
void setLineWrap(FXint w)
Change column at which lines are wrapped.
Definition: FXJSON.h:257
Illegal token.
Definition: FXJSON.h:89
FXint getColumn() const
Return current column number.
Definition: FXJSON.h:190
void setQuote(FXchar q)
Change quote type, either (&#39;) or (").
Definition: FXJSON.h:282
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:185
static const FXchar * getError(Error err)
Returns error for given error code.
Definition: FXJSON.h:212
Expected closing brace.
Definition: FXJSON.h:93
void setEscapeMode(FXuchar e)
Change string escape mode.
Definition: FXJSON.h:270
Unexpected identifier.
Definition: FXJSON.h:97
Definition: FX4Splitter.h:28
Stream-of-consciousness output.
Definition: FXJSON.h:104
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:244
Numeric conversion.
Definition: FXJSON.h:96
FXlong getOffset() const
Return offset from begin of file.
Definition: FXJSON.h:195
void setIndentation(FXuchar d)
Change indentation level for pretty print flow, the amount of indentation applied for each level...
Definition: FXJSON.h:251
Expected comma &#39;,&#39;.
Definition: FXJSON.h:91
Expected closing quote.
Definition: FXJSON.h:95

Copyright © 1997-2022 Jeroen van der Zijp